使用 display: inline-block + vertical-align 垂直水平居中时会出现问题
问题中提供了一段代码,其中使用了 display: inline-block 和 vertical-align: middle 来对齐文本。然而,多行文本不能垂直或水平居中。
检查代码后发现,缺失了 html 和 body 标签,导致这行代码无法正常工作。添加这些标签后,布局如预期的那样工作。
修改后的代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="parent">
<span class="child">测试示例测试示例测试示例测试示例测试示例测试示例测试示例测试示例测试示例测试示例测试示例测试示例测试示例测试示例测试示例测试示例测试示例测试示例测试示例测试示例测试示例</span>
</div>
<style>
.parent {
height: 100px;
background-color: aqua;
text-align: center; /*水平居中*/
line-height: 100px; /* 通过 line-height 设置幽灵节点的基线 */
}
.child {
color: #fff;
background-color: blueviolet;
vertical-align: middle;
line-height: normal; /* 块级元素时需要加 */
display: inline-block; /* 重点,要把 line-height 设置成 normal, 要不然会继承100 */
}
</style>
</body>
</html>以上就是使用inline-block和vertical-align实现文本垂直水平居中时,为何多行文本无法居中?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号