Skip to content

Instantly share code, notes, and snippets.

@z2015
Created May 2, 2016 02:21
Show Gist options
  • Save z2015/8c8c6bc48dd56b13d453b44683c8867b to your computer and use it in GitHub Desktop.
Save z2015/8c8c6bc48dd56b13d453b44683c8867b to your computer and use it in GitHub Desktop.
两种实现css垂直居中vertical-align的方法
/*#1*/
div {
display: table-cell;
vertical-align: middle;
}
/*#2*/
<h3>With an added pseudo element it's possible</h3>
<div class="block2"><div class="inner">Inline-Block</div></div>
/* With an added fake (pseudo) element it works */
.block2 {
background: orange;
height: 80px;
}
/* Fake (pseudo) element, that enables vertical-align */
.block2:before {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
.inner {
display: inline-block;
vertical-align: middle;
background: yellow;
padding: 3px 5px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment