CSS垂直居中 Posted on 2019-03-12 In HTML5 HTML 框架123456789101112131415<style> #box { width: 300px; height: 300px; background-color: #ddd; } #child { width: 200px; height: 100px; background-color: orange; }</style><div id="box"> <div id="child">target</div></div> 绝对定位 + 负外边距 112345678#box { position: relative;}#child { position: absolute; top: 50%; margin: -50px 0 0 0;} 缺点:需要知道目标元素的高度 绝对定位 + 负外边距 2123456789#box { position: relative;}#child { height: 30%; position: absolute; top: 50%; margin: -15% 0 0 0;} 绝对定位 + transform12345678#box { position: relative;}#child { position: absolute; top: 50%; transform: translate(0, -50%);} 绝对定位 + margin: auto12345678910#box { position: relative;}#child { position: absolute; top: 0; bottom: 0; margin: auto; line-height: 100px;} flex1234#box { display: flex; align-items: center;}