当前位置:Gxlcms > css > 双飞翼布局与圣杯布局图文详解

双飞翼布局与圣杯布局图文详解

时间:2021-07-01 10:21:17 帮助过:69人阅读

这次给大家带来双飞翼布局与圣杯布局图文详解,使用双飞翼布局与圣杯布局的注意事项有哪些,下面就是实战案例,一起来看一下。

双飞翼布局和圣杯布局都是实现两边固定中间自适应的三栏布局的方式,最近在整理三栏布局实现方式的笔记,决定但拉出来一篇,记一下这两个经典布局。

1、圣杯布局

浮动、负边距、相对定位、不添加额外标签

效果图

DOM结构:

  1. <p class="header">Header</p>
  2. <p class="bd">
  3. <p class="main">Main</p>
  4. <p class="left">Left</p>
  5. <p class="right">Right
  6. </p>
  7. </p>
  8. <p class="footer">Footer</p>

样式:

  1. <style>
  2. body{padding:0;margin:0}
  3. .header,.footer{width:100%; background: #666;height:30px;clear:both;}
  4. .bd{
  5. padding-left:150px;
  6. padding-right:190px;
  7. }
  8. .left{
  9. background: #E79F6D;
  10. width:150px;
  11. float:left;
  12. margin-left:-100%;
  13. position: relative;
  14. left:-150px;
  15. }
  16. .main{
  17. background: #D6D6D6;
  18. width:100%;
  19. float:left;
  20. }
  21. .right{
  22. background: #77BBDD;
  23. width:190px;
  24. float:left;
  25. margin-left:-190px;
  26. position:relative;
  27. right:-190px;
  28. }
  29. </style>

左中右部分样式变化过程

1、中间部分需要根据浏览器宽度的变化而变化,所以要用100%,这里设*左中右向左浮动,因为中间100%,左层和右层根本没有位置上去

  1. .left{
  2. background: #E79F6D;
  3. width:150px;
  4. float:left;
  5. }
  6. .main{
  7. background: #D6D6D6;
  8. width:100%;
  9. float:left;
  10. }
  11. .right{
  12. background: #77BBDD;
  13. width:190px;
  14. float:left;
  15. }

2、把左层负margin150后,发现left上去了,因为负到出窗口没位置了,只能往上挪

  1. .left{
  2. background: #E79F6D;
  3. width:150px;
  4. float:left;
  5. margin-left:-150px;
  6. }

3、那么按第二步这个方法,可以得出它只要挪动窗口宽度那么宽就能到最左边了,利用负边距,把左右栏定位

  1. .left{
  2. background: #E79F6D;
  3. width:150px;
  4. float:left;
  5. margin-left:-100%;
  6. }
  7. .right{
  8. background: #77BBDD;
  9. width:190px;
  10. float:left;
  11. margin-left:-190px;
  12. }

4、然而问题来了,中间被左右挡住了啊,只好给外层加padding了

  1. .bd{
  2. padding-left:150px;
  3. padding-right:190px;
  4. }

5、但是加了之后左右栏也缩进来了,于是采用相对定位方法,各自相对于自己把自己挪出去,得到最终结果

  1. .left{
  2. background: #E79F6D;
  3. width:150px;
  4. float:left;
  5. margin-left:-100%;
  6. position: relative;
  7. left:-150px;
  8. }
  9. .right{
  10. background: #77BBDD;
  11. width:190px;
  12. float:left;
  13. margin-left:-190px;
  14. position:relative;
  15. right:-190px;
  16. }

2、双飞翼布局

在不增加额外标签的情况下,圣杯布局已经非常完美,圣杯布局使用了相对定位,以后布局是有局限性的,而且宽度控制要改的地方也多,那么有没其他方法更加简洁方便呢?

在淘宝UED探讨下,增加多一个p就可以不用相对布局了,只用到了浮动和负边距,这就是我们所说的双飞翼布局。

DOM结构:main内层增加了一个p

  1. <p class="header">Header</p>
  2. <p class="bd">
  3. <p class="main">
  4. <p class="inner"> Main </p>*
  5. </p>
  6. <p class="left">Left</p>
  7. <p class="right">Right </p>
  8. </p>
  9. <p class="footer">Footer</p>

样式:

去掉了左右栏的相对定位

去掉包裹层padding,以中间栏新增p的margin代替

  1. body{
  2. padding:0;
  3. margin:0
  4. }
  5. .header,.footer{
  6. width:100%;
  7. background:#666;
  8. height:30px;clear:both;
  9. }
  10. .bd{
  11. /*padding-left:150px;*/
  12. /*padding-right:190px;*/
  13. }
  14. .left{
  15. background: #E79F6D;
  16. width:150px;
  17. float:left;
  18. margin-left:-100%;
  19. /*position: relative;*/
  20. /*left:-150px;*/
  21. }
  22. .main{
  23. background: #D6D6D6;
  24. width:100%;
  25. float:left;
  26. }
  27. .right{
  28. background: #77BBDD;
  29. width:190px;
  30. float:left;
  31. margin-left:-190px;
  32. /*position:relative;*/
  33. /*right:-190px;*/
  34. }
  35. .inner{
  36. margin-left:150px;
  37. margin-right:190px;

3、双飞翼布局和圣杯布局的区别

圣杯布局和双飞翼布局解决问题的方案在前一半是相同的,即:

  • 中间栏宽度设置为100%

  • 三栏全部float浮动

  • 左右两栏加上负margin让其跟中间栏p并排,以形成三栏布局。

不同在于解决中间栏p内容不被遮挡问题的思路不一样。

圣杯布局

  • 将三栏的外包裹层设置左右padding-left和padding-right

  • 将左右两个p用相对布局position: relative并分别配合right和left属性,相对自身移动以便不遮挡中间p

双飞翼布局

  • 中间p内部创建子p用于放置内容

  • 在该子p里用margin-left和margin-right为左右两栏p留出位置

多了1个p,少用4个css属性(圣杯布局中间pp的adding-left和padding-right这2个属性,加上左右两个p用相对布局position: relative及对应的right和left共4个属性,一共6个;而双飞翼布局子p里用margin-left和margin-right共2个属性,6-2=4)。

并且双飞翼布局还有个好处,让Main变成BFC元素了,屏幕宽度缩小Main也不会被挤下去,圣杯布局就会被挤下去。

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!
推荐阅读:

css3的Transition平滑过渡菜单栏实现

css如何做出0.5像素的线条

以上就是双飞翼布局与圣杯布局图文详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行