元素水平垂直居中的几种方式

本文详细介绍了使用CSS实现元素在页面中水平垂直居中的多种方法,包括单行文本、多行文本、相对定位、绝对定位、Flex布局、Grid布局及Table布局等技巧,适用于宽高已知或未知的情况。

实现元素的水平垂直居中(宽高已知):

  1. 单行文本居中
水平居中:text-align:center;垂直居中:line-height:XXpx; /*line-height与元素的height的值一致*/

2.多行文本居中

display:table

主要实现代码:

display: table使块状元素成为一个块级表格;

display: table-cell;子元素设置成表格单元格;

vertical-align: middle;使表格内容居中显示,即可实现垂直居中的效果;

   .box {
            width: 300px;
            height: 300px;
            margin: 0 auto;
            display: table;
            background: #ff5576;
        }

        .box p {
            color: #fff;
            display: table-cell;
            vertical-align: middle;
        }
   <div class="box">

        <p>
            宽高父元素为300px背景颜色是红色 元素内字体为白色 父元素box设置为display:table 子元素p便签设置为display:table-cell
        </p>
    </div>

在这里插入图片描述

3.使用相对定位、绝对定位

父元素设置宽高position relative,子元素设置绝对定位position absolute top和left位移本身宽高的一半(transform:translate(-50%,-50%)))

具体代码实现:

  <div class="box2">
        <div class="ch2">

        </div>
    </div>
   .box2{
            width: 300px;
            height: 300px;
            background: #0076ff;
            position: relative;
        }
        .ch2{
            width: 100px;
            height: 100px;
            position: absolute;
            background: #ff5577;
            left: 50%;
            top: 50%;
            margin-left: -50px;
            margin-top: -50px;
        }

在这里插入图片描述

4、使用display:flex将父元素设置为弹性盒,再给子元素设置 align-items: center;定义项目在交叉轴(纵轴)上如何对齐,垂直对齐居中

justify-content: center; 定义了项目在主轴上的对齐方式,水平对齐居中

 #box{
         width: 400px;
         height: 400px;
         background: red;
         display: flex;
         justify-content: center;
         align-items: center;
    
     }
     #x{
         width: 200px;
         height: 200px;
         background: yellow;
       
     }



5.父元素设置相对定位 子元素自适应

  #box{
         width: 400px;
         height: 400px;
         background: red;
         position: relative;
     }
     #x{
         width: 200px;
         height: 200px;
         background: yellow;
         position: absolute;
         left: 0;
         top: 0;
         right: 0;
         bottom: 0;
         margin: auto;
       
     }

宽高未知(4种方案):

方案1:定位

html

  <div class="father">
            <div class="son"></div>
   </div>

css

 .father {
            position: relative;
            width: 200px;
            height: 200px;
            background: skyblue;
        }
        .son {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
            width: 100px;
            height: 100px;
            background: red;
        }

方案2:flex
html不变:

 <div class="father">
            <div class="son"></div>
  </div>

css

  .father {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 200px;
            height: 200px;
            background: skyblue;
        }
        .son {
            width: 100px;
            height: 100px;
            background: red;
        }

方案3:table布局
需要设置父元素为display:table-cell,利用vertical和text-align可以让所有的行内块级元素水平垂直居中
给子元素设置 display: inline-block

html不变:

 <div class="father">
        <div class="son"></div>
    </div>

css:

 .father {
            display: table-cell;
            width: 200px;
            height: 200px;
            background: skyblue;
            vertical-align: middle;
            text-align: center;
        }
        .son {
            display: inline-block;
            width: 100px;
            height: 100px;
            background: red;
        }

方案4:grid布局

html不变

  <div class="father">
            <div class="son"></div>
   </div>

css不变:

  .father {
            display: grid;
            align-items:center;
            justify-content: center;
            width: 200px;
            height: 200px;
            background: skyblue;

        }
        .son {
            width: 10px;
            height: 10px;
            border: 1px solid red
        }
### CSS 实现元素水平垂直居中的方法汇总 以下是几种常见的实现元素水平垂直居中的方法: #### 方法一:使用 `display: table-cell` 和 `vertical-align: middle` 通过将父容器的 `display` 属性设置为 `table-cell`,并配合 `vertical-align: middle`,可以轻松实现子元素垂直居中。同时,为了达到水平居中,可以在父容器上设置 `text-align: center`[^1]。 ```html <div style="border: 1px solid black; width: 300px; height: 300px; display: table-cell; text-align: center; vertical-align: middle;"> <div style="background-color: red; display: inline-block;">水平垂直居中</div> </div> ``` --- #### 方法二:使用绝对定位和 `transform` 这种方法利用了 `position: absolute` 的特性以及 `translate()` 函数来调整位置。通过设置 `top: 50%; left: 50%` 将元素中心点移动到父容器的中心,再用 `transform: translate(-50%, -50%)` 抵消自身的宽度和高度偏移[^4]。 ```css .parent { position: relative; width: 300px; height: 300px; } .child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } ``` --- #### 方法三:使用 Flexbox 布局 Flexbox 是现代布局中最常用的方法之一。只需将父容器的 `display` 设置为 `flex`,并通过 `align-items: center` 和 `justify-content: center` 来分别控制垂直和水平方向上的居中[^2]。 ```css .parent { display: flex; align-items: center; justify-content: center; width: 300px; height: 300px; } ``` --- #### 方法四:使用 Grid 布局 Grid 布局提供了更强大的二维布局能力。可以通过设置 `place-items: center` 或者单独指定 `align-items` 和 `justify-items` 来完成居中操作[^2]。 ```css .parent { display: grid; place-items: center; width: 300px; height: 300px; } ``` --- #### 方法五:行内块元素与基线对齐 对于某些特定类型的行内块元素(如 `<input>`、`<img>` 等),可以直接通过设置其 `vertical-align` 属性为 `middle` 来实现与其他文本内容的水平对齐[^3]。 ```html <span>这是一个例子:</span><input type="text" style="vertical-align: middle;" /> ``` --- 以上列举了几种主流的 CSS 水平垂直居中方法,每种方法都有各自的适用场景和技术特点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值