css中圣杯布局和双飞翼布局的介绍(附代码)

这篇文章给大家介绍的内容是关于css中圣杯布局双飞翼布局的介绍(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

圣杯布局

659018467-5b61ac73ed6b0_articlex.png

  
#center
#left

登录后复制

实现的效果主要在container中,left 和 rgith固定宽度,center首先渲染,且自适应宽度。

    body {      min-width: 500px;    }    #container {      overflow: auto;        /* BFC */      padding-left: 180px;      padding-right: 150px;    }    #container .column {      height: 200px;      position: relative;      float: left;    }    #center {      background-color: #e9e9e9;      width: 100%;    }    #left {      background-color: red;      width: 180px;      right: 180px;       margin-left: -100%    }    #right {      background-color: blue;      width: 150px;       margin-right: -150px;    }    #header,     #footer {      background-color: #c9c9c9;    }

登录后复制

该方案几个注意的点:

center元素位于left和right之前,可以让center先渲染,用户首先看到页面的主要内容。

立即学习“前端免费学习笔记(深入)”;

container (width:100%)包裹着三栏内容,通过padding-left和padding-right为左右两栏腾出空间。

center,left,right都设置一个左浮动(float:left),所以container内部是一个浮动流

通过给 left 元素设置 margin-left: -100%,使得left移动到container的左上角,在通过position:relative; right: 180px,移动到container的padding-left的位置上去。

给right 元素设置 margin-right: -150px,使得它移动到container的padding-right的位置上去。

ps: margin-left 和 margin-right 利用了浮动流的特性,使得第一行能够同时容纳center,left,right这三个元素。

圣杯布局(flexbox实现)

474238071-5b61ac741c53f_articlex.png

    
#center
#left

登录后复制

    body {      min-width: 550px;      }    #HolyGrail {      display: flex;      min-height: 100vh;      flex-direction: column;    }    #container {      display: flex;      flex: 1;    }    #center {      background-color: #e9e9e9;      flex: 1;    }    #left {      background-color: red;      order: -1;      width: 150px;    }    #right {      background-color: blue;      width: 150px;    }    #header,     #footer {      height: 50px;      background-color: #c9c9c9;    }

登录后复制

如果不考虑ie10及以下的浏览器,那么可以使用flex来实现圣杯布局。而且圣杯布局可以通过让container填充高度来使得footer达到一个sticky的效果。
flex兼容性

双飞翼布局

659018467-5b61ac73ed6b0_articlex.png

圣杯布局和双飞翼布局解决的问题是一样的,就是两边定宽,中间自适应的三栏布局,中间栏要在放在文档流前面以优先渲染。圣杯布局和双飞翼布局解决问题的方案在前一半是相同的,也就是三栏全部float浮动,但左右两栏加上负margin让其跟中间栏p并排,以形成三栏布局。不同的地方在于解决中间p内容不被遮挡的思路上面

圣杯布局的为了中间内容不被修改,是通过包裹元素的padding-left和padding-right来使得内容p置于中间,然后再通过相对定位position:relative,配合right或left属性让左右两栏不则当中间内容。

双飞翼布局的解决方案是:通过再中间元素的内部新增一个p用于放置内容,然后通过左右外边距margin-left和margin-right为左右两栏留出位置。

双飞翼布局多了1个p标签,少用了4个css属性。少用了padding-left,padding-right,左右两个p用相对布局position: relative及对应的right和left,多了margin-left,margin-right。

  
#center
#left

登录后复制

    body {      min-width: 500px;      }    #container {      overflow: auto;        /* BFC */    }    #container .column {      height: 200px;      float: left;    }    #center {      background-color: #e9e9e9;      width: 100%;    }    #center-content {      margin-left: 180px;      margin-right: 150px;    }     #left {      width: 180px;      background-color: red;      margin-left: -100%;    }    #right {      background-color: blue;      width: 150px;        margin-left: -150px;     }    #header,     #footer {      background-color: #c9c9c9;    }

登录后复制

相关文章推荐:

css中如何实现浮动的元素换行

css中如何实现浮动的元素换行

以上就是css中圣杯布局和双飞翼布局的介绍(附代码)的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2894611.html

(0)
上一篇 2025年3月10日 22:50:06
下一篇 2025年3月8日 08:29:30

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论