Created
July 18, 2012 12:09
-
-
Save jmwhittaker/3135820 to your computer and use it in GitHub Desktop.
Vertical CSS3 centering with inline-block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div class="block"> | |
| <div class="centered">Hello world</div> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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