Skip to content

Instantly share code, notes, and snippets.

@opi
Created May 7, 2015 16:23
Show Gist options
  • Save opi/0b6496d25674841601ba to your computer and use it in GitHub Desktop.
Save opi/0b6496d25674841601ba to your computer and use it in GitHub Desktop.
Vertical Centering
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<title>Vertical centering - 3WA</title>
<style>
/* border box ftw*/
*, *::before, *::after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
html, body {
/* le html & le body prennent la totalité de la hauteur dispo dans le navigateur */
height: 100%;
/* reset padding/margin */
margin: 0;
padding: 0;
}
body {
background: #eee;
color: #111;
padding: 3em;
}
main {
display: block;
background: #fff;
/* devient un referenciel pour des enfant en position:abolute */
position: relative;
/* fait *au-moins* la hauteur du parent */
min-height: 100%;
}
main > div {
background: #999;
/*
position:absolute + top/left/right/bottom:0 => l'element (ici le div) prend l'intégralité du parent (ici le main)
*/
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
/* dimensions (relative au parent) */
width: 50%;
height: 50%;
/* centrage automatique vertical et horizontal */
margin: auto;
/* dimensions minimales */
min-width: 15em;
min-height: 10em;
}
/** Debug **/
body::before,
main::before,
main > div::before {
content:"<body>";
position: absolute;
display: block;
top: 2px;left: 2px;
}
body::before {content:"<body>";}
main::before {content:"<main>";}
main > div::before {content:"<div>";}
</style>
</head>
<body>
<main>
<div></div>
</main>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment