css-圣杯布局和双飞翼布局

淩亂°似流年 2022-05-29 12:35 373阅读 0赞

1.圣杯布局:

(1)父元素中包含左中右三个盒子,并且让他们分别左浮动。

(2)将中间的盒子宽度设为100%,并且它在html中代码位置放在左右代码位置的上面(就是所说的第一个渲染)。

(3)左右两边使用margin-left的负值,使他们与中间的盒子同行,(左边:-100%,右边:-右边盒子的宽度)。

(4)父元素用padding为左右盒子留位置。

(5)用相对定位将左右盒子移到正确的位置上

(6)给父元素清除浮动

代码如下:

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>圣杯布局</title>
  6. <style type="text/css">
  7. .main{width: 810px;height: 400px;border: 1px blue solid;padding-top: 25px;padding-left: 10px;padding-right: 10px;}
  8. ._center{width: 100%;height: 350px;text-align: center;line-height: 350px;float: left;background-color: #eeeeee}
  9. ._left{width: 200px;height: 350px;left: 20px;top:25px;text-align: center;line-height: 350px;float: left;margin-left: -100%;background-color: red;}
  10. ._right{width: 200px;height: 350px;right:20px;top:25px;text-align: center;line-height: 350px;float: left;margin-left:-200px;background-color: green;}
  11. </style>
  12. </head>
  13. <body>
  14. <div class="main clearfix">
  15. <div class="_center">我是中间</div>
  16. <div class="_left">我是左边</div>
  17. <div class="_right">我是右边</div>
  18. </div>
  19. </body>
  20. </html>

效果图:

20180323212946735

2.双飞翼布局:
(1)父元素中包含左中右三个盒子,并且让他们分别左浮动。

(2)将中间的盒子宽度设为100%,并且它在html中代码位置放在左右代码位置的上面(就是所说的第一个渲染)。

(3)左右两边使用margin-left的负值,使他们与中间的盒子同行,(左边:-100%,右边:-右边盒子的宽度)。

(4)给中间元素设置子元素,并给其左右margin为左右盒子留位置

(6)给父元素清除浮动.

代码如下:

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>双飞翼布局</title>
  6. <style type="text/css">
  7. .main{width: 800px;height: 400px;padding-top: 25px;margin: 0;}
  8. ._center{width: 100%;height: 350px;float: left;background-color: #eeeeee;}
  9. ._center>div{margin: 0 200px 0 200px;}
  10. ._left{width: 200px;height: 350px;left: 20px;top:25px;text-align: center;line-height: 350px;float: left;margin-left: -100%;background-color: red;}
  11. ._right{width: 200px;height: 350px;right:20px;top:25px;text-align: center;line-height: 350px;float: left;margin-left:-200px;background-color: green;}
  12. </style>
  13. </head>
  14. <body>
  15. <div class="main clearfix">
  16. <div class="_center">
  17. <div>我是中间我是中间我是中间我是中间我是中间我是中间我是中间我是中间我是中间我是中间我是中间我是中间我是中间我是中间我是中间我是中间我是中间我是中间我是中间我是中间我是中间我是中间我是中间</div>
  18. </div>
  19. <div class="_left">我是左边</div>
  20. <div class="_right">我是右边</div>
  21. </div>
  22. </body>
  23. </html>

效果如下:

20180323215455680

注:为中间元素设置子元素,并为其设置margin,目的是为了中间元素的内容可以在中间显示,margin的左右值为左右盒子的宽度。

发表评论

表情:
评论列表 (有 0 条评论,373人围观)

还没有评论,来说两句吧...

相关阅读

    相关 布局布局

    圣杯布局和双飞翼布局解决的问题是一样的,就是两边定宽,中间自适应的三栏布局,中间栏要在放在文档流前面以优先渲染。 两者相同之处是实现三栏布局,都是利用float浮动加上左右两

    相关 布局布局

    圣杯布局和双飞翼布局,虽然两者的实现方法略有差异,不过都遵循了以下要点: 两侧宽度固定,中间宽度自适应 中间部分在 DOM 结构上优先,以便先行渲染 允许三

    相关 布局布局

    圣杯布局 圣杯布局就是三栏布局,其中左右两栏固定宽度,中间部分自适应 主要步骤: 1. 在html中,中间的块在最前面,后面紧跟左边的块和右边的块 2. 三者均