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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • webstorm 2021激活码【永久激活】

    (webstorm 2021激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlML…

    2022年3月21日
    271
  • UVA1455 – Kingdom(并查集 + 线段树)

    UVA1455 – Kingdom(并查集 + 线段树)

    2021年12月14日
    48
  • atitit groovy 总结java 提升效率

    atitit groovy 总结java 提升效率

    2021年12月7日
    31
  • WPF Visifire.Charts4.6.1使用教程 附含源码

    WPF Visifire.Charts4.6.1使用教程 附含源码原因:前段时间,公司项目中用到Visifire.Charts4.5.6控件,项目中要求随时可以控制动画效果,用于在大屏上面展示,很酷炫。过程:但是没有源码,于是写了一个方法用动画去控制数量动态增长,无奈效率太低,多实例几个Chart就卡到爆,放弃。没有源码,怎么办呢,无奈之下反编译了一下dll,刚开始用reflector反编译,发现编译出来的大部分都用不了。然后又用ILSpy反编译…

    2022年7月21日
    15
  • windows2003 dns 414错误「建议收藏」

    windows2003 dns 414错误「建议收藏」原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://rainbird.blog.51cto.com/211214/121697       因为原来在Linux下实现过域根dns.所以朋友也想做域名用自己的dns服务器的时候肯定是一马当先的帮着做了。操作系统这回用的是2003。用windows配置服务就一个字:“简单”。

    2022年6月11日
    49
  • 错误:Unbound classpath container: ‘JRE System Library [JavaSE-1.7]’ in project[通俗易懂]

    错误:Unbound classpath container: ‘JRE System Library [JavaSE-1.7]’ in project[通俗易懂]用新的Eclipse创建Maven项目时出现的问题。经过查找资料,是jre问题。解决方案:              项目右键—>Properties—>Java Build Path—>Libraries—>按照下图操作 再重新添加Jre可以选择工作空间默认的jre,也可以重新添加外部的jre,如果选择默认的点击完成…

    2022年6月13日
    34

发表回复

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

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