CSS实现水平垂直居中的几种方法

在布局中,水平垂直居中这个问题经常会碰见,所以我就收集了比较常用的几种水平垂直居中的方法~ 方便自己寻找答案,也方便大家寻找!

1、利用margin负值

主要CSS代码:

position: absolute
left: 50%;
top: 50%;
width: (width)px;
height: (height)px;
margin-left: -(width/2)px; /*宽度的一半*/
margin-top: -(height/2)px; /*高度的一半*/

实现示例:

这里写图片描述

完整HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平垂直居中</title>
    <style>
        .container {
            position: relative;
            background: #006400;
            width: 600px;
            height: 400px;
        }
        .center {
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -50px;
            margin-top: -25px;
            width: 100px;
            height: 50px;
            background: #98FB98;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="center"></div>
    </div>
</body>
</html>

这种方法必须要知道该元素的宽高才可以设置水平垂直居中,且margin-left的值为宽度的一半,margin-top的值为高度的一半。

如果不知道该元素的宽高,你还想用绝对定位的方法的话,可以使用transform来实现。
使用transform实现的优点:
1、可以不知道宽高
2、代码量少
缺点:
1、IE8不支持
2、可能干扰其他transform实现效果

主要CSS代码:

transform: translate(-50%, -50%); /*50%为自身尺寸的一半*/

实现效果如下:

这里写图片描述

完整HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平垂直居中</title>
    <style>
        .container {
            position: relative;
            background: #006400;
            width: 600px;
            height: 400px;
        }
        .center {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            background: #98FB98;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="center">哈哈哈</div>
    </div>
</body>
</html>

2、利用margin: auto实现绝对定位元素的居中

这里的要点就是:
1、上下左右都定位为0
2、使用margin: auto

主要CSS代码:

position: absolute; 
left: 0; 
top: 0; 
right: 0; 
bottom: 0;
margin: auto;    /* 有了这个就自动居中了 */

实现示例:
这里写图片描述

完整HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平垂直居中</title>
    <style>
        .container {
            position: relative;
            background: #006400;
            width: 600px;
            height: 400px;
        }
        .center {
            position: absolute;
            left: 0;
            top: 0;
            bottom: 0;
            right: 0;
            margin: auto;
            width: 80px;
            height: 80px;
            background: #98FB98;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="center"></div>
    </div>
</body>
</html>

3、设置display: table-cell

优点:
1、高度可变
2、内容溢出会将父元素撑开。
3、跨浏览器兼容性好。
缺点:
需要额外的HTML代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平垂直居中</title>
    <style>
        .container {
            background: #006400;
            width: 600px;
            height: 400px;
            display: table;
        }
        .wrap {
            text-align: center;
            display: table-cell;
            vertical-align: middle;
        }
        .center {
            background: #98FB98;
            width: 20%;
            height: 20%;
            margin: auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="wrap">
            <div class="center"></div>
        </div>
    </div>
</body>
</html>

4、利用inline-block

将div设置成inline-block,之后,用处理图片垂直居中的方式处理即可。

主要CSS代码:

.container {
    display: table-cell;
    vertical-align: middle;
    text-align: center; 
}
.center {
    display: inline-block;
}

完整HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平垂直居中</title>
    <style>
        .container {
            background: #006400;
            width: 600px;
            height: 400px;
            display: table-cell;
            vertical-align: middle;
            text-align: center;
        }
        .center {
            display: inline-block;
            background: #98FB98;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="center">哈哈哈</div>
    </div>
</body>
</html>

5、使用flexbox

flexbox是CSS3的新增属性,设计初衷是为了解决像垂直居中这样的常见布局问题。

主要CSS代码:

display: flex;
justify-content: center;
align-items: center;

完整HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平垂直居中</title>
    <style>
        .container {
            background: #006400;
            width: 600px;
            height: 400px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .center {
            background: #98FB98;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="center">哈哈哈</div>
    </div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值