元素的水平和垂直居中
水平居中
对于行内级元素,通过设置父级的
text-align属性为center实现水平居中。html<style> .container { text-align: center; } </style> <div class="container">Hello world</div>对于块级元素,通过设置
margin属性为0 auto实现水平居中。html<style> .container { width: 200px; margin: 0 auto; } </style> <div class="container">Hello world</div>对于绝对定位的元素,通过设置
left和right属性为0,再设置margin属性为0 auto实现水平居中。html<style> .container { width: 200px; position: relative; left: 0; right: 0; margin: 0 auto; } </style> <div class="container">Hello world</div>使用 flex 布局,通过设置
flex-direction属性为row,再设置justify-content属性为center实现水平居中。html<style> .container { display: flex; justify-content: center; } </style> <div class="container"> <div class="box">Hello world</div> </div>
垂直居中
绝对定位
对于绝对定位元素,当它存在高度时,可以通过设置 {top: 0; bottom: 0; margin: auto 0;} 实现垂直居中。
使用这种方案时需要注意
- 必须使用定位布局(脱离文档标准流);
- 元素高度必须设置,不允许为
auto。
html
<style>
.container {
height: 300px;
background-color: #eaeaea;
position: relative;
}
.box {
width: 100px;
height: 100px;
background-color: #ccc;
position: absolute;
top: 0;
bottom: 0;
margin: auto 0;
}
</style>
<div class="container">
<div class="box"></div>
</div>flex 布局
使用 flex 布局的 {align-items: center;} 可以实现子元素的垂直居中。
使用这种方案时需要注意
使用 flex 布局时所有的子元素都将被垂直居中。
html
<style>
.container {
height: 300px;
background-color: #eaeaea;
position: relative;
display: flex;
align-items: center;
}
.box {
background-color: #ccc;
}
</style>
<div class="container">
<div class="box">Hello world</div>
</div>transform 属性
使用 transform 属性实现元素的垂直居中,需要注意:
- 设置元素相对定位
- 设置元素的
transform属性为translate(0, -50%),表示元素向上移动自身的50% - 元素向下移动
top: 50%,表示元素向下移动父元素的50%
html
<style>
.container {
height: 300px;
background-color: #eaeaea;
}
.box {
background-color: #ccc;
position: relative; /* 设置元素相对定位 */
transform: translate(0, -50%); /* 元素向上移动自身的 50% */
top: 50%; /* 元素向下移动父元素的 50% */
}
</style>
<div class="container">
<div class="box">Hello world</div>
</div>