对于前端来讲,布局也是必须掌握的,一个好的布局可让页面看起来更美观。提到布局,那就不得不说CSS三栏布局。这是前端面试常常会问到的一个问题,算是基础题。所谓的三栏布局,通常是指左右两边固定中间自适应,或者是中间固定左右两边自适应。前端
新建一个父元素,包含三个子元素:left、main、right(注意,main在写在前面,这样在页面渲染时会先加载中间,针对面试题优先加载中间部分)面试
style样式设置编程
一、父元素设置高度
二、三个元素均设置浮动
三、中间main部分定宽100%:width: 100%,左右两边按产品需求设置宽高
四、左边设置margin-left: -100%;右边设置margin-right: -右盒子宽
五、父元素设置padding-left: 左盒子宽;padding-right: 右盒子宽
六、左右盒子相对定位浏览器
<div class="container"> <div class="main f">go aheadgo aheadvgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo ahead</div> <div class="left f"></div> <div class="right f"></div> </div> <style> body { min-width: 700px; } .container { height: 300px; padding: 0 200px 0 200px; } .f { float: left; } .main { width: 100%; height: 300px; background-color: cornflowerblue; } .left { width: 200px; height: 300px; background-color: indianred; margin-left: -100%; position: relative; left: -200px; } .right { width: 200px; height: 300px; background-color: lightgreen; margin-left: -200px; position: relative; right: -200px; } </style>
该布局受内部元素影响而破坏布局的几率低,可是当浏览器屏幕缩小的必定程度时,左右两侧的内容会掉下来,或发生重叠现象。解决方案,给body加一个最小宽度(起码大于左右两侧宽度之和)布局
与圣杯布局的思路是一致的,只是有一些细微的差异。学习
新建一个父元素,包含三个子元素:left、main、right(注意,main在写在前面,这样在页面渲染时会先加载中间,针对面试题优先加载中间部分)flex
style样式设置code
一、父元素设置高度
二、三个元素均设置浮动
三、中间main部分定宽100%:width: 100%,左右两边按产品需求设置宽高
四、中间main部分再加一个盒子inner,放置内容(与圣杯布局的不一样点)
五、左边设置margin-left: -100%;右边设置margin-right: -右盒子宽
六、新添加盒子,inner,设置左右padding或margin对象
<div class="container"> <div class="main f"> <div class=inner>go aheadgo aheadvgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo ahead</div> </div> <div class="left f"></div> <div class="right f"></div> </div> <style> .container { height: 300px; } .f { float: left; } .main { width: 100%; height: 300px; background-color: cornflowerblue; } .left { width: 200px; height: 300px; background-color: indianred; margin-left: -100%; } .right { width: 200px; height: 300px; background-color: lightgreen; margin-left: -200px; } .inner { padding: 0 200px 0 200px; } </style>
新建三个元素:left、right、main(注意,main写在后面)产品
style样式设置
一、左盒子左浮动,右盒子右浮动
二、中间部分设置margin或padding值
<div class="left"></div> <div class="right"></div> <div class="main">我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容</div> <style> .main { margin: 0 200px 0 200px; background-color: red; height: 200px; } .left { float: left; width: 200px; background-color: blue; height: 200px; } .right { float: right; width: 200px; background-color: pink; height: 200px; } </style>
新建一个父元素,包含三个子元素:left、main、right(注意,main写在中间)
style样式设置
一、父元素设置宽度为100%,display: flex;
二、左右两则按产品需求设置宽高
三、中间部分设置flex: 1;
<div class="container"> <div class="left"></div> <div class="main">我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容</div> <div class="right"></div> </div> <style> .container { width: 100%; height: 200px; display: flex; } .main { flex: 1; background-color: red; height: 200px; } .left { width: 200px; background-color: blue; height: 200px; } .right { width: 200px; background-color: pink; height: 200px; } </style>
还有其余的写法,这里就不一一赘述,只是列举了一些比较经常使用的,以及面试可能会问到的状况。CSS3还有不少好玩的特性,在工做和学习的过程当中值得深刻研究。
新建一个父元素,包含三个子元素:main、left、right(注意,main写在前面)
style样式设置
一、父元素设置宽度为100%,display: flex;
二、main中间部分设置flex: 1;
三、左右两则按产品需求设置宽高;
四、left设置order: -1;
<div class="container"> <div class="main">我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容</div> <div class="left"></div> <div class="right"></div> </div> <style> .container { width: 100%; height: 200px; display: flex; } .main { flex: 1; background-color: red; height: 200px; } .left { width: 200px; background-color: blue; height: 200px; order: -1; } .right { width: 200px; background-color: pink; height: 200px; } </style>
order属性是调整或设置盒模型对象的子元素出现的順序,数值越小排列越靠前,默认为0。在上面的布局中,left想要排在前面,就须要比0小,因此设置order: -1,left盒子会跑到前面。须要注意的是,若是父元素没有设置flex,则 order 属性不起做用。
感谢【很是记得你】的建议,emmm,编程的解决方法有不少种,不少时候都是靠经验的积累。因此,小伙伴们,坚持会看到胜利的曙光,虽然我也在挣扎中(#^.^#)
新建一个父元素,包含三个子元素:left、main、right(注意,main写在中间)
style样式设置
一、左右两边各占50%的宽度
二、左边负边距 margin-left 占中间div宽度的一半
三、右边负边距 margin-right 也占中间div宽度的一半
<div class="container"> <div class="left"></div> <div class="main">我是中间内容</div> <div class="right"></div> </div> <style> .main { width: 100px; text-align: center; float: left; background-color: lightgreen; height: 300px; } .left { height: 300px; float: left; width: 50%; margin-left: -50px; background-color: pink; } .right { height: 300px; float: right; width: 50%; margin-right: -50px; background-color: cornflowerblue; } </style>
新建一个父元素,包含三个子元素:left、main、right
style样式设置
一、父元素设置display: flex;flex-direction: row;
二、左右设置flex-grow: 1,平分剩余空间
<div class="container"> <div class="left"></div> <div class="main">我是中间内容</div> <div class="right"></div> </div> <style> .container { display: flex; flex-direction : row; } .main { width: 200px; height: 300px; text-align: center; background-color: lightgreen; } .left { height: 300px; flex-grow: 1; background-color: pink; } .right { height: 300px; flex-grow: 1; background-color: cornflowerblue; } </style>
用于动态计算长度值。须要注意的是,运算符先后都须要保留一个空格,例如:width: calc(100% - 50px)。
新建一个父元素,包含三个子元素:left、main、right
style样式设置
一、父元素设置100%宽;
二、左右设置width: calc(50%, - 中间宽/2)
<div class="container"> <div class="left"></div> <div class="main">我是中间内容</div> <div class="right"></div> </div> .container { width: 100%; height: 300px; } .f { float: left; } .main { width: 100px; text-align: center; background-color: lightgreen; height: 300px; } .left { height: 300px; background-color: pink; width: calc(50% - 50px); /*平分中间部分的宽度*/ } .right { height: 300px; background-color: cornflowerblue; width: calc(50% - 50px); /*平分中间部分的宽度*/ }
路漫漫其修远兮,没有别人聪慧,那就坚持不懈努,相信勤能补拙。天天进步一点点,总有一天会迈进一大步。