div垂直居中的方法!!

让div垂直居中的方法

1.给有固定宽高的div垂直居中:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>给固定宽高的div垂直居中 </title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .box {
            position: relative;
            width: 400px;
            height: 400px;
            margin-top: 30px;
            margin-left: 30px;
            border: 1px solid #000;
        }
        /*.box1是固定宽高的div垂直居中方法*/
        .box1 {
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -100px;
            margin-left: -100px;
            width: 200px;
            height: 200px;
            background-color: #C0C0C0;
        }

    </style>
</head>
<body>
    <div class="box">
        <div class="box1"></div>
    </div>
</body>
</html>

在网页中显示入下图:
这里写图片描述

2.给不固定宽高的div
- - - 第一种:利用伪类元素和inline-block,vertical-align。前提条件是居中的元素的inline-block

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>给不固定的宽高的div居中的方法1</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .box {
            text-align: center;
            border: 1px solid #000;

        }
        .box:before {
            content: '';
            display: inline-block;
            height: 100%;
            vertical-align: middle;
            margin-right: -0.3em;  
        }
        .box1 {
            display: inline-block;
            vertical-align: middle;
            width: 50%;
        }
    </style>
</head>
<body>
    <div class="box" style="height: 300px;">
        <div class="box1">
            <h1>给不固定的宽高div</h1>
            <p>给不固定的宽高的div居中的方法1:利用伪类元素和inline-block,vertical-align。前提条件是居中的元素的inline-block</p>
        </div>
    </div>
</body>
</html>

给不固定的宽高div

— 第二种:使用网页最开始的布局,table布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>table布局</title>
    <style>
        .box {
            display: table;
            width: 100%;

        }
        .box1 {
            display: table-cell;
            text-align: center;
            vertical-align: middle;
        }
    </style>
</head>
<body>
    <div class="box" style="border: 1px solid #000;">
        <div class="box1">
            table布局,table布局,table布局,table布局,table布局.
        </div>
    </div>
</body>
</html>

这里写图片描述

注释:上面2种兼容IE8

–第三种:利用css3的tranlate(),不兼容IE8,但比较通用!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box {
            position: fixed;
            top: 50%;
            left: 50%;
            background-color: #c0c0c0;
            width: 50%;
            height: 50%;
            -webkit-transform: translateX(-50%) translateY(-50%);
            -moz-transform: translateX(-50%) translateY(-50%);
            -ms-transform: translateX(-50%) translateY(-50%);
            transform: translateX(-50%) translateY(-50%);
        }
    </style>
</head>
<body>
    <div class="box"></div>
</body>
</html>

这里写图片描述

其他方法
- - 1.使用flex布局,在不固定宽高div水平垂直居中,在它的父元素添加

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

如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box {
            display: flex;
            justify-content: center;
            align-items: center;
        }

    </style>
</head>
<body>
    <div class="box">
        <div class="box1">采有flex布局</div>
    </div>
</body>
</html>

这里写图片描述

-—2.利用margin:auto
在设置块级元素margin:0 auto 时,这个块级元素左右居中,然后使用定位来设置块级元素上下居中。这样不就可以实现让不给宽高的div实现水平垂直居中。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: #C9C9C5;
        }
        .box1 {
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            width: 50%;
            height: 50%;
            margin: auto;
            background-color: #C0C0C0;

        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box1"></div>
    </div>
</body>
</html>

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值