垂直居中

使用margin:auto

  1. 父容器使用相对定位,并设置

  2. 子元素使用绝对定位,并且left,right,top,bottom都设置为0,margin设置为auto

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .content {
            background: #ee2c37;
            width: 400px;
            height: 400px;
            position: relative;
        }

        .item {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            width: 200px;
            height: 200px;
            background: #69ee20;
            margin: auto;
        }
    </style>
</head>
<body>
<div class="content">
    <div class="item"></div>
</div>
</body>
</html>

marign负值法

  1. 父容器使用相对定位,并设置

  2. 子元素使用绝对定位,并设置topleft的值为50%

  3. 使用margin的负值将元素移到中间,margin-top为元素height/2的负值,margin-left为元素width/2的负值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .content {
            background: #ee2c37;
            width: 400px;
            height: 400px;
            position: relative;
        }

        .item {
            position: absolute;
            top: 50%;
            left: 50%;
            width: 200px;
            height: 200px;
            margin-left: -100px;
            margin-top: -100px;
            background: #69ee20;
        }
    </style>
</head>
<body>
<div class="content">
    <div class="item"></div>
</div>
</body>
</html>

使用table-cell

  1. 父元素的display属性设置为table-cell,设置宽和高

  2. 在垂直方向上居中对齐,在水平方向上居中对齐

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .content {
            background: #ee2c37;
            width: 400px;
            height: 400px;
            display: table-cell;
            vertical-align: middle;
            text-align: -webkit-center;
            text-align: -moz-center;
        }

        .item {
            width: 200px;
            height: 200px;
            background: #69ee20;
        }
    </style>
</head>
<body>
<div class="content">
    <div class="item"></div>
</div>
</body>
</html>

这个有兼容性的问题,如果直接设置text-align: center;,在浏览器上是不会居中的,谷歌浏览器需要设置为text-align: -webkit-center;,火狐浏览器需要设置为text-align: -moz-center;

flex布局

  1. 将父容器设置为一个flex容器

  2. 在垂直的交叉轴上居中对齐,在水平的主轴上居中对齐

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .content {
            background: #ee2c37;
            width: 400px;
            height: 400px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .item {
            width: 200px;
            height: 200px;
            background: #69ee20;
        }
    </style>
</head>
<body>
<div class="content">
    <div class="item"></div>
</div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值