css实现元素水平垂直居中——6种方式

实现元素水平垂直居中的方式:

  • 利用定位+margin:auto
  • 利用定位+margin:负值
  • 利用定位+transform
  • table布局
  • flex布局
  • grid布局
  • 利用定位+margin:auto:父级设置为相对定位,子级绝对定位 ,并且四个定位属性的值都设置了0,那么这时候如果子级没有设置宽高,则会被拉开到和父级一样宽高。这里子元素设置了宽高,所以宽高会按照我们的设置来显示,但是实际上子级的虚拟占位已经撑满了整个父级,这时候再给它一个margin:auto它就可以上下左右都居中了
<style>
    .father{
        width:500px;
        height:300px;
        border:1px solid #0a3b98;
        position: relative; // 父级相对定位
    }
    .son{
        width:100px;
        height:40px;
        background: #f0a238;
        position: absolute; // 子级绝对定位
        top:0; // 以下四个定位属性设为0
        left:0;
        right:0;
        bottom:0;
        margin:auto; // 外边距自动
    }
</style>

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

  • 利用定位+margin:负值:绝大多数情况下,设置父元素为相对定位, 子元素移动自身50%实现水平垂直居中
<style>
    .father {
        position: relative;
        width: 200px;
        height: 200px;
        background: skyblue;
    }
    .son {
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left:-50px;
        margin-top:-50px;
        width: 100px;
        height: 100px;
        background: red;
    }
</style>
<div class="father">
    <div class="son"></div>
</div>

整个实现思路如下图所示:

  1. 初始位置为方块1的位置
  2. 当设置lefttop50%的时候,内部子元素为方块2的位置
  3. 设置margin为负数时,使内部子元素到方块3的位置,即中间位置

这种方案不要求父元素的高度,也就是即使父元素的高度变化了,仍然可以保持在父元素的垂直居中位置,水平方向上是一样的操作

但是该方案需要知道子元素自身的宽高,但是我们可以通过下面transform属性进行移动

实现代码如下:

<style>
    .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;
    }
</style>
<div class="father">
    <div class="son"></div>
</div>

translate(-50%, -50%)将会将元素位移自己宽度和高度的-50%

这种方法其实和最上面被否定掉的margin负值用法一样,可以说是margin负值的替代方案,并不需要知道自身元素的宽高

  • table布局

设置父元素为display:table-cell,子元素设置 display: inline-block。利用verticaltext-align可以让所有的行内块级元素水平垂直居中

<style>
    .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;
    }
</style>
<div class="father">
    <div class="son"></div>
</div>

  • flex布局
<style>
    .father {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 200px;
        height: 200px;
        background: skyblue;
    }
    .son {
        width: 100px;
        height: 100px;
        background: red;
    }
</style>

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

css3中的flex布局,可以非常简单实现垂直水平居中

    这里可以简单看看flex布局的关键属性作用:

  1. display: flex时,表示该容器内部的元素将按照flex进行布局
  2. align-items: center表示这些元素将相对于本容器水平居中
  3. justify-content: center也是同样的道理垂直居中
  • grid布局(CSS新出的网格布局,代码量少,但兼容性不太好)
<style>
    .father {
            display: grid;
            align-items:center;
            justify-content: center;
            width: 200px;
            height: 200px;
            background: skyblue;
        }
        .son {
            width: 10px;
            height: 10px;
            border: 1px solid red
        }
</style>
<div class="father">
    <div class="son"></div>
</div>

这里看到,gird网格布局和flex弹性布局都简单粗暴

### CSS 实现元素水平垂直居中布局的方法 以下是常见的几种方法来实现 CSS 中的水平垂直居中: #### 方法一:使用 `margin: auto` 和绝对定位 通过设置子元素的位置为绝对,并利用外边距自动分配的特点,可以轻松实现水平垂直居中。 ```css .parent { position: relative; height: 300px; /* 设置高度 */ width: 300px; /* 设置宽度 */ } .child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); margin: auto; } ``` 这种方法适用于已知宽高的子元素[^1]。 #### 方法二:使用 `vertical-align` 和 `line-height` 当需要对行内元素或表单元素进行垂直居中时,可以通过组合 `text-align` 和 `line-height` 来完成。这种方式适合于单行文字或者固定高度的内容。 ```css .container { text-align: center; /* 水平居中 */ line-height: 200px; /* 容器的高度 */ height: 200px; } .item { display: inline-block; vertical-align: middle; line-height: normal; /* 防止继承容器的行高 */ } ``` 此方法仅限于特定场景下的应用[^2]。 #### 方法三:弹性布局(Flexbox) 现代浏览器广泛支持 Flexbox 布局模型,它提供了非常简洁的方式来处理复杂的布局需求,包括水平和垂直方向上的中心化。 ```css .flex-container { display: flex; justify-content: center; /* 主轴上居中 */ align-items: center; /* 交叉轴上居中 */ height: 300px; /* 设定容器大小 */ width: 300px; } ``` 这是目前推荐使用的解决方案之一,因为它具有良好的兼容性和易读性[^3]。 #### 方法四:网格布局(Grid Layout) 类似于 Flexbox 的功能强大之处在于能够创建二维空间内的项目排列方式。同样也可以很容易做到让某个区域位于屏幕正中央。 ```css .grid-container { display: grid; place-items: center; /* 同时指定行列方向均居中 */ height: 300px; width: 300px; } ``` 以上列举了几种主流技术手段用于解决网页设计过程中遇到的实际问题——即如何使某对象既能在页面内部横向又能纵向精确地位于其中心点处。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值