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

垂直居中的三种方式(父子容器高度不一定)

时间:2017-04-28 18:37:23      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:yellow   居中   dem   back   方式   splay   item   set   isp   

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>vertical center</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.parent{
width: 200px;
height: 100px;
margin: 20px;
background-color: yellow;
}
.child{
background-color: green;
}
/*the three ways can be adapted to unknown height of parent ild container*/
/*first way*/
.parent-a{
position: relative;
}
.child-a{
position: absolute;
top: 50%;
transform: translateY(-50%);
}
/*second way*/
.parent-b{
display: flex;
align-items: center;
}
/*third way*/
.parent-c{
display: table-cell;
vertical-align: middle;
}
</style>
</head>
<body>
<div class="parent parent-a">
<div class="child child-a">demo-a</div>
</div>
<div class="parent parent-b">
<div class="child child-b">demo-b</div>
</div>
<div class="parent parent-c">
<div class="child child-c">demo-c</div>
</div>
</body>
<html>

  

垂直居中的三种方式(父子容器高度不一定)

标签:yellow   居中   dem   back   方式   splay   item   set   isp   

原文地址:http://www.cnblogs.com/fm060/p/6782601.html

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