<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML 上下居中示例</title>
<style>
/* 使用 Flexbox 实现上下居中 */
.container {
display: flex;
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
height: 100vh; /* 设置容器高度为视口高度 */
}
.centered-box {
width: 200px;
height: 200px;
background-color: lightblue;
text-align: center;
line-height: 200px; /* 让文本在盒子内垂直居中 */
}
</style>
</head>
<body>
<div class="container">
<div class="centered-box">居中的内容</div>
</div>
</body>
</html>
display: flex 将 .container 设置为 Flex 容器。justify-content: center 实现子元素的水平居中。align-items: center 实现子元素的垂直居中。height: 100vh 设置容器的高度为整个视口的高度,确保内容可以在页面上居中显示。上一篇:html rem
下一篇:html里面display
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站