Skip to content

Instantly share code, notes, and snippets.

@kmosher
Last active April 8, 2016 05:22
Show Gist options
  • Save kmosher/c549934625ba1323539f69f7d0b7ff7f to your computer and use it in GitHub Desktop.
Save kmosher/c549934625ba1323539f69f7d0b7ff7f to your computer and use it in GitHub Desktop.
Positioning relative to perfect center with flexbox
body {
width: 100%;
height: 100%;
margin: 0;
box-sizing: border-box;
display: flex;
flex-direction: column;
justify-content: center;
align-content: center;
}
body::after {
content:"";
position: absolute;
z-index: -1;
top: 0;
bottom: 0;
left: 50%;
margin-left: -1px;
border-left: 2px dashed red;
}
body::before {
content:"";
position: absolute;
z-index: -1;
left: 0;
right: 0;
top: 50%;
margin-top: -1px;
border-top: 2px dashed red;
}
.flex-filler {
flex: 1;
}
.centered-thing {
font-size: 50px;
}
.underthing {
font-size: 20px;
justify-self: start;
}
div {
border-color: grey;
border-style: dotted;
margin: 2;
}
p {
border-color: lime;
border-style: dotted;
text-align: center;
color: #000;
padding: 10;
margin: 5;
font-family: "Helvetica Neue", "Helvetica", "Arial", sans-serif;
}
html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="spacer flex-filler"> </div>
<div class="centered-thing">
<p>My div is perfectly centered.</p>
<p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nos\
trud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla p\
ariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p>
</div>
<div class="underthing flex-filler">
<p>I'm directly underneath</p>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment