div适应屏幕垂直居中的多种解决方案

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{margin: 0;padding: 0;font-size: 20px}
        /*方法一 div使用绝对布局 position:absolute; top left right button的值一样,不一定都为0;margin:auto;*/
        .main{
            width: 200px;
            height: 200px;
            background-color: #00b7ee;
            position: absolute;
            top:0;
            left: 0;
            right: 0;
            bottom:0;
            margin: auto;
            text-align: center;
            line-height: 180px;
        }
        /*方法二 绝对布局,让left和top都是50%,这在水平方向上让div的最左与屏幕的最左相距50%,垂直方向上一样,所以再用transform向左(上)平移它自己宽度(高度)的50%,也就达到居中效果了*/
        .main{
            width: 200px;
            height: 200px;
            background-color: #00b7ee;
            position: absolute;
            top:50%;
            left: 50%;
            transform: translate(-50%,-50%);
            text-align: center;
            line-height: 180px;
        }
        /*方法三 绝对布局,让left和top都是50%,margin: -100px 0 0 -100px;*/
       .main{
           width: 200px;
           height: 200px;
           background-color: #00b7ee;
           position: absolute;
           top:50%;
           left: 50%;
           margin: -100px 0 0 -100px;
           text-align: center;
           line-height: 180px;
       }
        /*方法四  flex布局*/
       body{
           width: 100%;
           height: 100vh;/**屏幕高度百分百*/
            display: flex;
            align-items:center;
            justify-content:center;
        }
        .main{
            border: 1px solid #0000FF;
            height: 200px;
            width: 400px;
            display: flex;
            align-items:center;
            justify-content:center;
        }
   </style>
</head>
<body>
  <div class="main">
    <h1 class="h11">MAIN</h1>
  </div>
</body>
</html>

 

图片文字并列时候 垂直居中

img{vertical-align:middle}

text{vertical-align:middle}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值