Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【CSS】水平垂直居中 #15

Open
Tracked by #6
swiftwind0405 opened this issue Feb 28, 2020 · 1 comment
Open
Tracked by #6

【CSS】水平垂直居中 #15

swiftwind0405 opened this issue Feb 28, 2020 · 1 comment
Labels

Comments

@swiftwind0405
Copy link
Owner

swiftwind0405 commented Feb 28, 2020

子元素不确定大小的情况

方法一:flex

.parent {
	display: flex;
    justify-content: center; // 主轴居中
    align-items: center;     // 交叉轴居中
}

缺点:flex 布局只支持现代浏览器。

方法二:absoulte + transform

.parent {
    position: relative;
}

.child {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

缺点:CSS3 属性只支持现代浏览器。

方法三:table-cell

.parent {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}

.child {
    display: inline-block; // 必须是行内元素
}

缺点:子元素必须是行内元素。不然只会垂直居中。table-cell 布局会引起一些不必要的麻烦。

@swiftwind0405 swiftwind0405 changed the title 好文收藏 非技术好文收藏 Feb 28, 2020
@swiftwind0405 swiftwind0405 changed the title 非技术好文收藏 【Day19】水平垂直居中 Mar 16, 2020
@swiftwind0405 swiftwind0405 changed the title 【Day19】水平垂直居中 【CSS】水平垂直居中 Apr 29, 2020
@swiftwind0405
Copy link
Owner Author

swiftwind0405 commented May 12, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant