CSS三栏布局的方法有哪些

发布时间:2021-10-15 14:05:06 作者:小新
来源:亿速云 阅读:263

这篇文章将为大家详细讲解有关CSS三栏布局的方法有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

题目:假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px,中间自适应.

三栏布局的5种方案

这是一道经典的面试题,下面记录了css布局的5种方法。

<!DOCTYPE html> <html> <head>   <meta charset="UTF-8">   <meta name="viewport" content="width=device-width, initial-scale=1.0">   <meta http-equiv="X-UA-Compatible" content="ie=edge">   <title>三栏布局</title>   <style>     * {       margin: 0;       padding: 0;     }   </style> </head> <body> <!--5种解决方案--> </body>

1. 浮动解决方案

  <!-- 1\. 浮动解决方案 -->   <scetion class="layout float">     <!-- 样式 -->     <style media="screen">       .layout.float article div {         min-height: 80px;       }       .layout.float .left {         width: 300px;         background-color: red;         float: left;       }       .layout.float .center {         background-color: yellow;       }       .layout.float .right {         width: 300px;         background-color: blue;         float: right;       }     </style>     <!-- 结构 -->     <!-- 注意:结构中 右浮动的div一定要写在 center 的前面,否则无效. -->     <h3>三栏布局</h3>     <article>       <div></div>       <div></div>       <div>         <h3>浮动解决方案</h3>         1.这是三栏布局的浮动解决方案;         2.这是三栏布局的浮动解决方案;         3.这是三栏布局的浮动解决方案;         4.这是三栏布局的浮动解决方案;         5.这是三栏布局的浮动解决方案;         6.这是三栏布局的浮动解决方案;       </div>     </article>   </scetion>

2. 绝对定位解决方案

  <!-- 2\. 绝对定位解决方案 -->   <section class="layout absolute">     <!-- 样式 -->     <style>       .layout.absolute article div {         min-height: 80px;         position: absolute;       }       .layout.absolute .left {         width: 300px;         background-color: red;         left: 0;       }       .layout.absolute .center {         background-color: yellow;         left: 300px;         right: 300px;       }       .layout.absolute .right {         width: 300px;         background-color: blue;         right: 0;       }     </style>     <!-- 结构 -->     <h2>三栏布局</h2>     <article>       <div></div>       <div>         <h3>绝对定位解决方案</h3>         1.这是三栏布局的绝对定位解决方案;         2.这是三栏布局的绝对定位解决方案;         3.这是三栏布局的绝对定位解决方案;         4.这是三栏布局的绝对定位解决方案;         5.这是三栏布局的绝对定位解决方案;         6.这是三栏布局的绝对定位解决方案;       </div>       <div></div>     </article>   </section>

3. flexbox 解决方案

  <!-- 3\. flexbox解决方案 -->   <section class="layout flexbox">     <!-- 样式 -->     <style>       /* flexbox解决方案在浏览器中显示时被上面的绝对定位解决方案挡住了,设置一个margin-top */       .layout.flexbox {         margin-top: 110px;       }       /* 设置最低高度 */       .layout.flexbox article div {         min-height: 80px;       }       .layout.flexbox article {         display: flex;       }       .layout.flexbox .left {         width: 300px;         background-color: red;       }       .layout.flexbox .center {         /*center部分增长 使整行填充*/         flex: 1;         background-color: yellow;       }       .layout.flexbox .right {         width: 300px;         background-color: blue;       }     </style>     <!-- 结构 -->     <h2>三栏布局</h2>     <article>       <div></div>       <div>         <h3>flexbox解决方案</h3>         1.这是三栏布局的flexbox解决方案;         2.这是三栏布局的flexbox解决方案;         3.这是三栏布局的flexbox解决方案;         4.这是三栏布局的flexbox解决方案;         5.这是三栏布局的flexbox解决方案;         6.这是三栏布局的flexbox解决方案;       </div>       <div></div>     </article>   </section>

4. 表格布局解决方案

  <!-- 4\. 表格布局解决方案 -->   <section class="layout table">     <!-- 样式 -->     <style>       .layout.table .left-center-right {         width: 100%;         min-height: 80px;         display: table;       }       .layout.table .left-center-right>div {         display: table-cell;       }       .layout.table .left {         width: 300px;         background-color: red;       }       .layout.table .center {         background-color: yellow;       }       .layout.table .right {         width: 300px;         background-color: blue;       }     </style>     <!-- 结构 -->     <h2>三栏布局</h2>     <article>       <div></div>       <div>         <h3>表格布局解决方案</h3>         1.这是三栏布局的表格布局解决方案;         2.这是三栏布局的表格布局解决方案;         3.这是三栏布局的表格布局解决方案;         4.这是三栏布局的表格布局解决方案;         5.这是三栏布局的表格布局解决方案;         6.这是三栏布局的表格布局解决方案;       </div>       <div></div>     </article>   </section>

5. 网格布局解决方案

  <!-- 5\. 网格布局解决方案 -->   <section class="layout grid">     <!-- 样式 -->     <style>       .layout.grid .left-center-right {         width: 100%;         display: grid;         /* 网格行高 */         grid-template-rows: 100px;         /* 网格列宽 */         grid-template-columns: 300px auto 300px;       }       .layout.grid .left {         background-color: red;       }       .layout.grid .center {         background-color: yellow;       }       .layout.grid .right {         background-color: blue;       }     </style>     <!-- 结构 -->     <h2>三栏布局</h2>     <article>       <div></div>       <div>         <h3>网格布局解决方案</h3>         1.这是三栏布局的网格布局解决方案;         2.这是三栏布局的网格布局解决方案;         3.这是三栏布局的网格布局解决方案;         4.这是三栏布局的网格布局解决方案;         5.这是三栏布局的网格布局解决方案;         6.这是三栏布局的网格布局解决方案;       </div>       <div></div>     </article>   </section>

将浏览器窗口压窄,可以看到变化。由于上面的代码中设置的高度是min-width,而不是设置的固定高度width,所以现在看到的也就是高度不固定的情况。

5种布局方案在高度不固定的情况下呈现出不同的效果的分析

这是因为在flex布局中,align-items属性默认为stretch,如果设置为:align-items: center;或align-items: start;或align-items: end;或其他值,那么就不会自动保持一样高。

关于“CSS三栏布局的方法有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. css实现等高布局的方法有哪些
  2. CSS实现三列布局方法有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css

上一篇:HTML中DOM节点的示例分析

下一篇:jQuery面试题有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》