css 中垂直居中
在 CSS 中实现垂直居中有多种方法,下面列举了其中的几种常见方法:
- 使用 flexbox:
.container {
display: flex;
align-items: center; /* 垂直居中 */
}
- 使用绝对定位和 transform 属性:
.container {
position: relative;
}
.centered {
position: absolute;
top: 50%;
transform: translateY(-50%); /* 垂直居中 */
}
- 使用表格布局:
.container {
display: table;
}
.centered {
display: table-cell;
vertical-align: middle; /* 垂直居中 */
}
- 使用 line-height 属性(只适用于单行文本):
.container {
height: 100px;
line-height: 100px; /* 垂直居中 */
}
这些方法适用于不同的布局场景,请根据具体情况选择合适的方法
原文地址: https://www.cveoy.top/t/topic/iT2X 著作权归作者所有。请勿转载和采集!