Skip to content

Instantly share code, notes, and snippets.

@cycold
Created May 8, 2014 08:16
Show Gist options
  • Save cycold/597ca7f0c829ed0713f1 to your computer and use it in GitHub Desktop.
Save cycold/597ca7f0c829ed0713f1 to your computer and use it in GitHub Desktop.
*{margin:0;padding:0;}
.text{
display: inline-block;
font-size:16px;
vertical-align: middle;/* 保证文字垂直居中 */
}
.box-1{
height:200px;
width:300px;
background:pink;
margin:30px auto;
font-size:0px; /*这里的字体大小一定要设为0*/
}
.box-1:before{
content: '我是那个备胎元素';
display: inline-block;
vertical-align: middle;
width: 1em;
background:lightblue;
margin-left:-1em;
font-size:16px;
height: 100%;
}
.box-2{
height:200px;
width:300px;
background:pink;
margin:30px auto;
font-size:0px; /*这里的字体大小一定要设为0*/
}
.box-2:before{
content: '我是那个备胎元素';
display: inline-block;
vertical-align: middle;
width: 0;
height: 100%;
}
<meta name="description" content="多行文本未知高度垂直居中" />
<body>
<div class="box-1">
<p class="text">
多行文本未知高度垂直居中
多行文本未知高度垂直居中
多行文本未知高度垂直居中
-by 一丝
</p>
</div>
<div class="box-2">
<p class="text">
多行文本未知高度垂直居中
多行文本未知高度垂直居中
多行文本未知高度垂直居中
-by 一丝
</p>
</div>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment