码迷,mamicode.com
首页 > 其他好文 > 详细

左边固定,右边自适应常见方式总结

时间:2016-09-19 19:44:06      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

左边固定,右边自适应常见方式总结

html结构如下:

<div class="parent">
    <div class="left">我是左边固定</div>
    <div class="right">我是右边自适应</div>
</div>

 

(1)左边向左浮动并固定宽度,右边给margin-left    (注:右边这个div一定不能给width:100%)

.parent{
    width:100%;
    height:400px;
}
.left{
    float:left;
    width:200px;
    height:100%;
    background:#afa;
}
.right{
    height:100%;
    margin-left:200px;
    background:yellow;
}

 (2)父元素相对定位,左边绝对定位并给固定宽度,右边margin-left   (注:右边这个div一定不能给width:100%)

 

.parent{
    position:relative;
    width:100%;
    height:400px;
}
.left{
    position:absolute;
    left:0;
    width:200px;
    height:100%;
    background:#afa;
}
.right{
    margin-left:200px;
    height:100%;
    background:#aba;
}

 

(3)使用flexbox,父元素display:flex,左边元素固定宽度,右边一定记得加flex:1

.parent{
    width:100%;
    height:400px;
    display:flex;
}
.left{
    width:200px;
    background:#afa;
}
.right{
    flex:1;
    background:yellow;
}

 

左边固定,右边自适应常见方式总结

标签:

原文地址:http://www.cnblogs.com/mujinxinian/p/5886231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!