CSS垂直居中 发表于 2018-04-15 分类于 前端 1. 行内元素123456.parent { height: X; /* X为一个绝对高度 */}.child { line-hight: X;} 2. Table1234567.parent { display: table;}.child { display: table-cell; vertical-align: middle;} 3. 绝对定位(left+height)12345678910.parent { position: releative;}.child { position: absolute; height: X; /* X为一个绝对高度 */ top: 50%; margin-top: -0.5*X} 4. 绝对定位(transform)123456789.parent { position: releative;}.child { position: absolute; top: 50%; transform: translate(0, -50%);} 5. 绝对定位(top+bottom)12345678910.parent { position: releative;}.child { position: absolute; top: 0; bottom: 0; margin: auto 0;} 6. Flex布局1234.parent { display: flex; align-items: center;} 7. Grid布局1234.parent { display: grid; align-items: center;}