<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.parent{
display: table;
height:300px;
width: 300px;
background-color: #FD0C70;
}
.child{
display: table-cell;
vertical-align: middle;
font-size: 16px;
color: #fff;
text-align: center;
}
</style>
<body>
<!-- 方法一:父元素dispaly:table,子元素display:cell-table。 -->
<!-- 优势:父元素可以动态改变高度。
劣势:table属性容易造成多次reflow,IE8以下不支持 -->
<div class="parent">
<div class="child">DEMO</div>
</div>
<style type="text/css">
.par1{
position: absolute;
width: 500px;
height: 500px;
background: red;
text-align: center;
}
.child1{
background: pink;
color: #fff;
display: inline-block;
}
.par1:after{
display: inline-block;
content: "";
width: 0;
height: 100%;
vertical-align: middle;
}
</style>
<!-- 方法二:利用空元素或伪类 优点:兼容性好-->
<div class="par1">
<div class="child1">
<p>你若暗火,便是晴天哈哈哈哈哈</p>
<p>啦啦啦啦</p>
</div>
</div>
<style type="text/css">
.p3{
position: relative;
height:300px;
width: 300px;
background: #FD0C70;
}
.c3{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
color: #fff;
}
</style>
<!--方法三:绝对定位+transform 优点:方便,支持webkit内核
缺点:transform兼容性差,IE9以下不支持 -->
<div class="p3">
<div class="c3">
<p>你若暗火,便是晴天哈哈哈哈哈</p>
<p>若果没有面试,就海投吧</p>
</div>
</div>
<style type="text/css">
.p4{
display: flex;
justify-content: center;
align-items: center;
width: 300px;
height:300px;
background: #FD0C70;
}
.c4{
color: #fff;
}
</style>
<!-- 方法4:flexbox布局 优点:方便
缺点:兼容性不好,IE支持很差 -->
<div class="p4">
<div class="c4">
<p>你若暗火,便是晴天哈哈哈哈哈</p>
<p>若果没有面试,就海投吧</p>
</div>
</div>
</body>
</html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.parent{
display: table;
height:300px;
width: 300px;
background-color: #FD0C70;
}
.child{
display: table-cell;
vertical-align: middle;
font-size: 16px;
color: #fff;
text-align: center;
}
</style>
<body>
<!-- 方法一:父元素dispaly:table,子元素display:cell-table。 -->
<!-- 优势:父元素可以动态改变高度。
劣势:table属性容易造成多次reflow,IE8以下不支持 -->
<div class="parent">
<div class="child">DEMO</div>
</div>
<style type="text/css">
.par1{
position: absolute;
width: 500px;
height: 500px;
background: red;
text-align: center;
}
.child1{
background: pink;
color: #fff;
display: inline-block;
}
.par1:after{
display: inline-block;
content: "";
width: 0;
height: 100%;
vertical-align: middle;
}
</style>
<!-- 方法二:利用空元素或伪类 优点:兼容性好-->
<div class="par1">
<div class="child1">
<p>你若暗火,便是晴天哈哈哈哈哈</p>
<p>啦啦啦啦</p>
</div>
</div>
<style type="text/css">
.p3{
position: relative;
height:300px;
width: 300px;
background: #FD0C70;
}
.c3{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
color: #fff;
}
</style>
<!--方法三:绝对定位+transform 优点:方便,支持webkit内核
缺点:transform兼容性差,IE9以下不支持 -->
<div class="p3">
<div class="c3">
<p>你若暗火,便是晴天哈哈哈哈哈</p>
<p>若果没有面试,就海投吧</p>
</div>
</div>
<style type="text/css">
.p4{
display: flex;
justify-content: center;
align-items: center;
width: 300px;
height:300px;
background: #FD0C70;
}
.c4{
color: #fff;
}
</style>
<!-- 方法4:flexbox布局 优点:方便
缺点:兼容性不好,IE支持很差 -->
<div class="p4">
<div class="c4">
<p>你若暗火,便是晴天哈哈哈哈哈</p>
<p>若果没有面试,就海投吧</p>
</div>
</div>
</body>
</html>