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)
上一篇 2022年6月24日 下午5:36
下一篇 2022年6月24日 下午5:46


相关推荐

  • Telerik RadControls for ASP.NET AJAX 2010 Q2最新版下载+源码下载

    Telerik RadControls for ASP.NET AJAX 2010 Q2最新版下载+源码下载TelerikRadControlsforASP.NETAJAX2010Q2RadControlsforASP.NET是一套强大的用户界面控件套装,它可以帮助您创建拥有桌面应用程序华丽外表和高速性能的Web应用程序。18种可靠的UI及数据控件全面提供AJAX性能,使用户可以得到高级的体验。但RadControlsforASP.NET不仅仅只支持AJAX,尽管其包含的控件以一流的性能帮助开发者执行AJAX。RadControlsforASP.NET同样还可以跨浏览器支持,兼容XHTM

    2022年7月19日
    15
  • 《哈佛幸福课》笔记

    《哈佛幸福课》笔记前言:幸福感是衡量人生的唯一标准,是所有目标的最终目标积极心理学第一课什么是积极心理学  与缺乏实质内容的心理自助行动不同,积极心理学从学术界而来,有着严谨的学术内容,并且兼顾应用到生活的方方面面,在象牙塔和大众之间构成一座桥梁。  学会安静反省,享受安静的理念,从小白鼠的实验中可知,有时候安静下来思考和重放过去的行为和知识,会学到更多。重现可能形成一种学习记忆机制。…

    2022年7月18日
    22
  • Sereja and Suffixes

    Sereja and Suffixes J- SerejaandSuffixesTimeLimit:1000MS     MemoryLimit:262144KB     64bitIOFormat:%I64d&%I64uSubmit StatusDescriptionSerejahasanarray a,consistingof n integers a1, a2, …, …

    2025年7月5日
    6
  • Ubantu下通过iptables开放端口「建议收藏」

    Ubantu下通过iptables开放端口「建议收藏」1.iptablesan安装#检查是否安装@shylin:/etc#sudowhereisiptablesiptables:/sbin/iptables/usr/share/iptables/usr/share/man/man8/iptables.8.gz#安装sudoapt-getinstalliptables2.查看防火墙的配置信息root…

    2022年10月20日
    5
  • 强制升级openssl导致无法使用SSH(生成ssh公钥)

    最近由于工作需求要搭建freeswitch,整个过程比较纠结,遂总结于此留作备忘。起源由于我的需求是将freeswitch跑起来,所以先按照freeswitchwiki上的介绍编译freeswitch,但configure过程便遇到问题:configure:error:OpenSSL>=1.0.1eandassociateddevelopementheadersrequi…

    2022年4月15日
    74
  • JAVA通过FDFS上传以及加载文件原理简析

    JAVA通过FDFS上传以及加载文件原理简析初学者的总结 仅是简单案例如果我们想在 java 后台中将图片上传到 linux 下的 FDFS 分布式系统中 首先通过 VMware 虚拟机安装 linux 系统 进入系统后 需要安装以下服务 tracker storage client nginx 这几个服务首先说说他们的基本工作原理 客户端发送请求 将文件通过 fdfs 插件上传 因为 tracker 不支持 http 协议 客户端不能通过 HTTP 直接访问 需要

    2026年3月26日
    2

发表回复

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

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