html - Vertically centering with flexbox -
i'm trying center div on webpage using flexbox. i'm setting following css properties. see it's being centered horizontally, not vertically.
.flex-container { display: flex; align-items: center; justify-content: center; }
here's fiddle: jsfiddle
can explain i'm doing wrong?
a <div>
element without explicit height defaults height of it's contents, block elements do. you'd want set 100% of it's parent, <body>
, that's not enough, since block element. again, need set 100% height, match it's parent, <html>
. , yet again, 100% still required.
but once done, annoying vertical scroll bar. that's result of default margin body has, , way box model defined. have several ways can combat that, easiest set margins 0.
see corrected fiddle.
html, body { height: 100%; margin: 0px; } .flex-container { display: flex; align-items: center; justify-content: center; height: 100%; } .item { background-color: blue; height: 50px; width: 50px; }
<div class="flex-container"> <div class="item"> </div> </div>
Comments
Post a Comment