We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
[TOC]
以下布局均以不定宽为前提,定宽情况包含其中
.parent { text-align: center; } .child { display: inline-block; }
tips: 此方案兼容性较好,可兼容至 IE8,对于 IE567 并不支持 inline-block,需要使用 css hack 进行兼容。
.child { display: table; margin: 0 auto; }
tips: 此方案兼容至 IE8,可以使用<table/>代替 css 写法,兼容性良好。
<table/>
.parent { position: relative; height: 1.5em; } .child { position: absolute; left: 50%; transform: translateX(-50%); }
tips: 此方案兼容至 IE9,因为 transform 兼容性限制,如果.child为定宽元素,可以使用以下写法,兼容性极佳。
.child
.parent { position: relative; height: 1.5em; } .child { position: absolute; width: 100px; left: 50%; margin-left: -50px; }
.parent { display: flex; justify-content: center; } .child { margin: 0 auto; }
tips: 移动端兼容性良好
.parent { display: table-cell; vertical-align: middle; }
tips: 可替换为 <table/> 布局,兼容性良好。
.parent { position: relative; } .child { position: absolute; top: 50%; transform: translateY(-50%); }
tips: 存在 css3 兼容性问题,定宽兼容性良好。
.parent { display: flex; align-items: center; }
.parent { text-align: center; display: table-cell; vertical-align: middle; } .child { display: inline-block; }
tips: 兼容至 IE8
.parent { position: relative; } .child { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); }
.parent { display: flex; justify-content: center; align-items: center; }
.left { float: left; width: 100px; } .right { margin-left: 120px; }
tips: 此方案对于定宽布局比较好,不定宽布局推荐方案 1.2
.left { float: left; width: 100px; margin-right: 20px; } .right { overflow: hidden; }
tips: 兼容性极好,此方案不管是多列定宽或是不定宽都可以完美实现,且可以实现等高布局。
.parent { display: table; width: 100%; table-layout: fixed; } .left, .right { display: table-cell; } .left { width: 100px; padding-right: 20px; }
.parent { display: flex; } .left { width: 100px; padding-right: 20px; } .right { flex: 1; }
.left, .center { float: left; width: 100px; margin-right: 20px; } .right { overflow: hidden; }
.parent { display: table; width: 100%; table-layout: fixed; } .left, .center, .right { display: table-cell; } .right { width: 100px; padding-right: 20px; }
.parent { display: flex; } .left, .center { width: 100px; padding-right: 20px; } .right { flex: 1; }
.left { float: left; margin-right: 20px; } .right { overflow: hidden; } .left p { width: 200px; }
.parent { display: table; width: 100%; } .left, .right { display: table-cell; } .left { width: 0.1%; padding-right: 20px; } .left p { width: 200px; }
.parent { display: flex; } .left { margin-right: 20px; } .right { flex: 1; } .left p { width: 200px; }
.left, .center { float: left; margin-right: 20px; } .right { overflow: hidden; } .left p, .center p { width: 100px; }
.parent { margin-left: -20px; } .column { float: left; width: 25%; padding-left: 20px; box-sizing: border-box; }
.parent-fix { margin-left: -20px; } .parent { display: table; width: 100%; table-layout: fixed; } .column { display: table-cell; padding-left: 20px; }
.parent { display: flex; } .column { flex: 1; } .column + .column { margin-left: 20px; }
.parent { overflow: hidden; } .left, .right { padding-bottom: 9999px; margin-bottom: -9999px; } .left { float: left; width: 100px; } .right { overflow: hidden; }
.parent { display: table; width: 100%; } .left { display: table-cell; width: 100px; margin-right: 20px; } .right { display: table-cell; }
.parent { display: flex; width: 100%; } .left { width: 100px; } .right { flex: 1; }
.main { display: flex; flex-flow: row wrap; justify-content: space-between; } .item { display: inline-block; } .empty { height: 0; visibility: hidden; }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
【布局】CSS 布局方案
[TOC]
居中布局
1、水平居中
1.1 inline-block + text-align
1.2 table + margin
1.3 absolute + transform
1.4 flex + justify-content
2.垂直居中
2.1 table-cell + vertical-align
2.2 absolute + transform
2.3 flex + align-items
3. 水平垂直居中
3.1 inline-block + table-cell + text-align + vertical-align
3.2 absolute + transform
3.3 flex
多列布局
1. 一列定宽,一列自适应
1.1 float + margin
1.2 float + overflow
1.3 table
1.4 flex
2. 多列定宽,一列自适应
2.1 float + overflow
2.2 table
2.3 flex
3. 一列不定宽,一列自适应
3.1 float + overflow
3.2 table
3.3 flex
4. 多列不定宽,一列自适应
4.1 float + overflow
5. 等分
5.1 float + margin
5.2 table + margin
5.3 flex
6. 等高
6.1 float + overflow
6.2 table
6.3 flex
并排等分,单排对其靠左布局
flex
圣杯布局&双飞翼布局
参考文档
The text was updated successfully, but these errors were encountered: