美文网首页
CSS: 居中

CSS: 居中

作者: 写代码的海怪 | 来源:发表于2019-01-28 02:17 被阅读0次

水平居中

flex 布局

.parent {
    display: flex;
    justify-content: center;
}

或者

.parent {
    display: flex;
    flex-direction: column;
    align-items: center;
}

内联元素

.parent {
    text-align: center;
}

margin

.children {
    margin: 0 auto;
}

position

.parent {
    position: relative;
}
.children {
    position: absolute;
    left: 50%;
    top: 0;
    transform: translateX(-50%);
}

table

过时,放弃

垂直居中

flex 布局

.parent {
    display: flex;
    align-items: center;
}

或者

.parent {
    display: flex;
    flex-direction: column;
    justify-content: center;
}

内联元素

.parent {
  height: 100px;
  line-height: 100px;
}

margin

必须设置父元素高度。

.parent {
    height: 100px;
}

.children {
    margin: auto 0;
}

position

.parent {
    position: relative;
}

.children {
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
}

相关文章

网友评论

      本文标题:CSS: 居中

      本文链接:https://www.haomeiwen.com/subject/dasojqtx.html