【CSS】 布局之圣杯布局
在看众多大神的css布局指南时,经常看到一个布局:圣杯布局(也有称为双飞翼布局的)。今天我们也来剖析一下。
其实,对于众多css布局,我们只要明确理解了3种技术,那么基本上大多数布局都难不倒我们了:
浮动 float
绝对定位和相对定位 negative margin
负边距 relative position
浮动
浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。
由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样。(w3c)
上面是官方的界定。我们可以用"float:left"和"float:right"来使元素块向左向右浮动。
当一个元素块被浮动后,其在原文档流中的空间被关闭,后面的内容会向前补充,这样很容易造成后面的元素包围了浮动的元素块。
为了解决这个问题,我们可以用clear方法来阻止行框包围元素块。
绝对定位和相对定位
我们通过position属性来声明这个元素块进行绝对定位或者相对定位。如果要进行定位的是行内元素,首先需要用display:block来将其声明为一个块级元素。
两者的区别为:
相对定位是相对于自身在文档流中的原有位置的起点为基准进行位移定位。其在文档流中的位置不会被关闭。
绝对定位是相对于最接近的已定位父元素的起点为基准,如果没有已定位的父元素,则为其最初的包含快,也就是body。绝对定位相对于相对定位的不同,是其在文档流中的元素框被关闭,该位置被后面的元素填充。
负边距
在css中,有两种边距,分别为内边距padding和外边距margin。
内边距padding是元素内动与边框之间的距离,外边距margin为元素边框到父元素框外边距之间的距离。
内边距padding只能设置为正值,而外边距margin可以设置为正值也可以设置为负值。
我们还需要了解一点,元素的背景是可以占用内边距padding距离的。
了解了以上三种方法,我们就可以通过这三种方法来实现我们的各种布局。
这包括了下面介绍的圣杯布局:
先写一个基本布局:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>圣杯布局/双飞翼布局</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
text-Align: center;
}
#parents {
width: 600px;
border: 2px solid;
margin: 0 auto;
}
#top {
background: #666;
}
#left {
background: #E79F6D;
}
#content {
background: #D6D6D6;
}
#right {
background: #77BBDD;
}
#foot {
background: #666;
}
</style>
</head>
<body>
<div id="parents">
<div id="top">这是Top!</div>
<div id="main">
<div id="left">这是Left!</div>
<div id="content">这是Content!</div>
<div id="right">这是Right!</div>
</div>
<div id="foot">这是Foot!</div>
</div>
</body>
</html>
效果:
我们将中间left、content和right向左浮动,并设置left和right的宽度分别设置为100px和150px,将三者的父元素的左右内边距设置为100px和150px,与left和right的宽度相同。然后我们将foot清除浮动。
<style type="text/css">
* {
margin: 0;
padding: 0;
text-Align: center;
}
#parents {
width: 600px;
border: 2px solid;
margin: 0 auto;
}
#top {
background: #666;
}
#main {
margin-left: 100px;
margin-right: 150px;
}
#left {
background: #E79F6D;
float: left;
width: 100px;
}
#content {
background: #D6D6D6;
float: left;
width: 100%;
}
#right {
background: #77BBDD;
float: left;
width: 150px;
}
#foot {
background: #666;
clear: both;
}
</style>
效果:
我们发现,由于content宽度设置了100%,故而其宽度不够,所以content和right就掉下来了,对于这个我们可以为content和right设置一个margin-left属性来解决这个问题。
#left {
background: #E79F6D;
float: left;
width: 100px;
}
#content {
background: #D6D6D6;
float: left;
width: 100%;
margin-left: -100px;
}
#right {
background: #77BBDD;
float: left;
width: 150px;
margin-left: -150px;
}
效果:
上图中left被content挡住了。
随后,我们用相对定位把left向左移,right向右移。并把top和foot设置高度为50px;
#top {
background: #666;
height: 50px;
}
#main {
padding-left: 100px;
padding-right: 150px;
}
#left {
background: #E79F6D;
float: left;
width: 100px;
position: relative;
left: -100px;
}
#content {
background: #D6D6D6;
float: left;
width: 100%;
margin-left: -100px;
}
#right {
background: #77BBDD;
float: left;
width: 150px;
margin-left: -150px;
position: relative;
left: 150px;
}
#foot {
background: #666;
clear: both;
height: 50px;
}
效果:
看起来差不多了,不过我们还需要处理一个问题,就是中间三列的等高问题,这个是在前面已经探讨过的多列等高问题。
<style type="text/css">
* {
margin: 0;
padding: 0;
text-Align: center;
}
#parents {
width: 600px;
border: 2px solid;
margin: 0 auto;
}
#top {
background: #666;
height: 50px;
}
#main {
padding-left: 100px;
padding-right: 150px;
overflow: hidden;
}
#left,#content,#right {
padding-bottom: 2000px;
margin-bottom: -2000px;
}
#left {
background: #E79F6D;
float: left;
width: 100px;
position: relative;
left: -100px;
}
#content {
background: #D6D6D6;
float: left;
width: 100%;
margin-left: -100px;
}
#right {
background: #77BBDD;
float: left;
width: 150px;
margin-left: -150px;
position: relative;
left: 150px;
}
#foot {
background: #666;
clear: both;
height: 50px;
}
</style>
效果:
这样就没问题了,当我们给三者无论哪个添加内容时,三列的高度总会以最高的那列为准维持等高。
在这个布局中,主要内容content的宽度是自适应的,而left和right的宽度是固定的,当我们增加parents的宽度时,content的宽度随之增加,left和right保持不变。
这就是圣杯布局,也有叫做双飞翼布局的。
而且我们通过相对定位,可以通过计算,随意定制left、content和right三者的前后顺序。
关于圣杯布局,引用一下CobbySung总结的优缺点:
优点:
- 实现了内容与布局的分离,即Eric提到的Any-Order Columns.
- content部分是自适应宽度的,很容易在定宽布局和流体布局中切换。
- 任何一栏都可以是最高栏,不会出问题。
- 需要的hack非常少(就一个针对ie6的清除浮动hack:_zoom: 1;)
- 在浏览器上的兼容性非常好,IE5.5以上都支持。
不足:
- content需要添加一个额外的包裹层。
- 等待你的发现与反馈。
最后附上完整代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>圣杯布局/双飞翼布局</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
text-Align: center;
}
#parents {
width: 1000px;
border: 2px solid;
margin: 0 auto;
}
#top {
background: #666;
height: 50px;
}
#main {
padding-left: 100px;
padding-right: 150px;
overflow: hidden;
}
#left,#content,#right {
padding-bottom: 2000px;
margin-bottom: -2000px;
}
#left {
background: #E79F6D;
float: left;
width: 100px;
position: relative;
right: 150px;
left: 100px; }
#content {
background: #D6D6D6;
float: left;
width: 100%;
margin-left: -100px;
position: relative;
left: -100px;
}
#right {
background: #77BBDD;
float: left;
width: 150px;
margin-left: -150px;
position: relative;
left: 150px;
}
#foot {
background: #666;
clear: both;
height: 50px;
}
</style>
</head>
<body>
<div id="parents">
<div id="top">这是Top!</div>
<div id="main">
<div id="left">这是Left!</div>
<div id="content">这是Content!<br><br><br><br>这是多行高度!<br></div>
<div id="right">这是Right!</div>
</div>
<div id="foot">这是Foot!</div>
</div>
</body>
</html>
最新文章
- 转-基于NodeJS的14款Web框架
- 用python实现最长公共子序列算法(找到所有最长公共子串)
- SQLite 加密 -- SQLCipher
- MY_FIRSH_MODULE
- 【Oracle】Oracle时间日期格式
- maven编译设置pom.xml
- 20145236 《Java程序设计》第4周学习总结
- 【BZOJ 1103】 [POI2007]大都市meg
- javascript高级编程笔记03(正则表达式)
- Java基础知识强化之集合框架笔记48:产生10个1~20之间的随机数(要求:随机数不能重复) 简洁版
- Something broke! (Error 500)——reviewboard
- The Most Wanted Letter
- ARM体系结构与编程
- HTTP协议与HTML form
- POJ1221(整数划分)
- Oracle查询临时表空间的占用
- java中值类型与引用类型的关系
- $(";#form1";). serialize()提交表单
- j.u.c系列(10)---之并发工具类:Semaphore
- Spark of work