background: #eee;
width: 300px;
height: 300px;
position: relative;
}
.block {
background: skyblue;
width: 100px;
height: 100px;
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
}
.inline {
background: pink;
position: absolute;
width: 100px;
left: 0;
right: 0;
margin: 0 auto;
}
===============================================================
父元素 height: 高度;
行内元素 line-height: 高度;
.parent {
background: #eee;
width: 300px;
height: 300px;
}
.inline {
background: pink;
line-height: 300px;
}
父元素 display: table-cell;
子元素 vertical-align: middle;
.parent {
background: #eee;
width: 300px;
height: 300px;
display: table-cell;
vertical-align: middle;
}
display: flex;
align-items: center;
.parent {
background: #eee;
width: 300px;
height: 300px;
display: flex;
align-items: center;
}
.block {
background: skyblue;
width: 100px;
height: 100px;
}
.inline {
background: pink;
}
position: absolute;
top: 50%;
height: 高度;
margin-top: -0.5高度;
.parent {
background: #eee;
width: 300px;
height: 300px;
position: relative;
}
.block {
background: skyblue;
width: 100px;
height: 100px;
position: absolute;
top: 50%;
margin-top: -50px;
}
.inline {
background: pink;
height: 100px;
position: absolute;
top: 50%;
margin-top: -50px;
}
position: absolute;
top: 50%;
transform: translate( 0, -50%);
.parent {
background: #eee;
width: 300px;
height: 300px;
position: relative;
}
.block {
background: skyblue;
width: 100px;
height: 100px;
position: absolute;
top: 50%;
transform: translate( 0, -50%);
}
.inline {
background: pink;
height: 100px;
position: absolute;
top: 50%;
transform: translate( 0, -50%);
}
position: absolute;
height: 高度;
top: 0;
bottom: 0;
margin: auto 0;
.parent {
background: #eee;
width: 300px;
height: 300px;
position: relative;
}
.block {
background: skyblue;
width: 100px;
height: 100px;
position: absolute;
top: 0;
bottom: 0;
margin: auto 0;
}
.inline {
background: pink;
position: absolute;
height: 100px;
top: 0;
bottom: 0;
margin: auto 0;
}
===================================================================
绝对定位 transform
position:absolute;
top:50%;
left:50%;
transform:translate(-50%,-50%)
.parent {
background: #eee;
width: 300px;
height: 300px;
position: relative;
}
.block {
background: skyblue;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50% ,-50%);
}
绝对定位 margin
position:absolute;
top:50%;
left:50%;
margin-top:-(height/2)px;
margin-left:-(width/2)px;
.parent {
background: #eee;
width: 300px;
height: 300px;
position: relative;
}
.block {
background: skyblue;
width: 100px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
}
绝对定位 margin:auto
position:absolute/fixed;
top:0;left:0;right:0;bottom:0;
margin:atuo;
.parent {
background: #eee;
width: 300px;
height: 300px;
position: relative;
}
.block {
background: skyblue;
width: 100px;
height: 100px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
绝对定位 calc 水平、垂直居中也可以使用此方法
position:absolute;
top:calc(50% - (height/2)px);
left:calc(50% - (width/2)px);
.parent {
background: #eee;
width: 300px;
height: 300px;
position: relative;
}
.block {
background: skyblue;
position: absolute;
width: 100px;
height: 100px;
top:calc(50% - 50px);
left:calc(50% - 50px);
}
calc() 函数
calc() 函数用于动态计算长度值
。
需要注意的是,运算符前后都需要保留一个空格
,例如:width: calc(100% - 10px);
任何长度值
都可以使用calc()函数进行计算;
calc()函数支持"+", "-", "*", "/"
运算;
calc()函数使用标准的数学运算优先级规则
;
display: flex;
justify-content: center;
align-items: center;
.parent {
background: #eee;
width: 300px;
height: 300px;
display: flex;
justify-content: center;
align-items: center;
}
display: table-cell;
vertical-align: middle;
text-align: center;
如果是块级元素 需要 display: inline-block;
background: #eee;
width: 300px;
height: 300px;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.block {
background: skyblue;
width: 100px;
height: 100px;
display: inline-block;
}
.inline {
background: pink;
}
=================================================================
height:高度
line-height:高度
.parent {
background: #eee;
width: 300px;
height: 300px;
text-align: center;
line-height: 300px;
}
=================================================================
在图片前或者后定义一个span元素
width: 0; height: 100%; display: inline-block; vertical-align: middle;
给图片img添加vertical-algin:middle
中线对齐