html圣杯布局,三种实现圣杯布局方法「建议收藏」

html圣杯布局,三种实现圣杯布局方法「建议收藏」本文介绍三种实现圣杯布局的方法,前两种可归为一类,与第三种方法差别在于加载顺序不同。圣杯布局实现1:步骤1:给出HTML结构:圣杯布局1*{margin:0;padding:0;text-Align:center;}#top{background:#666;height:60px;}#left{background:#E79F6D;}#content{background:…

大家好,又见面了,我是你们的朋友全栈君。

本文介绍三种实现圣杯布局的方法,前两种可归为一类,与第三种方法差别在于加载顺序不同。

圣杯布局实现1:

步骤1:给出 HTML结构:

圣杯布局1

* {

margin: 0;

padding: 0;

text-Align: center;

}

#top {

background: #666;

height: 60px;

}

#left {

background: #E79F6D;

}

#content {

background: #D6D6D6;

}

#right {

background: #77BBDD;

}

#foot {

background: #666;

height: 50px;

}

这是Top!
这是Left!
这是Content!

这是多行高度!

效果如下:

9ffda0159c4d

Paste_Image.png

步骤2:

设置中间三个div向左浮动,使其排列在一行: #left, #content,#right{ float:left;}。

设置Foot元素清除浮动,阻止与上面的main部分重叠:#foot { background: #666; height: 50px; clear: both;}。

设置中间三个div的宽度,左右定宽,中间宽度自适应:

#left {

background: #E79F6D;

width: 150px;

}

#content {

background: #D6D6D6;

width: 100%;

}

#right {

background: #77BBDD;

width: 200px;

}

效果如下:

9ffda0159c4d

Paste_Image.png

步骤3:

现在Content宽度为100%,占满整个浏览器宽度,若想要中间Content给两边Left和Right挪出空间,则需要设置容器main左右padding值。

由于容器main内元素均设置左浮动,都不在文档流中,导致main高度塌陷,设置overflow:hidden可解决该问题。

#main{

padding-left: 150px;

padding-right: 200px;

background-color: bisque;

}

效果如下:

9ffda0159c4d

Paste_Image.png

步骤4:

由于设置padding后,中间三个div宽度之和超过浏览器宽度,所以各独占一行,要使Left元素和Right元素分别移动到Content的左右两边,则需要给Left设置负的margin-left,给Right设置负的margin-right。

#left {

background: #E79F6D;

width: 150px;

margin-left: -150px;;

}

#right {

background: #77BBDD;

width: 200px;

margin-right: -200px

}

效果如下:

9ffda0159c4d

Paste_Image.png

步骤5:

使中间三个div高度自适应,即高度相等且等于高度最大的div,使用内外下边距相抵消的方法:

#left, #content, #right { float:left; padding-bottom: 2000px; margin-bottom: -2000px; }

最终效果如下:

9ffda0159c4d

Paste_Image.png

最终代码:

圣杯布局1

* {

margin: 0;

padding: 0;

text-Align: center;

}

#top {

background: #666;

height: 60px;

}

#left, #content,#right{

float:left;

margin-bottom: -2000px;

padding-bottom: 2000px;

}

#main{

padding-left: 150px;

padding-right: 200px;

background-color: bisque;

overflow: hidden;

}

#left {

background: #E79F6D;

width: 150px;

margin-left: -150px;;

}

#content {

background: #D6D6D6;

width: 100%;

}

#right {

background: #77BBDD;

width: 200px;

margin-right: -200px;

}

#foot {

background: #666;

height: 50px;

clear: both;

}

这是Top!
这是Left!
这是Content!

这是多行高度!
这是Right!

这是Foot!

圣杯布局实现2:

与方法1的前三步相同,在第四步时采用了不同的解决方案:

步骤4-1

通过为Content和Right设置一个负的margin-left属性:

#content {

background: #D6D6D6;

width: 100%;

margin-left: -150px;

}

#right {

background: #77BBDD;

width: 200px;

margin-left: -200px;

}

效果如下:(此时Left被覆盖)

9ffda0159c4d

Paste_Image.png

步骤4-2

设置Left和Right的positon分别调整其位置使左移和右移:

#left {

background: #E79F6D;

width: 150px;

