首页 > Web开发 > 详细

关于css垂直水平居中的几种方法

时间:2016-10-08 19:09:01      阅读:163      评论:0      收藏:0      [点我收藏+]

第一种, 针对内联元素居中.

<div class="box box1">
    <span class="test">垂直居中</span>
</div>
<style>
    .box{
        width: 200px;
        height: 200px;
        border:1px solid red;
    }
    .box1{
        display: table-cell;
        vertical-align: middle;
        text-align: center;
    }
    .test{
        width: 30px;
        height: 30px;
        border:1px solid black;
    }
</style>

2: 弹性盒子

<div class="box box1">
    <div class="test"></div>
</div>
<style>
    .box{
        display: flex;
        justify-content: center;
        align-items: center;
        width: 200px;
        height: 200px;
        border:1px solid red;
    }
    .test{
        width: 30px;
        height: 30px;
        border:1px solid black;
    }
</style>

3translate

<div class="box box1">
    <div class="test"></div>
</div>
<style>
    .box{
        position: relative;
        width: 200px;
        height: 200px;
        border:1px solid red;
    }
    .test{
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        width: 30px;
        height: 30px;
        border:1px solid black;
    }
</style>

 

关于css垂直水平居中的几种方法

原文:http://www.cnblogs.com/joesbell/p/5939441.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!