Skip to content

Latest commit

 

History

History
31 lines (26 loc) · 679 Bytes

Element-middle.md

File metadata and controls

31 lines (26 loc) · 679 Bytes

Element in the middle(元素居中)


margin:auto中

.element {
    width: 600px; height: 400px;
    position: absolute; left: 0; top: 0; right: 0; bottom: 0;
    margin: auto;    /* 有了这个就自动居中了 */
}

绝对定位元素的居中实现

/* 兼容 */
.element {
    width: 600px; height: 400px;
    position: absolute; left: 50%; top: 50%;
    margin-top: -200px;    /* 高度的一半 */
    margin-left: -300px;    /* 宽度的一半 */
}

/* css3 实现 */
.element {
    width: 600px; height: 400px;
    position: absolute; left: 50%; top: 50%;
    transform: translate(-50%, -50%);    /* 50%为自身尺寸的一半 */
}