position: relative;

left: -150px;

}

#right {

background: #77BBDD;

width: 200px;

margin-left: -200px;

position: relative;

left: 200px;

}

效果如下:

9ffda0159c4d

Paste_Image.png

步骤四完成,步骤五同方法1。

最终代码:

圣杯布局1

* {

margin: 0;

padding: 0;

text-Align: center;

}

#top {

background: #666;

height: 60px;

}

#left, #content,#right{

float:left;

padding-bottom: 2000px;

margin-bottom: -2000px;

}

#main{

padding-left: 150px;

padding-right: 200px;

background-color: bisque;

overflow: hidden;

}

#left {

background: #E79F6D;

width: 150px;

position: relative;

left: -150px;

}

#content {

background: #D6D6D6;

width: 100%;

margin-left: -150px;

}

#right {

background: #77BBDD;

width: 200px;

margin-left: -200px;

position: relative;

left: 200px;

}

#foot {

background: #666;

height: 50px;

clear: both;

}

这是Top!
这是Left!
这是Content!

这是多行高度!
这是Right!

这是Foot!

圣杯布局实现3(先加载主列):

效果如下:

9ffda0159c4d

4770448-f4aeb930e2732d89.png

最终代码:

polandeme

body{

color: aliceblue;

}

.main-wrap{

margin-left: 190px;

margin-right: 190px;

}

/*.grid-s5m0e5 { width:100%;}*/

.col-main {

float:left;

width: 100%;

min-height:30px;

background:#000;

}

.col-sub {

float:left;

margin-left: -100%;

width:190px;

min-height:30px;

background:#f00;

}

.col-extra {

margin-left: -190px;

float:left;

width:190px;

min-height:30px;

background:#00f;

}

我是主列,出来吧!

我是子列
我是附加列

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/149646.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • CTK 插件框架介绍

    CTK 插件框架介绍CTKPluginFramework:IntroductionTheCTKPluginFrameworkcanshortlybedescribedasadynamiccomponentsystemforC++.CTK插件框架可以简要的描

    2022年6月1日
    139
  • readandwritelock_读写锁使用场景

    readandwritelock_读写锁使用场景ReadWriteLock管理一组锁,一个是只读的锁,一个是写锁。读锁可以在没有写锁的时候被多个线程同时持有,写锁是独占的。所有读写锁的实现必须确保写操作对读操作的内存影响。换句话说,一个获得了读锁的线程必须能看到前一个释放的写锁所更新的内容。读写锁比互斥锁允许对于共享数据更大程度的并发。每次只能有一个写线程,但是同时可以有多个线程并发地读数据。ReadWriteLock适用于读多写少的并发

    2022年8月12日
    8
  • tlb表项_谷物对人体的好处

    tlb表项_谷物对人体的好处TLB:TranslationLookasideBuffer.根据功能可以译为快表,直译可以翻译为旁路转换缓冲,也可以把它理解成页表缓冲。里面存放的是一些页表文件(虚拟地址到物理地址的转换表)。当处理器要在主内存寻址时,不是直接在内存的物理地址里查找的,而是通过一组虚拟地址转换到主内存的物理地址,TLB就是负责将虚拟内存地址翻译成实际的物理内存地址,而CPU寻址时会优先在TLB中进行寻址。处理器的性能就和寻址的命中率有很大的关系。映射机制必须使一个程序能断言某个地址在其自己的进程空间或地址空间

    2025年5月28日
    4
  • goland2021激活码3月最新在线激活

    goland2021激活码3月最新在线激活,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月15日
    55
  • webstorm激活码最新2021(注册激活)

    (webstorm激活码最新2021)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~MLZPB5EL5Q-eyJsaWNlb…

    2022年3月21日
    228
  • Linux下定时执行脚本实例

    Linux下定时执行脚本实例在Linux下我们用crontab来实现定期执行脚本。查看crontab    输入命令:cat/etc/crontabcrontab-u//设定特定用户的定时服务crontab-l//列出当前用户定时服务内容crontab-r//删除当前用户的定时服务crontab-e//编辑当前用户的定时服务在设定编辑之前都建议列出服务查看一下:crontab-l显示有一个已经设定好的…

    2022年7月17日
    17

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号