

思路:子元素绝对定位,距离顶部 50%,左边50%,然后使用css3 transform:translate(-50%; -50%)
优点:高大上,可以在webkit内核的浏览器中使用
缺点:不支持IE9以下不支持transform属性
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>未知宽高元素水平垂直居中</title>
</head>
<style>
.parent3{
 position: relative;
 height:300px;
 width: 300px;
 background: #FD0C70;
}
.parent3 .child{
 position: absolute;
 top: 50%;
 left: 50%;
 color: #fff;
 transform: translate(-50%, -50%);
}
</style>
<body>
<p class="parent3">
 <p class="child">hello world-3</p>
 </p>
</body>
</html>相关推荐:
CSS选择器的代码实例以及css优先级的代码实例
当页面文字过多时,怎么用css使超出部分显示省略号?(单/多行代码演示)
