css如何实现三栏布局

互联网 20-4-21

实现方法:

一、float浮动

<section class='layout float'>          <style>              .layout.float .left-right-center{                  height: 100px;              }              .layout.float .left{                  float: left;                  width: 300px;                  background-color: red;              }               .layout.float .right{                  float: right;                  width: 300px;                  background-color: blue;              }               .layout.float .center{                  background-color: yellow;              }          </style>          <article class="left-right-center">              <div class="left"></div>              <div class="right"></div>              <div class="center">我是中间的自适应元素--浮动</div>          </article>      </section>

原理:左右两个div由于浮动脱离了文档流,center就会上移,造成三栏布局的效果(前提是高度相同)

优点:兼容性高

缺点:需要清除浮动来防止影响其他元素

如果高度不固定,中间的内容会被撑开,左右两边不会一起撑开

(推荐教程:CSS教程)

二、绝对定位

<section class="layout absolute">          <style>              .layout.absolute .left-center-right div{                  position: absolute;                  height: 100px;              }               .layout.absolute .left{                  left: 0;                  width: 300px;                  background-color: red;              }               .layout.absolute .center{                  left: 300px;                  right: 300px;                  background-color: yellow;              }               .layout.absolute .right{                  right: 0;                  width: 300px;                  background-color: blue;              }          </style>          <article class="left-center-right">             <div class="left"></div>             <div class="center">                 我是中间的自适应元素--绝对定位             </div>             <div class="right"></div>          </article>      </section>

原理:利用绝对定位以及宽度,将左右两边的div固定住,中间div的宽度就会有自适应的效果

优点:快捷

缺点:如果父元素脱离了文档流,子元素一定会脱离文档流,运用的场景不多

如果中间元素的高度增加,两边元素的高度不会增加,所以只有中间的div会撑开

三、flex布局

<section class="layout flex">          <style>              .layout.flex .left-center-right{                  display: flex;                  height: 100px;              }               .layout.flex .left{                  width: 300px;                  background-color: red;              }               .layout.flex .center{                  flex: 1;                  background-color: yellow;              }               .layout.flex .right{                  width: 300px;                  background-color: blue;              }          </style>          <article class="left-center-right">             <div class="left"></div>             <div class="center">                 我是中间的自适应元素--flex布局             </div>             <div class="right"></div>          </article>      </section>

原理:将父元素设置为flex布局,然后中间元素设置flex为1,达到自适应的效果

优点:在实际开发中常用

缺点:IE8及以下的浏览器不支持

如果高度不固定,中间内容的高度撑开后,两边也会随之撑开

四、table布局

   <section class="layout table">         <style>             .layout.table .left-center-right{                 display: table;                 height: 100px;                 width: 100%;             }              .layout.table .left{                 display: table-cell;                 width: 300px;                 background-color: red;             }              .layout.table .center{                 display: table-cell;                 background-color: yellow;             }              .layout.table .right{                 display: table-cell;                 width: 300px;                 background-color: blue;             }         </style>         <article class="left-center-right">             <div class="left"></div>             <div class="center">                 我是中间的自适应元素--table             </div>             <div class="right"></div>         </article>     </section>

原理:将父元素设置为table布局,然后每个子元素都是teble-cell,给左右两个格子设置固定的宽度,中间的格子就可以达到自适应的效果

优点:兼容性好,可做flex布局在ie8以下的代替

缺点:局限性

如果高度不固定,中间被撑开时,左右两边也会被撑开,和flex类似

五、网格布局

<section class="layout grid">         <style>             .layout.grid .left-center-right{                 display: grid;                 width: 100%;                 grid-template-rows: 100px;/*每一格都是100px高*/                 grid-template-columns: 300px auto 300px;/*左右两格都是300px,中间是自适应*/             }                          .layout.grid .left{                 background-color: red;             }              .layout.grid .center{                 background-color: yellow;             }              .layout.grid .right{                 background-color: blue;             }         </style>         <article class="left-center-right">             <div class="left"></div>             <div class="center">                 我是中间的自适应元素--grid布局             </div>             <div class="right"></div>         </article>     </section>

原理:将父元素设置为网格布局,然后规定每格的高度以及每格的宽度,只用分别给每格单独设置颜色即可

优点:技术比较新,方便

缺点:兼容性不是很好

如果高度不固定,中间元素添加文本,也不会撑开

相关视频教程推荐:css视频教程

以上就是css如何实现三栏布局的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
标签: css
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:css实现文本图标对齐的方法

相关资讯