inline或者inline-*元素css
.center-children { text-align: center; }
inline、inline-block、inline-table、inline-flex等有效flex
一個塊級元素flexbox
.center{ margin:0 auto; }
使用inline-block和text-align配合或者使用flexcode
inline或者inline-*元素
一、單行:上下添加相等的內邊距orm
.link { padding-top: 30px; padding-bottom: 30px; }
二、多行:it
// 能夠模擬表格 .center-table{ display:table; } .center-table children{ display:table-cell; vertical-align:center; }
// 可使用flexbox .flex-center-vertically { display: flex; justify-content: center; flex-direction: column; height: 400px; }
塊級元素io
知道塊級元素的高度table
.parent { position: relative; } .child { position: absolute; top: 50%; height: 100px; margin-top: -50px; box-sizing: border-box; */ }
不知道塊級元素的高度form
.parent { position: relative; } .child { position: absolute; top: 50%; transform: translateY(-50%); }
使用flexboxclass
.parent { display: flex; flex-direction: column; justify-content: center; }
固定寬度和高度
.parent { position: relative; } .child { width: 300px; height: 100px; padding: 20px; position: absolute; top: 50%; left: 50%; margin: -70px 0 0 -170px; }
不知道寬度和高度
.parent { position: relative; } .child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
使用flexbox
.parent { display: flex; justify-content: center; align-items: center; }