display:设置元素的显示方式
display:block(块级元素)
默认为父元素宽高,可设置宽高
相对前序换行显示,其后续也为换行显示
默认display:block元素 --div, p, h1-h6, form...
应用:水平居中
margin:0 auto;
display:inline
默认宽度为内容宽度,不可设置宽高
inline元素之间为同行显示,元素内部可换行
默认display:inline元素 -- span, a, label, cite, em,...
display:inline-block
默认为内容宽高,可设置宽高
同行显示
inline-block元素如果超过边界将整行换行
默认display:inline-block元素 -- input, textarea, select, button,...
应用:居中导航
text-align:center;
a{display:inline-block;
width:**px;
height: **px;
display:none
设置元素不显示
visibility:hidden ---只是隐藏看不到但元素依然占据位置
position:设置定位方式
top,right,bottom,left,z-index--设置位置
(x,y)的定位方式要考虑元素的宽高等
.sample{
top: 30px;
left: 30px;
bottom: 30px;
right:30px;
}
同时设置top、bottom,使得元素撑开
z-index:设置元素在z轴上的位置,存在栈的概念
在未设置位置时按照文档流排序
position:static 静态
position:relative
主要应用于作为绝对定位元素的参照物
元素仍在文档流中(即按照文档流的顺序进行排列)
参照物为元素本身
.sample{
position:relative;
top:20px; //改变了元素z轴上的层级
left:-30px;
}
position: absolute
默认宽度为内容宽度,脱离文档流(相当于上浮)
参照物是第一个定位祖先/根元素
轮播头图
.is{
position:relative;
}
.is.title{
position: absolute;
}
.is.controls{
position:absolute;
}
position:fixed
默认为内容宽度,元素脱离文档流,参照物为视窗,不随滚动条滚动而滚动
固定顶栏布局
padding-top: **px;
}
.top{
position: fixed;
}
遮罩布局
.mask{
position:fixed;
top:0;
widht:%100;
z-index:999;
}
三行自适应布局
.head{
position:absoulte;
}
.body{
position:absolute;
top:100px;
bottom:100px;
right:0;
}
.foot{
position:absolute;
bottom: 0;
left: 0;
width:%100;
height:**px;
}
float
默认宽度为内容宽度,向指定方向一直移动,float的元素在同一文档流中,半脱离文档流
对后续元素,脱离文档流;对后续内容,在文档流
float:right
clear :both| left| right| none| inherit
应用于浮动元素的后续元素清除浮动影响
应用于块级元素
清除浮动:
- 增加空白元素(不常用)
- clearfix
.clearfix:after{
content: ".";
display: block;
clear: both;
height: 0;
overflow: hidden;
visibility: hidden;
}
<div class="sb clearfix">
</div>
- 用空白元素
.cb{
clear: both;
height: 0;
overflow: hidden;
visibility: hidden;
}
<div>
<br class="cb">
</div>
- 两列布局
<div class="body clearfix">
<div class="side">side</div>
<div class="main">main</div>
</div>
.side{
float: right;
}
.main{
float: left;
}
.clearfix: after{
content: ".";
display: block;
height: 0;
overflow: hidden;
visibility: hidden;
}
flex(弹性布局)
创建flex container
display:flex;
flex item(弹性元素)
在弹性容器文档流中的子元素
float是在文档流中的元素
flex-direction:row | row-reverse | column | column-reverese
设置排列方向
flex-wrap: nowrap | wrap | wrap-reverse |
设置换行方式,默认为不换行
flex-flow: <'flex-direction'> || <'flex-wrap'>
order
初始值为0,是一个相对的值,按照从小到大方式排列
弹性
flex-basis
flex-basis: main-size | <width>
设置flex item的初始宽 / 高
flex-grow
flex-grow: <number>
inital: 0
flex-grow设置元素所能分配到的空余空间的比例
弹性布局的元素width = flex-basis + flex-grow/sum(flex-grow) * remain
flex-shrink
flex-shrink :<number>
inital: 1
flex -shrink 设置的元素把负的空间按照一定比例平摊
width = flex-basis + flex-shrink/sum(flex-shrink) *remain
flex
flex: <'flex-grow> || <'flex-shrink'> || <'flex-basis'>
inital: 0 1 main-size
对齐
justify-content
justify-content: flex-start | flex-end | center | space-between | space-around
align-items
align-items: flex-strat | flex-end | center|baseline | stretch
类似vertical-align
设置在容器上
align-self
align-self: auto | flex-start | flex-end | center | baseline | stretch
设置单个flex item在cross-axis方向上对齐方式
align-content
align-content: flex-start | flex-end | center | space-between | space-around | stretch
设置cross-axis方向行对齐方式,可应用于多行
三行两列自适应布局
body{
display: flex;
flex-flow:: column;
}
.head, .foot{
height: 100px;
}
.body{
flex:1;
display: flex;
}
side{
width: 200px;
}
.main{
flex:1;
}
<div class="head">head </body>
<div class="body">
<div class="side">side</div
<div class="main">main</div>
</div>
<div class="foot">foot</div>
水平居中
-
inline-block + text-align
.child{
display: inline-block;
}
.parent{
text-align: center;
}
<div class="parent">
<div class="child"> DEMO</div>
</div>
兼容性良好,甚至可以兼容IE6、7,但会使child内部文字也为center,需要额外代码修复text-align带来的问题
-
tabel + margin
.child{
display: table;
margin: 0 auto;
}
<div class="parent">
<div class="child"> DEMO</div>
</div>
只对child设置不用管parent
-
absolute + transform
.parent{
position: relative;
}
.child{
position: absolute;
left: 50%;
transform: translate(-50%);
}
<div class="parent">
<div class="child"> DEMO</div>
</div>
child元素不会对其他元素产生影响,但是对IE6、7、8兼容性不好
-
flex +justify-content
flex-item在默认情况下宽度是内容宽度
.parent{
display: flex;
justify-content: center;
}
<div class="parent">
<div class="child"> DEMO</div>
</div>
或者为
.parent{
display: flex;
}
.child{
margin: 0 auto;
}
flex在低版本浏览器中不支持
垂直居中
父子容器高度不定
-
table-cell +vertical-align
.parent{
display: table-cell;
vertical-align: middle;
}
<div class="parent">
<div class="child"> DEMO</div>
</div>
兼容性好
-
absolute + transform
.parent{
position: relative;
}
.child{
position: absolute;
top: 50%;
}
<div class="parent">
<div class="child"> DEMO</div>
</div>
兼容性不好。可能要给不同的浏览器加上不同的私有前缀
-
flex + align-items
.parent{
display: flex;
align-items: center;
}
<div class="parent">
<div class="child"> DEMO</div>
</div>
只有高版本浏览器可以用
居中
父子容器高度未知
-
inline-block + text-align+ table-cell +vertical-align
.parent{
text-align: center;
display: table-cell;
vertical-align: middle;
}
.child{
display: inline-block;
}
<div class="parent">
<div class="child"> DEMO</div>
</div>
兼容性高,将table-cell换成table结构,inline-block不支持可以换成display-inline + zoom:1
-
absolute+ transform
.parent{
position: relative;
}
.child{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
<div class="parent">
<div class="child"> DEMO</div>
</div>
child不会对其他元素产生影响,但存在兼容问题
- flex+ justify-content + align-items
.parent{
display: felx;
justify-content: center;
align-items: center;
}
兼容性存在问题
解决方案的思路
- 了解css元素属性、值、特性
- 分析需求,分解问题
多列布局
定宽 + 自适应
-
float + margin
.left{
float: left;
width: 100px;
}
.right{
margin-left: 120px;
}
<div class="parent">
<div class="left">
<p> left</p>
</div>
<div class="right">
<p></p>
</div>
</div>
在IE6中会产生3像素的bug
-
float+ margin + (fix)
.left{
float: left;
width: 100px;
position: relative;
}
.right-fix{
float: right;
width: 100%;
margin-left: -100%;
}
.right{
margin-left: 120px;
}
<div class="parent">
<div class="left">
<p> left</p>
</div>
<div class="right-fix">
<div class="right">
<p></p>
</div>
</div>
</div>
不会有三像素问题,兼容性好,但结构多
-
float + overflow
.left{
float: left;
width: 100px;
margin-right: 20px;
}
.right{
overflow: hidden;
}
<div class="parent">
<div class="left">
<p> left</p>
</div>
<div class="right">
<p></p>
</div>
</div>
-
table
.parent{
display: table;
width: 100%;
table-layout: fixed;
}
.left, .right{
display: table-cell;
}
.left{
width: 100px;
padding-right: 20px;
}
<div class="parent">
<div class="left">
<p> left</p>
</div>
<div class="right">
<p></p>
</div>
</div>
table-layout可以加速渲染
-
flex
.parent{
display: flex;
}
.left{
width:100px;
margin-right: 20px;
}
.right{
flex: 1;
}
<div class="parent">
<div class="left">
<p> left</p>
</div>
<div class="right">
<p></p>
</div>
</div>
性能可能存在问题,flex多应用于小范围布局
两列定宽一列自适应
.left,.center{
flaot: left;
width: 100px;
margin-right: 20px;
}
.right{
overflow: hidden;
}
<div class="parent">
<div class="left">
<p> left</p>
</div>
<div class="center">center
</div>
<div class="right">
<p></p>
</div>
</div>
等分布局
.parent{
margin-left:-20px;
}
.column{
float:left;
width: 25%;
padding-left: 20px;
box-sizing: border-box;
}
<div class="parent">
<div class="column"> 1</div>
<div class="column"> 1</div>
<div class="column"> 1</div>
<div class="column"> 1</div>
</div>
兼容性比较好,但是当列数发生变化是要修改width
-
table
.parent-fix{
margin-left: -20px;
}
.parent{
display: table;
width: 100%;
table-layout: fixed;
}
.column{
display: table-cell;
padding-left: 20px;
}
<div class="parent-fix">
<div class="parent">
<div class="column"> 1</div>
<div class="column"> 1</div>
<div class="column"> 1</div>
<div class="column"> 1</div>
</div>
</div>
table-layout布局优先加速渲染加速布局,当table-layout:flex,单元格没有设置宽度,单元格就是平分的
-
flex
display: flex;
}
.column{
flex: 1;
}
.column+.cloumn{
margin-left:20px;
}
<div class="parent">
<div class="column"> 1</div>
<div class="column"> 1</div>
<div class="column"> 1</div>
<div class="column"> 1</div>
</div>
等高布局
-
table
.parent{
display: table;
width: 100%;
table-layout: fixed;
}
.left, .right{
display: table-cell;
}
.left{
width: 100px;
padding-right: 20px;
}
<div class="parent">
<div class="left">
<p> left</p>
</div>
<div class="right">
<p></p>
</div>
</div>
-
flex
.parent{
display: flex;
}
.left{
width:100px;
margin-right: 20px;
}
.right{
flex: 1;
}
<div class="parent">
<div class="left">
<p> left</p>
</div>
<div class="right">
<p></p>
</div>
</div>
-
float
.paren{
overflow: hidden;
}
.left, .right{
padding-bottom: 9999px;
margin-bottom:-9999px;
}
.left{
float:left;
width: 100px;
margin-right: 20px;
}
.right{
overflow: hidden;
}
<div class="parent">
<div class="left">
<p> left</p>
</div>
<div class="right">
<p></p>
</div>
</div>
只是伪等高,此处只是把left的背景看起来和right一样高
全屏布局
-
position
html,body,.paernt{
height: 100%;
overfollow: hidden;
}
.top{
position: absolute;
top: 0;
left: 0;
right: 0;
height:100px;
}
.left{
position: absolute;
top: 100px;
left: 0;
width: 200px;
bottom: 50px;
}
.right{
position: absolute;
overflow: auto;
left: 200px;
right: 0;
top: 100px;
bottom: 50px;
}
.bottom{
position: absolute;
left: 0;
right: 0;
bottom: 0;
height: 50px;
}
.right.inner{
min-height: 1000px;
}
<div class="parent">
<div class="top"> top </div>
<div class="left "> left</div>
<div class="right">
<div class="inner">inner</div>
</div>
<div class="bottom"> bottom</div>
</div>
转换成百分比,将px换为百分比
html,body,.paernt{
height: 100%;
overfollow: hidden;
}
.top{
position: absolute;
top: 0;
left: 0;
right: 0;
height:10%;
}
.left{
position: absolute;
top: 10%;
left: 0;
width: 20%
bottom: 5%;
}
.right{
position: absolute;
overflow: auto;
left: 20%;
right: 0;
top: 10%;
bottom: 5%;
}
.bottom{
position: absolute;
left: 0;
right: 0;
bottom: 0;
height: 5%;
}
.right.inner{
min-height: 1000px;
}
<div class="parent">
<div class="top"> top </div>
<div class="left "> left</div>
<div class="right">
<div class="inner">inner</div>
</div>
<div class="bottom"> bottom</div>
</div>
兼容性:不支持IE6
-
flex
.parent{
height: 100%;
overflow: hidden;
}
.parent{
display: flex;
flex-direction: column;
.top{
height: 100px;
}
.bottom{
height: 50px;
}
.middle{
flex: 1;
display: flex;
}
.left{
width: 20px;
}
.right{
flex: 1;
overflow: auto;
}
.right.inner{
min-height: 1000px;
}
<div class="parent">
<div class="top"> top </div>
<div class="middle ">
<div class="left">left</div>
<div class="right">
<div class="inner">inner</div>
</div>
</div>
<div class="bottom"> bottom</div>
</div>
兼容性:IE9以下都不兼容
全部自适应布局
-
flex
.parent{
height: 100%;
overflow: hidden;
}
.parent{
display: flex;
flex-direction: column;
.top{
}
.bottom{
}
.middle{
flex: 1;
display: flex;
}
.left{
}
.right{
flex: 1;
overflow: auto;
}
.right.inner{
min-height: 1000px;
}
<div class="parent">
<div class="top"> top </div>
<div class="middle ">
<div class="left">left</div>
<div class="right">
<div class="inner">inner</div>
</div>
</div>
<div class="bottom"> bottom</div>
</div>