Skip to content

Instantly share code, notes, and snippets.

@Hotell
Created September 16, 2014 18:39
Show Gist options
  • Save Hotell/d88ef664f340b0d3da94 to your computer and use it in GitHub Desktop.
Save Hotell/d88ef664f340b0d3da94 to your computer and use it in GitHub Desktop.
The Holy Grail of CSS Centering
<body>
<div class="container">
<!-- Centered inside whatever sized container -->
<div class="outer">
<div class="inner">
<div class="centered">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nam assumenda blanditiis.
</p>
<div class="positioned-absolutely">Foo</div>
</div>
</div>
</div>
</div>
</body>
html,
body {
height: 100%;
}
.container {
width: 70%;
height: 70%;
margin: 40px auto;
background: #f00;
}
.outer {
display: table;
width: 100%;
height: 100%;
}
.inner {
display: table-cell;
vertical-align: middle;
text-align: center;
}
.centered {
position: relative;
display: inline-block;
width: 50%;
padding: 1em;
background: #ffa500;
color: #fff;
}
.positioned-absolutely {
position: absolute;
top: -2em;
right: -2em;
background: #00f;
padding: 1em;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment