码迷,mamicode.com
首页 > 其他好文 > 详细

div在父元素中的居中问题

时间:2018-01-25 14:18:33      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:enter   center   tab   ali   lap   col   bottom   orm   play   

方法一:定位方法

 

<div class="parent1">
    <div class="child1">
        content1
    </div>
</div>
.parent1{
    position:relative;
    height:300px;
     width:300px;
}
.child1{
    position:absolute;
  top:0px;
  left:0px;
  right:0px;
  bottom:0px;
  margin:auto;

}

  

 方法二:设置父元素的css为:display:table;z子元素的为:dispaly:table-cell;vertical-align:middle;text-align:center

 

<div class="parent2">
    <div class="child2">
        content2
    </div>
</div>
.parent2{
    display:table;
    height:300px;
     width:300px;
    background:red;
}
.child2{
    display:table-cell;
    vertical-align:middle;
    text-align:center;
    background:green;
    ont-size:16px;

}

  方法三:利用transform

<div class="parent3">
    <div class="child3">
        content3
    </div>
</div>

.parent3{ position: relative; height:300px; width: 300px; background: #FD0C70; } .parent3 .child{ position: absolute; top: 50%; left: 50%; color: #fff; background: green; transform: translate(-50%, -50%); }

  

 

div在父元素中的居中问题

标签:enter   center   tab   ali   lap   col   bottom   orm   play   

原文地址:https://www.cnblogs.com/hl2016-10-28/p/8350757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!