圣杯布局的来历是2006年发在a list part上的这篇文章:
In Search of the Holy Grail · An A List Apart Article
圣杯是西方表达“渴求之物"的意思,不是一种对页面的形象表达。
双飞翼据考源自淘宝UED,应该是一种页面的形象的表达。
圣杯布局和双飞翼布局解决的问题是一样的,就是两边顶宽,中间自适应的三栏布局,中间栏要在放在文档流前面以优先渲染。
圣杯布局和双飞翼布局解决问题的方案在前一半是相同的,也就是三栏全部float浮动,但左右两栏加上负margin让其跟中间栏div并排,以形成三栏布局。
不同在于解决”中间栏div内容不被遮挡“问题的思路不一样:
圣杯布局,为了中间div内容不被遮挡,将中间div设置了左右padding-left和padding-right后,将左右两个div用相对布局position: relative并分别配合right和left属性,以便左右两栏div移动后不遮挡中间div。
双飞翼布局,为了中间div内容不被遮挡,直接在中间div内部创建子div用于放置内容,在该子div里用margin-left和margin-right为左右两栏div留出位置。
多
了1个div,少用大致4个css属性(圣杯布局中间divpadding-left和padding-right这2个属性,加上左右两个div用相对
布局position:
relative及对应的right和left共4个属性,一共6个;而双飞翼布局子div里用margin-left和margin-right共2个
属性,6-2=4),个人感觉比圣杯布局思路更直接和简洁一点。
简单说起来就是”双飞翼布局比圣杯布局多创建了一个div,但不用相对布局了“。
具体代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>圣杯、双飞翼</title>
<style type="text/css">
.warp
{
margin: 0;
padding: 0;
}
.box
{
width: 100%;
}
.mian
{
width: 100%;
background: #CCFFCC;
float: left;
}
.inner
{
margin: 0 200px 0 200px;
}
.sub
{
float: left;
width: 200px;
background: #DDDDDD;
margin-left: -100%;
}
.extra
{
width: 200px;
float: left;
background: #CCDD99;
margin-left: -200px;
}
.box2
{
padding-left: 200px;
padding-right: 200px;
}
.mian2
{
width: 100%;
background: #CCFFCC;
float: left;
}
.sub2
{
float: left;
width: 200px;
background: #DDDDDD;
margin-left: -100%;
position: relative;
left: -200px;
}
.extra2
{
width: 200px;
float: left;
background: #CCDD99;
margin-left: -200px;
position: relative;
right: -200px;
}
</style>
</head>
<body>
<div class="warp">
<h3>
双飞翼</h3>
<div class="box">
<div class="mian">
<div class="inner">
我是主流
</div>
</div>
<div class="sub">
我是左边
</div>
<div class="extra">
我是右边
</div>
</div>
<br />
<h3>
圣杯</h3>
<div class="box2">
<div class="mian2">
我是主流
</div>
<div class="sub2">
我是左边
</div>
<div class="extra2">
我是右边
</div>
</div>
</div>
</body>
</html>
运行结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABCIAAAC9CAIAAADpzEuBAAAXsElEQVR4nO3dT2icZ54n8Oemg2mMD2IOdhOWEIXCBB3GB5FmCDFLw1ZAdPmwJtA6mSw9Zl2Qvoj1IToZtmAql3ZY1x6Sg2cmdMULYrczUzQrCASSi2IQoyHNMGJwOwymDzo0PtSt9vDW+9b7Vr31R9Lj1J/9fPhCx6W3SnIjys+X3/O8FXoAAABRhXn/AAAAwKpRMwAAgMjUDAAAIDI1AwAAiEzNAAAAIlMzAACAyNQMAAAgMjUDAACITM0AAAAiUzMAAIDI1AwAACAyNQMAAIhsuGYcNzZDmZ2dndLHky/u9/YnfDmEEMJm47j0Bxj7xM3G/pifJezsv/r/YwAAgPOKUTN+ev/bs9SMF1/ff+edva+ed/t/7h7cW1+7/MbNX39y8E+n3V7aPDYbx72Thz8Ll/7DzV9/+s2/vcz9dGoGAAAssmmbpr68szZ2gJBWknGDirKLXn734Ebyimtv/urL571er9fr/ukP3/3j3/63//QX4cqd//Xsh9MnWc3onv7rN59+tP2Xl9fCu4/+qGYAAMBSGK0Z3ef/8Ou/vP34RfKn/pTiyr2D47//cHvrtfXK/W/TK9MJxu0n3ZFXyaQ14/aTbu/F1/f7HSOE9Z8/Our2er0/fHwjP/RYu/wf/8fHac3onv7TwSdJy1i7fHX90u0HD9QMAABYeMM149//7heXQgjh9fvf5kvAZuM47Qvbj0+TS9MHfjooHiWymvE/n9y52u8Yazfuf/0ivWD/zuWrW9sfNj47ePrDabfXG2yaOtrfubx+/eb7HzY+++Lg6bMfT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