Skip to content

Instantly share code, notes, and snippets.

@jmwhittaker
Created July 18, 2012 12:09
Show Gist options
  • Select an option

  • Save jmwhittaker/3135820 to your computer and use it in GitHub Desktop.

Select an option

Save jmwhittaker/3135820 to your computer and use it in GitHub Desktop.
Vertical CSS3 centering with inline-block
<div class="block">
<div class="centered">Hello world</div>
</div>
/* This parent can be any width and height */
.block {
position:absolute;
bottom:0;
top:0;
left:0;
right:0;
text-align:center;
background:red;
overflow-y:scroll;
}
/* The ghost, nudged to maintain perfect centering */
.block:before {
text-align:center;
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; /* Adjusts for spacing */
}
/* The element to be centered, can
also be of any width and height */
.centered {
background:white;
display: inline-block;
vertical-align: middle;
width: 300px;
min-height:400px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment