元素垂直水平居中

方法一:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body,html{
            width: 100%;
            height: 100%;
        }
        .box{
            width: 100%;
            height: 100%;
            background-color: rgba(0, 255, 255, 0.466); 
            text-align: center;  // 必不可少,否则元素不水平居中
        }
        .small{
            width: 500px;
            height: 500px;
            background-color: rgba(224, 75, 75, 0.808);
            display: inline-block;
            vertical-align: middle;  
            margin: 0 auto;
        }
        .help{
            width: 0;
            height: 100%;
            display: inline-block;
            vertical-align: middle;  
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="small"></div>
        <div class="help"></div>
    </div>
</body>
</html>

方法二:

也可以通过父元素设置伪元素 :before ,然后设置子元素 verticle-align:middle 实现垂直居中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body,html{
            width: 100%;
            height: 100%;
        }
        .box{
            width: 100%;
            height: 100%;
            background-color: rgba(0, 255, 255, 0.466); 
            text-align: center;
        }
        .box::before{
            content: "";
            width: 0;
            height: 100%;
            display: inline-block;
            vertical-align: middle;  
        }

        .small{
            width: 500px;
            height: 500px;
            background-color: rgba(224, 75, 75, 0.808);
            display: inline-block;
            vertical-align: middle;  
            margin: 0 auto;
        } 
    </style>
</head>
<body>
    <div class="box">
        <div class="small"></div> 
    </div>
</body>
</html>

方法三: 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body,html{
            width: 100%;
            height: 100%;
        }
        .box{
            width: 100%;
            height: 100%;
            background-color: rgba(0, 255, 255, 0.466); 
            display: flex;
            align-items: center; // 让元素垂直居中
            justify-content: center; // 让元素水平居中
        }
        .small{
            width: 500px;
            height: 500px;
            background-color: rgba(224, 75, 75, 0.808); 
        }
        
    </style>
</head>
<body>
    <div class="box">
        <div class="small"></div> 
    </div>
</body>
</html>

方法四: 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body,html{
            width: 100%;
            height: 100%;
        }
        .box{
            width: 100%;
            height: 100%;
            background-color: rgba(0, 255, 255, 0.466); 
            position: relative;
        }
        .small{
            width: 500px;
            height: 500px;
            background-color: rgba(224, 75, 75, 0.808); 
            // 万能适用,尤其是自适应场景也可以直接写具体px
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%); 
        }
        
    </style>
</head>
<body>
    <div class="box">
        <div class="small"></div> 
    </div>
</body>
</html>

方法五:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body,html{
            width: 100%;
            height: 100%;
        }
        .box{
           width: 100vw;
           height: 100vh;
            line-height: 100vh;
            background-color: rgba(0, 255, 255, 0.466); 
            text-align: center;
        }
        .small{
            width: 500px;
            height: 500px;
            background-color: rgba(224, 75, 75, 0.808);  
             display: inline-block;
        }
        
    </style>
</head>
<body>
    <div class="box">
        <div class="small"></div> 
    </div>
</body>
</html>

 方法六:

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html,body{
            width: 100%;
            height: 100%;
        }
        .box {
            width: 100%;
            height: 100%;
            background: red;
            /* BEGIN 垂直居中 */
            display: flex;
            flex-direction: column;
            justify-content: center;
            
             /* END 垂直居中 */
        }
 
        .main {
            width: 200px;
            height: 200px;
            background: palegoldenrod;
            /* 如果水平也需要居中 */
            margin: 0 auto;
        }
    </style>
</head>
 
<body>
    <div class="box">
        <div class="main"></div>
    </div>
</body>
 
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值