不使用line-height或flex或absoulte 文字垂直居中
时间: 2023-12-29 09:06:01 浏览: 132
可以使用以下方法实现文字垂直居中:
1. 使用 display: table 和 display: table-cell
HTML:
```
<div class="container">
<div class="content">
<p>垂直居中的文本</p>
</div>
</div>
```
CSS:
```
.container {
display: table;
width: 100%;
height: 200px;
}
.content {
display: table-cell;
vertical-align: middle;
}
p {
margin: 0;
}
```
2. 使用 position: absolute 和 transform
HTML:
```
<div class="container">
<div class="content">
<p>垂直居中的文本</p>
</div>
</div>
```
CSS:
```
.container {
position: relative;
width: 100%;
height: 200px;
}
.content {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
p {
margin: 0;
}
```
这两种方法都可以实现文字的垂直居中,但是第一种方法需要在父元素上设置 display: table 和 display: table-cell,不太符合语义化标准。第二种方法需要使用 position: absolute,可能会影响到其他元素的布局。因此,在实际使用中需要根据具体情况选择合适的方法。
阅读全文
相关推荐











