<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>垂直居中示例</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>
container
容器,里面有一个 centered-box
元素。.container
使用了 display: flex
,并通过 justify-content: center
和 align-items: center
实现了子元素的水平和垂直居中。height: 100vh
确保容器的高度等于视口高度,从而使子元素在整个页面中居中。.centered-box
是一个固定大小的盒子,背景颜色为浅蓝色,并且通过 line-height
使文本在其内部垂直居中。上一篇:html个人简介网页代码
下一篇:html 右对齐
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站