Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

html上下居中

作者:夏威夷丶霪男   发布日期:2025-10-25   浏览:84

<!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>

解释说明:

  1. Flexbox 布局:通过 display: flex.container 设置为 Flex 容器。
  2. 水平居中:使用 justify-content: center 实现子元素的水平居中。
  3. 垂直居中:使用 align-items: center 实现子元素的垂直居中。
  4. 视口高度:通过 height: 100vh 设置容器的高度为整个视口的高度,确保内容可以在页面上居中显示。

上一篇:html rem

下一篇:html里面display

大家都在看

静态html源码

404 html

ios打开html

nginx访问不到html

html 符号

colspan在html中是什么意思

xml转html

html时间代码

html2canvas使用

html标题标签是什么

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站