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

垂直居中html

作者:倾尽尘光暖流年   发布日期:2025-06-11   浏览:17

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

解释说明:

  1. HTML 结构:页面包含一个 container 容器,里面有一个 centered-box 元素。
  2. CSS 样式
    • .container 使用了 display: flex,并通过 justify-content: centeralign-items: center 实现了子元素的水平和垂直居中。
    • height: 100vh 确保容器的高度等于视口高度,从而使子元素在整个页面中居中。
    • .centered-box 是一个固定大小的盒子,背景颜色为浅蓝色,并且通过 line-height 使文本在其内部垂直居中。

上一篇:html个人简介网页代码

下一篇:html 右对齐

大家都在看

静态html源码

ios打开html

colspan在html中是什么意思

xml转html

html时间代码

html2canvas使用

html标题标签是什么

html 换行符号

html中b标签的作用

html美化代码

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

Laravel 中文站