内容垂直居中

方法一: 内边距(padding)

方法二: 行高 (line-height)

方法三: 定位 (position, transform)

   html: 

<div class="box">
    <div class="content">
        <p>测试垂直居中效果测试垂直居中效果</p>
        <p>测试垂直居中效果测试垂直居中效果</p>
    </div>
</div>

  css: 

    .box {
            margin:0 auto;
            width: 300px;
            height: 300px;
            background: #000;
            position: relative;
        }
        .box .content {
            color: red;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            -webkit-transform: translate(-50%, -50%);
            -ms-transform: translate(-50%, -50%);
            -moz-transform: translate(-50%, -50%);
        }

效果图:

  

 

 

方法四: 模拟表格(display: table;)

  css: 

        .box {
            display:table;
            width:300px;
            height:300px;
            background:#000;
            margin:0 auto;
            color:red;
        }
        .box .content {
            display:table-cell; 
            vertical-align:middle;
            text-align: center;
        }                

效果图: 

    

 

方法五: 盒子(box)

  css: 

       .box {
            width: 300px;
            height: 200px;
            padding: 10px;
            border: 1px solid #ccc;
            background:#000;
            color:#fff;
            margin: 20px auto;


            display: -webkit-box;
            -webkit-box-orient: horizontal;
            -webkit-box-pack: center;  //start center end -->左 中 右
            -webkit-box-align: center;  //start center end -->上 中 下

            display: -moz-box;
            -moz-box-orient: horizontal;
            -moz-box-pack: center;
            -moz-box-align: center;

            display: -o-box;
            -o-box-orient: horizontal;
            -o-box-pack: center;
            -o-box-align: center;

            display: -ms-box;
            -ms-box-orient: horizontal;
            -ms-box-pack: center;
            -ms-box-align: center;

            display: box;
            box-orient: horizontal;
            box-pack: center;
            box-align: center;
        }

效果图: 

 

转载于:https://www.cnblogs.com/wangyihong/p/7267816.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值