css实现两栏布局,左侧固定宽,右侧自适应的7种方法,代码如下所示:
1、利用 calc 计算宽度的方法 css代码:
1 2 3 4 | .box>div{ height : 100% ;} #box 1 >div{ float : left ;} .left 1 { width : 100px ; background : yellow;} .right 1 { background : #09c ; width :calc( 100% - 100px );} |
dom结构:
1 2 3 4 | <div class= "box" id= "box1" > <div class= "left1" >左侧定宽</div> <div class= "right1" >右侧自适应</div> </div> |
2、利用 float 配合 margin 实现 css代码:
1 2 3 4 | .box{ overflow : hidden ; height : 100px ; margin : 10px 0 ;} .box>div{ height : 100% ;} .left 2 { float : left ; background : yellow; width : 100px ;} .right 2 { background : #09c ; margin-left : 100px ;} |
dom结构:
1 2 3 4 | <div class= "box" id= "box2" > <div class= "left2" >左侧定宽</div> <div class= "right2" >右侧自适应</div> </div> |
3、利用 float 配合 overflow 实现 css代码:
1 2 3 4 | .box{ overflow : hidden ; height : 100px ; margin : 10px 0 ;} .box>div{ height : 100% ;} .left 3 { float : left ; background : yellow; width : 100px ;} .right 3 { background : #09c ; overflow : hidden ;} |
dom结构:
1 2 3 4 | <div class= "box" id= "box3" > <div class= "left3" >左侧定宽</div> <div class= "right3" >右侧自适应</div> </div> |
4、利用 position:absolute 配合 margin 实现
css代码:
1 2 3 4 5 | .box{ overflow : hidden ; height : 100px ; margin : 10px 0 ;} .box>div{ height : 100% ;} #box 4 { position : relative ;} .left 4 { position : absolute ; left : 0 ; top : 0 ; width : 100px ; background : yellow;} .right 4 { margin-left : 100px ; background : #09c ;} |
dom结构:
1 2 3 4 | <div class= "box" id= "box4" > <div class= "left4" >左侧定宽</div> <div class= "right4" >右侧自适应</div> </div> |
5、利用 position:absolute 实现
css代码:
1 2 3 4 5 | .box{ overflow : hidden ; height : 100px ; margin : 10px 0 ;} .box>div{ height : 100% ;} #box 5 { position : relative ;} .left 5 { position : absolute ; left : 0 ; top : 0 ; width : 100px ; background : yellow;} .right 5 { position : absolute ; left : 100px ; top : 0 ; right : 0 ; width : 100% ; background : #09c ;} |
dom结构:
1 2 3 4 | <div class= "box" id= "box5" > <div class= "left5" >左侧定宽</div> <div class= "right5" >右侧自适应</div> </div> |
6、利用 display: flex 实现
css代码:
1 2 3 4 5 | .box{ overflow : hidden ; height : 100px ; margin : 10px 0 ;} .box>div{ height : 100% ;} #box 6 { display : flex ; display : -webkit- flex ;} .left 6 { flex : 0 1 100px ; background : yellow;} .right 6 { flex : 1 ; background : #09c ;} |
dom结构:
1 2 3 4 | <div class= "box" id= "box6" > <div class= "left6" >左侧定宽</div> <div class= "right6" >右侧自适应</div> </div> |
7、利用 display: table 实现 css代码:
1 2 3 4 5 6 | .box{ overflow : hidden ; height : 100px ; margin : 10px 0 ;} .box>div{ height : 100% ;} #box 7 { display : table ; width : 100% ;} #box 7 >div{ display : table-cell ;} .left 7 { width : 100px ; background : yellow;} .right 7 { background : #09c ;} |
dom结构:
1 2 3 4 | <div class= "box" id= "box7" > <div class= "left7" >左侧定宽</div> <div class= "right7" >右侧自适应</div> </div> |
到此这篇关于css实现两栏布局,左侧固定宽,右侧自适应的7中方法的文章就介绍到这了,更多相关css两栏布局内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章,希望大家以后多多支持IT俱乐部!