CSS常见布局

来源:互联网 发布:国内免费空间可帮域名 编辑:程序博客网 时间:2024/06/05 02:05

单列布局


水平居中

下面各个实例中实现的是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' />
原创粉丝点击