CSS水平居中与垂直居中的方法

当我们页面布局的时候,通常需要把某一个元素居中,这一篇文章为大家介绍一下居中的几种方法,本人文笔有限,请见谅!

一.水平居中

行内元素 水平居中的方法,我们使用text-align:center;

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平居中</title>
    <style>
        .box {
            /* 给块元素设置宽高 */
            width: 300px;
            height: 50px;
            background-color: orange;
            /* 水平居中 */
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="box">
        <span>我是需要水平居中的文字</span>
    </div>
</body>
</html>


块元素水平居中 的方法
1.margin(外边距)的方法来做,使用margin:0 auto;

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平居中</title>
    <style>
        .box {
            /* 给块元素设置宽高 */
            width: 300px;
            height: 50px;
            background-color: orange;
            margin: 0 auto; 
        }
    </style>
</head>
<body>
    <div class="box"></div>
</body>
</html>

2.使用absolute加margin-left的方法

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平居中</title>
    <style>
        .box {
            /* 给加一个绝对定位 */
            position: absolute;
            /* 向右百分之50 */
            left: 50%;
            /* 外边距再减自身宽度的一半 */
            margin-left: -150px;
            /* 给块元素设置宽高 */
            width: 300px;
            height: 50px;
            background-color: orange;
        }
    </style>
</head>
<body>
    <div class="box">我是定位加margin</div>
</body>
</html>


3.使用absolute加transform

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平居中</title>
    <style>
        .box {
            /* 给加一个绝对定位 */
            position: absolute;
            /* 向右百分之50 */
            left: 50%;
            /* 横向上向左移动自身宽度的一半 */
            transform: translateX(-50%);
            /* 给块元素设置宽高 */
            width: 300px;
            height: 50px;
            background-color: orange;
        }
    </style>
</head>
<body>
    <div class="box">我是定位加transform</div>
</body>
</html>


4.flex弹性盒子方法

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平居中</title>
    <style>
      .father {
        width: 400px;
        height: 400px;
        background-color: orange;
        /* 给父级开启弹性盒子 */
        display: flex;
        /* 主轴对齐方式 */
        justify-content: center;
      }
      .son {
        width: 200px;
        height: 200px;
        background-color: pink;
      }
    </style>
</head>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
</body>
</html>

二.垂直居中

行内元素垂直居中,使用line-height

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>垂直居中</title>
    <style>
        .box {
            width: 300px;
            height: 50px;
            background-color: orange;
            /* 行高等于全部高度 */
            line-height: 50px;
        }
    </style>
</head>
<body>
    <div class="box"><span>我是垂直居中</span></div>
</body>
</html>

块元素垂直居中方法:
1.使用absolute加margin-top的方法

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>垂直居中</title>
    <style>
        .boss {
            position: relative;
            width: 300px;
            height: 200px;
            background-color: pink;
        }
        .box {
            /* 给加一个绝对定位 */
            position: absolute;
            /* 向下百分之50 */
            top: 50%;
            /* 外边距再减自身高度的一半 */
            margin-top: -25px;
            /* 给块元素设置宽高 */
            width: 300px;
            height: 50px;
            background-color: orange;
        }
    </style>
</head>
<body>
    <div class="boss">
        <div class="box">我是定位加margin</div>
    </div>
</body>
</html>

2.flex方法

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平居中</title>
    <style>
      .father {
        width: 400px;
        height: 400px;
        background-color: orange;
        /* 给父级开启弹性盒子 */
        display: flex;
        /* 侧轴对齐方式 */
        align-items: center;
      }
      .son {
        width: 200px;
        height: 200px;
        background-color: pink;
      }
    </style>
</head>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
</body>
</html>


3.使用absolute加transform

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>垂直居中</title>
    <style>
        .boss {
            position: relative;
            width: 300px;
            height: 300px;
            background-color: pink;
        }
        .box {
            /* 给加一个绝对定位 */
            position: absolute;
            /* 向下百分之50 */
            top: 50%;
            /* 横向上向上移动自身宽度的一半 */
            transform: translateY(-50%);
            /* 给块元素设置宽高 */
            width: 300px;
            height: 50px;
            background-color: orange;
        }
    </style>
</head>
<body>
    <div class="boss">
        <div class="box">我是定位加transform</div>
    </div>
</body>
</html>


感谢大家的阅读,如有什么不对的地方,可以向我提出,感谢大家!

相关推荐
玲小珑1 分钟前
Auto.js 入门指南(八)高级控件与 UI 自动化
android·前端
HarderCoder5 分钟前
ByAI:Rect-redux实现及connect函数
前端·react.js
小张快跑。7 分钟前
【Vue3】(三)vue3中的pinia状态管理、组件通信
前端·javascript·vue.js
我想说一句7 分钟前
当 map 遇上 parseInt:JS 中一场参数引发的“血案”
前端·javascript·面试
陈随易7 分钟前
2025年100个产品计划之第12个(杰森排序) - 对 JSON 属性进行排序
前端·后端·程序员
LeeAt8 分钟前
《谁杀死了比尔?》:使用Trae完成的一个推理游戏项目!!
前端·游戏开发·trae
Hockor12 分钟前
写给前端的 Python 教程四(列表/元组)
前端·后端·python
GetcharZp12 分钟前
「DPlayer」超强弹幕视频播放器来了!支持m3u8直播,5分钟搞定集成!
前端
天天码行空16 分钟前
Bootstrap Table企业级web数据表格集成框架
前端·javascript·开源
import_random20 分钟前
[关联规则]apriori算法和fp-growth算法(区别)
前端