css布局
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了css布局,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3539字,纯文字阅读大概需要6分钟。
内容图文
![css布局](/upload/InfoBanner/zyjiaocheng/1318/caa035fb60184f7aa5eb08ad1ae77200.jpg)
css布局主要是position,float以及margin等属性实现
本文主要介绍几种常见的布局:三列布局,等高布局以及水平垂直居中布局。
1 三列布局
1.1 左右列固定宽度,中间列宽度自适应
方法1:absolute+margin
1 < div id ="left" >left</div>2<div id="center">center</div>3<div id="right">right</div>
1 #left,#right{ 2 width:200px; 3 postion:absolute; 4 top:0; /*注意不能少*/ 5 background-color:red; 6 } 7#left{left:0;} 8#right{right:0;} 910#center{ 11 position:relative; 12 margin:0 210px; 13 background-color:green; 14 }
缺点:当中间列设置最小宽度限制时,当页面小于一定宽度,三列元素会发生层叠
方法2:float+margin
1 < div id ="left" >left</div>2<div id="right">right</div>3<div id="center">center</div>
1 #left,#right{ 2 float:left; 3 width:200px; 4 background-color:red; 5 } 6#right{float:right;} 7 8#center{ 9 float:left; 10 margin:0 210px; 11 background-color:green; 12 }
方法3:负margin
margin-left为负时,该元素向之前的元素铺盖;margin-right为负时,该元素将其他元素拽向自己
1 < div id ="center" > 2 < div id ="centerContent" >center</div>3</div>4<div id="left">left</div>5<div id="right">right</div>
1 #center{ 2 float:left; 3 width:100%; 4 } 5#centerContent{ 6 margin:0 210px; 7 background-color:green; 8 } 910#left,#right{ 11 float:left; 12 width:200px; 13 background-color:red; 14 } 1516#left{margin-left:-100%;} 17#right{margin-left:-210px}
1.2 左右列宽度自适应,中间列固定宽度
1 < div id ="left" > 2 < div class ="inner" >this is left sidebar content</div> 3</div> 4 5<div id="main"> 6<div class="inner">this is main content</div> 7</div> 8 9<div id="right">10<div class="inner">this is right siderbar content</div>11</div>
1 #left, #right { 2 float: left; 3 margin: 0 0 0 -271px; 4 width: 50%; 5 } 6#main { 7 width: 540px; 8 float: left; 9 background: green; 10 } 111213#left .inner, #right .inner { 14 margin: 0 0 0 271px; 15 background: red; 16 }
2 等高布局
方法1:负margin
方法2:table
1 < div id ="parent" > 2 < div id ="left" >left</div>3<div id="center"> center </div>4<div id="right">right</div>5</div>
1 #parent{ 2 display:table; 3 } 4#left,#center,#right{ 5 display:table-cell; 6 }
方法3:绝对定位absolute
1#parent{ 2 position: relative; 3 height: 40px; 4 } 5#left, # center, # right{ 6 position: absolute; 7 top: 0; 8 bottom: 0; 9 } 10# left{ 11 left: 0; 12 width: 100px; 13 } 14# center{ 15 left: 120px; 16 right: 120px; 17 } 18# right{ 19 width: 100px; 20 right: 0; 21 }
3 水平垂直居中布局
方法1:absolute
大家都知道margin:0 auto可以实现元素的水平居中,那么margin:auto+绝对定位可以实现水平垂直居中。
1 < div id ="parent" > 2 < div id ="center" >center</div>3</div>
1 #center{ 2 position:absolute; 3 margin:auto; 4 top:0; 5 bottom:0; 6 left:0; 7 right:0; 8 } 910#parent{ 11 position:relative; 12 }
方法2:table
table本来就是水平居中,只需设置垂直居中就可以了。
1 #parent{ 2 display:table; 3 } 4#center{ 5 display:table-cell; 6 vertical-align:middle; 7 }
参考资料:
原文:http://www.cnblogs.com/wan2/p/6395295.html
内容总结
以上是互联网集市为您收集整理的css布局全部内容,希望文章能够帮你解决css布局所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。