方法: 給行內元素父元素使用text-align: centercss
<style>
* {
margin: 0;
padding: 0;
}
.box {
border: 1px solid blue;
height: 200px;
text-align: center;
}
.box > span {
background: pink;
}
</style>
<div class="box">
<span>行內元素水平居中</span>
</div>
複製代碼
方法: 塊級元素使用margin: 0 auto。css3
<style>
* {
margin: 0;
padding: 0;
}
.box {
border: 1px solid blue;
height: 200px;
}
.box > p {
width: 200px;
background: pink;
margin: 0 auto;
}
</style>
<div class="box">
<p>塊級元素水平居中</p>
</div>
複製代碼
若子元素包含float,爲了讓子元素水平居中,可以讓父元素寬度設置爲fit-content,而且配合margin。bash
<style>
* {
margin: 0;
padding: 0;
}
.box {
border: 1px solid blue;
height: 200px;
width: fit-content;
margin: 0 auto;
}
.box > p {
float: left;
width: 300px;
background: pink;
}
</style>
<div class="box">
<p>塊級元素水平居中: 子元素浮動</p>
</div>
複製代碼
關於fit-content推薦閱讀理解CSS3 max/min-content及fit-content等width值svg
<style>
* {
margin: 0;
padding: 0;
}
.box {
display: flex;
justify-content: center;
border: 1px solid blue;
height: 200px;
}
.box > p {
width: 300px;
background: pink;
}
</style>
<div class="box">
<p>flex + justify-content: center</p>
</div>
複製代碼
<style>
* {
margin: 0;
padding: 0;
}
.box {
border: 1px solid blue;
height: 200px;
}
.box > p {
position: absolute;
left: 50%;
background: pink;
transform: translate(-50%, 0);
}
</style>
<div class="box">
<p>寬度未知: absolute + transform</p>
</div>
複製代碼
關於transform使用推薦閱讀理解SVG transform座標變換wordpress
<style>
* {
margin: 0;
padding: 0;
}
.box {
border: 1px solid blue;
height: 200px;
}
.box > p {
position: absolute;
left: 50%;
width: 300px;
background: pink;
margin-left: -150px; /*-0.5width*/
}
</style>
<div class="box">
<p>寬度已知: absolute + 負值的margin-left</p>
</div>
複製代碼
<style>
* {
margin: 0;
padding: 0;
}
.box {
border: 1px solid blue;
height: 200px;
}
.box > p {
position: absolute;
left: 0;
right: 0;
width: 300px;
background: pink;
margin: 0 auto;
}
</style>
<div class="box">
<p>寬度已知: absolute + left:0;right:0;margin:0 auto</p>
</div>
複製代碼
若元素是單行文本, 則可設置 line-height 等於父元素高度。flex
<style>
* {
margin: 0;
padding: 0;
}
.box {
border: 1px solid blue;
height: 200px;
}
.box > p {
background: pink;
line-height: 200px;
}
</style>
<div class="box">
<p>已知父元素高度狀況: line-height: height</p>
</div>
複製代碼
<style>
* {
margin: 0;
padding: 0;
}
.box {
border: 1px solid blue;
height: 200px;
}
.box::after {
content: '';
height: 100%;
}
.box > img, .box::after {
display: inline-block;
vertical-align: middle;
width: 100px;
}
</style>
<div class="box">
<img src="./mm.png" alt="">
</div>
複製代碼
<style>
* {
margin: 0;
padding: 0;
}
.box {
position: relative;
height: 200px;
border: 1px solid blue;
}
.box > p {
position: absolute;
top: 50%;
transform: translate(0, -50%);
background: pink;
}
</style>
<div class="box">
<p>absolute + transform</p>
</div>
複製代碼
<style>
* {
margin: 0;
padding: 0;
}
.box {
display: table;
width: 100%;
height: 200px;
border: 1px solid blue;
}
.box > p {
display: table-cell; /* 相似於表格中的單元格, 此時vertical-align: middle才生效 */
vertical-align: middle;
}
</style>
<div class="box">
<p>flex</p>
</div>
複製代碼
<style>
* {
margin: 0;
padding: 0;
}
.box {
position: relative;
height: 200px;
border: 1px solid blue;
}
.box > p {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: pink;
}
</style>
<div class="box">
<p>absolute + transform</p>
</div>
複製代碼
方法: inline-block + text-align + table-cell + vertical-alignui
<style>
* {
margin: 0;
padding: 0;
}
.box {
display: table-cell;
width: 100vw;
height: 200px;
border: 1px solid blue;
text-align: center; /*水平居中*/
vertical-align: middle; /*垂直居中*/
}
.box > span {
}
.box > p {
width: 100px;
display: inline-block; /* 防止塊級元素寬度獨佔一行,內聯元素可不設置 */
}
</style>
<div class="box">
<p>塊級元素: table水平垂直居中</p>
</div>
<div class="box">
<span>行內元素: table水平垂直居中</span>
</div>
複製代碼
方法: 絕對定位方式+四個方向置0 + margin: autospa
<style>
* {
margin: 0;
padding: 0;
}
.box {
position: relative;
height: 300px;
border: 1px solid blue;
}
.box > p {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
width: 100px;
height: 100px;
}
</style>
<div class="box">
<p>絕對定位方式+四個方向置0</p>
</div>
複製代碼