css中间自适应布局的5种解法详解
前端  /  管理员 发布于 7年前   183
前言
在做页面时,我们往往会碰到页面布局相关的内容,面试时也经常会被问到,那么今天我就来总结一下关于页面布局的内容。
问题:如何实现三栏布局(高度固定,左中右的结构)
假设高度已知,请写出三栏布局,其中左右宽度均为300px,中间自适应。
看了上面的题目,有经验的人也许会觉得很简单,仔细想想,如果我们来写,能写出几种方案呢?一般都会想到两种吧,float和position定位,其实除了这两种外,还有3种可以写,下面我就来一一介绍一下:
方案一(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>
方案二(绝对定位)
<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>
方案三(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>
方案四(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>
方案五(网格布局)
<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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号