Skip to content

Instantly share code, notes, and snippets.

@davorbadrov
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davorbadrov/7314bfcf1f8706ba2bce to your computer and use it in GitHub Desktop.
Save davorbadrov/7314bfcf1f8706ba2bce to your computer and use it in GitHub Desktop.
Vertically center content using pseudo elements
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div class="container">
<div class="box">OMG! I'm centered!</div>
</div>
</body>
</html>
.container {
background: yellow;
display: block;
width: 300px;
height: 300px;
white-space: nowrap;
overflow: hidden;
}
.box {
width: 150px;
text-align: center;
}
.container:after {
content: '';
display: inline-block;
height: inherit;
vertical-align: middle;
width: 0;
}
.container .box {
display: inline-block;
white-space: normal;
width: 100%;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment