CSS水平居中 发表于 2018-04-07 分类于 前端 1. 行内元素123.parent { text-align: center;} 2. 块级元素123div { margin: 0 auto;} 3. 绝对定位(left+width)12345678910.parent { position: releative;}.child { position: absolute; width: X; /* X为一个绝对宽度 */ left: 50%; margin-right: -0.5*X} 4. 绝对定位(transform)123456789.parent { position: releative;}.child { position: absolute; left: 50%; transform: translate(-50%, 0);} 5. Flex布局1234.parent { display: flex; justify-content: center;} 6. Grid布局1234.parent { display: grid; justify-items: center;}