CSS常见布局

13 篇文章 0 订阅

单列布局


水平居中

下面各个实例中实现的是child元素的对齐操作,child元素的父容器是parent元素

inline-block 和 text-align

.parent{text-align: center;}
.child{display: inline-block;}

margin:0 auto

.child{width: 200px; margin: 0 auto;}

table

.parent{position:relative;}
/*或者实用margin-left的负值为盒子宽度的一半也可以实现,不过这样就必须知道盒子的宽度,但兼容性好*/
.child{position:absolute; left:50%; transform:translate(-50%);}

绝对定位

.child{display: table; margin: 0 auto;}

flex

/*第一种方法*/
.parent{display:flex; justify-content:center;}
/*第二种方法*/
.parent{display:flex;}
.child{margin:0 auto;}

垂直居中

vertical-align

/*第一种方法*/
.parent{display:table-cell;vertical-align:middle;height:20px;}
/*第二种方法*/
.parent{display:inline-block;vertical-align:middle;line-height:20px;}

绝对定位

.parent{position:relative;}
.child{positon:absolute; top:50%; transform:translate(0,-50%);}

flex

.parent{display:flex; align-items:center;}

水平垂直全部居中

vertical-align,text-align,inline-block

.parent{display:table-cell; vertical-align:middle; text-align:center;}
.child{display:inline-block;}

绝对定位

.parent{position:relative;}
.child{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);}

flex

.parent{display:flex;justify-content:center;align-items:center;}

多列布局


左列定宽,右列自适应(右列定宽,则左右CSS对调)

float+margin

.left{float:left;width:100px;}
.right{margin-left:100px;}

float+overflow

.left{width:100px;float:left;}
.right{overflow:hidden;}

table

.parent{display:table;table-layout:fixed;width:100%;}
.left{width:100px;}
.right,.left{display:table-cell;}

flex

.parent{display:flex;}
.left{width:100px;}
.right{flex:1;}

两列定宽,一列自适应

基本html结构为父容器为parent,自容器为left,center,right.其中,left,center定宽,right自适应

float+margin

.left,.center{float:left:width:200px;}
.right{margin-left:400px;}

float+overflow

.left,.center{float:left:width:200px;}
.right{overflow:hidden;}

table

.parent{display:table;table-layout:fixed;width:100%;}
.left,.center,.right{display:table-cell;}
.left,.center{width:200px;}

flex

.parent{display:flex;}
.left,.center{width:100px;}
.right{flex:1}

两侧定宽,中栏自适应

基本html结构为父容器为parent,自容器为left,center,right.其中,left,center定宽,right自适应

float+margin

.left{width:100px;float:left;}
.center{float:left;width:100%;margin-right:-200px;}
.right{width:100px;float:right;}}

float+overflow

.parent{width:100%;display:table;table-layout:fixed}
.left,.center,.right{display:table-cell;}
.left{width:100px;}
.right{width:100px;}

table

.parent{width:100%;display:table;table-layout:fixed}
.left,.center,.right{display:table-cell;}
.left{width:100px;}
.right{width:100px;}

flex

.parent{display:flex;}
.left{width:100px;}
.center{flex:1;}
.right{width:100px;}

一列不定宽,一列自适应

基本html结构为父容器为parent,自容器为left,center,right.其中,left,center定宽,right自适应

float+overflow

.left{float:left;}
.right{overflow:hidden;}

table

.parent{display:table;table-layout:fixed;width:100%;}
.left{width:0.1%;}
.left,.right{display:table-cell;}

flex

.parent{display:flex;}
.right{flex:1;}

响应式布局


meta标签

设置布局宽度等于设备宽度,布局viewport等于度量viewport

<meta name="viewport" content="width=device-width,initial-scale=1">

具体语法

@media screen and (max-width:960px){....}
<link rel="stylesheet" media="screen and (max-width:960px)" href='xxx.css' />
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值