CSS经典布局——圣杯布局与双飞翼布局
一、圣杯布局和双飞翼布局的目的
- 实现三栏布局,中间一栏最先加载和渲染
- 两侧内容固定,中间内容随着宽度自适应
- 一般用于PC网
二、圣杯布局的实现
技术要点:
- 设置最小宽度min-width
- 使用float布局,注意清除浮动
- 使用margin负值(如果对margin负值不太了解的话,可以看看)
- 对三栏整体区域设置左右内边距,宽度为left和right的宽度,避免内容重叠
- 使用定位
1 2 3 4 5 6 7 8圣杯布局 9 10 58 59 60this is header 6162 66 67 68this is center63this is left64this is right65
三、双飞翼布局的实现
技术要点:
- 设置最小宽度min-width
- 使用float布局,注意清除浮动
- 使用margin负值(双飞翼布局不需要使用margin-right负值)
- 对center设置左右外边距,避免与侧边栏内容重叠
代码如下:
双飞翼布局
this is header
this is center
this is left
this is right
圣杯布局与双飞翼布局效果图如下:
四、圣杯布局与双飞翼布局的区别
- 圣杯布局使用了margin-right负值,相对来说比较难理解
- 圣杯布局设置的是内边距padding,避免内容重叠。而双飞翼布局是给center元素添加了一个父盒子,只需要设置center的外边距即可避免与左右侧边栏重叠
五、总结
圣杯布局和双飞翼布局的技术总结:
- 使用了float布局
- 两侧使用margin负值,以便和中间内容横向重叠
- 防止中间内容被两侧覆盖,一个用padding,一个用margin