Skip to content

Instantly share code, notes, and snippets.

@gabrieljoelc
Last active December 30, 2015 04:39
Show Gist options
  • Save gabrieljoelc/7777185 to your computer and use it in GitHub Desktop.
Save gabrieljoelc/7777185 to your computer and use it in GitHub Desktop.
Pretty solid way to center vertically with HTML/CSS. From http://css-tricks.com/centering-in-the-unknown/
.something-semantic {
display: table;
width: 100%;
}
.something-else-semantic {
display: table-cell;
text-align: center;
vertical-align: middle;
}
<div class="something-semantic">
<div class="something-else-semantic">
Unknown stuff to be centered.
</div>
</div>
/* This parent can be any width and height */
.block {
text-align: center;
}
/* The ghost, nudged to maintain perfect centering */
.block:before {
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 {
display: inline-block;
vertical-align: middle;
width: 300px;
}
<div class="block" style="height: 300px;">
<div class="centered">
<h1>Some text</h1>
<p>But he stole up to us again, and suddenly clapping his hand on my shoulder, said&mdash;"Did ye see anything looking like men going towards that ship a while ago?"</p>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment