Vertically-Center Anything
No, it’s not black magic, you really can center elements vertically. You can do this with flexbox…
html,
body {
height: 100%;
margin: 0;
}
body {
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
display: -webkit-flex;
display: flex;
}
…and also with CSS Grid:
body {
display: grid;
height: 100vh;
margin: 0;
place-items: center center;
}
Want to center something else? Vertically, horizontally…anything, anytime, anywhere? CSS-Tricks has a nice write-up on doing all of that.
Note: Watch for some buggy behavior with flexbox in IE11.
Demo
当前内容版权归 AllThingsSmitty 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 AllThingsSmitty .