居中布局
垂直布局
写出两个盒子并满足以下条件条件:
1.里面的高度盒子不固定
2.外面盒子的高度不固定
第一种方式table-cell+ vertical-align
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
body{
margin: 0;
}
.parent{
background: gray;
width: 100px;
height: 500px;
display: table-cell;
vertical-align:middle;
}
.son{
background: pink;
}
</style>
</head>
<body>
<div class="parent">
<div class="son">我是一段文字</div>
</div>
</body>
</html>
第二种方式absolute+transform(兼容性不是很好)
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
body{
margin: 0;
}
.parent{
background: gray;
width: 100px;
height: 500px;
position: relative;
}
.son{
background: pink;
height: 100px;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
</style>
</head>
<body>
<div class="parent">
<div class="son">我是一段文字</div>
</div>
</body>
</html>
第三种方式flex(兼容问题)
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
body{
margin: 0;
}
.parent{
background: gray;
width: 100px;
height: 500px;
display: flex;
align-items: center;
}
.son{
background: pink;
}
</style>
</head>
<body>
<div class="parent">
<div class="son">我是一段文字</div>
</div>
</body>
</html>
http://www.zhangxinxu.com/wordpress/2015/08/css-deep-understand-vertical-align-and-line-height/