垂直居中
首先将<html>与<body>的高度设置为100%(为演示父元素不定宽高的效果),并清除<body>的默认样式。
1 2 3 4
| html,body{ margin: 0; height: 100%; }
|
垂直居中大致分为两类,父元素定宽高与父元素不定宽高,将两类样式以及子容器设定好。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| .set-parent,.dy-parent{ width: 300px; height: 200px; background: #eee; margin: 10px 0; } .child{ width: 20px; height: 10px; background: #fff; } .dy-parent{ width: 30%; height: 20%; }
|
父元素定宽高 position+margin
使用margin: auto使水平居中,将子容器设定为relative以在不脱离文档流的情况下偏移50%,由于本身子容器占用一定宽高,会将其撑下,使用margin使其向上偏移。
若是子容器使用absolute定位,则父容器应设置为relative,否则会其相对于static定位以外的第一个父元素进行定位,在本示例中会以浏览器为基准定位,此外absolute无法使用margin: auto水平居中。
1 2 3 4
| <div class="set-parent" > <div class="child" style="position: relative;top: 50%;margin: auto;margin-top: -5px;"></div> </div>
|
原理与position+margin相同,CSS3的transform使得div向上平移自身高度的50%。
1 2 3 4
| <div class="set-parent" > <div class="child" style="position: relative;top: 50%;margin: auto;transform: translateY(-50%);"></div> </div>
|
父元素定宽高 position+calc
css3提供calc函数,能够进行动态计算。
1 2 3
| <div class="set-parent" > <div class="child" style="position: relative;top: calc(50% - 5px);left: calc(50% - 10px);"></div> </div>
|
父元素不定宽高 flex
flex布局可以说是布局神器,极其强大,绝大部分现代浏览器都兼容性flex布局。
1 2 3
| <div class="dy-parent" style="display: flex;justify-content: center;align-items: center;"> <div class="child" ></div> </div>
|
父元素不定宽高 grid
grid布局将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,grid布局与flex布局有一定的相似性,都可以指定容器内部多个项目的位置。但是,它们也存在重大区别,flex布局是轴线布局,只能指定”项目”针对轴线的位置,可以看作是一维布局,grid布局则是将容器划分成”行”和”列”,产生单元格,然后指定”项目”所在的单元格,可以看作是二维布局,可以认为grid布局比flex布局强大。
1 2 3
| <div class="dy-parent" style="display: grid;justify-content: center;align-content: center;"> <div class="child" ></div> </div>
|
父元素不定宽高 table
table中有vertical-align属性设置垂直对齐方式。
1 2 3 4 5
| <div class="dy-parent" style="display: table;"> <div style="display: table-cell;vertical-align: middle;"> <div class="child" style="margin: auto;" ></div> </div> </div>
|
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| <!DOCTYPE html> <html> <head> <title>垂直居中</title> <meta charset="utf-8"> </head> <body>
<div class="set-parent" > <div class="child" style="position: relative;top: 50%;margin: auto;margin-top: -5px;"></div> </div>
<div class="set-parent" > <div class="child" style="position: relative;top: 50%;margin: auto;transform: translateY(-50%);"></div> </div>
<div class="set-parent" > <div class="child" style="position: relative;top: calc(50% - 5px);left: calc(50% - 10px);"></div> </div>
<div class="dy-parent" style="display: flex;justify-content: center;align-items: center;"> <div class="child" ></div> </div>
<div class="dy-parent" style="display: grid;justify-content: center;align-content: center;"> <div class="child" ></div> </div>
<div class="dy-parent" style="display: table;"> <div style="display: table-cell;vertical-align: middle;"> <div class="child" style="margin: auto;" ></div> </div> </div>
</body> <style type="text/css"> html,body{ margin: 0; height: 100%; } .set-parent,.dy-parent{ width: 300px; height: 200px; background: #eee; margin: 10px 0; } .child{ width: 20px; height: 10px; background: #fff; } .dy-parent{ width: 30%; height: 20%; } </style> </html>
|