用CSS有多种方法实现垂直居中对齐。如果已知外部div的高度,不管是否知道内部div的高度,垂直居中实现起来很简单,但如果内部div高度是变量,如文字,垂直居中实现起来就比较复杂了,很可能需要使用hacks。如:
<div id="containingBlock">
<div><p>This sentence will change in each example</p> </div>
</div>
1、已知高度情况
很简单,直接计算就可以了
#containingBlock {display: block; height: 200px;}
#containingBlock div {height:50px; margin: 75px 0;}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbEAAADQCAIAAAAGbcitAAAGbUlEQVR4nO3cXW7yOACG0a6l+9/KLKUXSEiRokjMBS1vfuzESaEwwznyxZSSkDjmIfST5uMTgB8fzz4AgBeiiQChiQDx8fn5efn4MAzDePMxaeIzswzwbJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEJoIEPMmflwMwzDed1wKTXz6QRmGYTxl1Jr4BfBmNBEgNBEgNBEgNBEg9jfx1A2XoTv97WE+3e2s73X6Lfs58KJPuToHXvTu8/l0xRO5y9mtz9X/ZgJfxloT+8vS0J3f4xrMltrdV96uJr6a3x/YU5r40Nd63M418W813Cc+ug6vSRNXaOJf7lwT/9bRJna5iezP2d2pG3JHWblO535643ma7fN7f7VdFR5fPaSv1U0r7Rs98/rk5bo8NA+Fc2+fn9PXqRvGL3PuF7sYn874JIpvpcqutmZ4GGqTU9p4MZXT42w/tZUL2LQe6geytvnGaty4oG2rpXR+a3O1fIm+H+Y7LpxFbfrGm9T+u3CKTVO3ughf07Emji7puZ+sm+Jcjyy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QhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBQhMBYt5EwzCMNx9pIgBXmggQ/wLDzYFsgd2PpQAAAABJRU5ErkJggg==" alt="" />
2、通过display: table属性布局
通过使用 display: table;
和vertical-align: middle
可以比较轻松的实现垂直居中,但IE6和IE7并不识别table和table-cell属性,必须使用hacks作为补充。
#containingBlock {display: table; height: 200px; position: relative; overflow: hidden;}
#containingBlock div {display: table-cell; vertical-align: middle;}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAALUAAADOCAIAAAD31NT8AAAEYUlEQVR4nO3b0XHaSgBAUdeShiiHaqhFpVAK78M2sLB7wX6248mcM5tJbCOxKy6S8Exe/sDay9+eAL/ay+nFMJbj/Y9h3I3TuY+/fRrj19EHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR8UfVD0QdEHRR+UoY+Xl5NhDOOmj7/dK7+IPij6oOiDog/K9/exOxxPx8PuR7b62j38Rb9m8rM+9tswt93heDpt+8s2+2348oEfWOrucDyNPvGEX7KTrxIHbZjnt09xev4YAnmbz6WIj+XxU33876f4NW/ZP3/Wk9lvty/F6UMvxofNry9XgewOx+PhcBXMLJ4h5de1HbbTa1bXS90djucHXr8P7g/Feathb6f54Zgezfke3raezHvZx+Kxl4nst9UK7t/ouZz9dppskuu8fOt2z8NxWq736rBMJzzvY3c4vu//9WxxiWI/pnKexuXfr89znt5lBTcPvw7q7kUfXt2b12L94Gf2cPPw81IXfczXeLXdzdV46tLTejl3s1qv6IllXje7Xu/y5HOe8OL+9Hr1w9+7w3Hxhlsd6Lcvb17Y6eu8Wvb93iaruW1/tYfJ7dXV8V3t5GaN56kcn7soPTg4dwdkucz7Y/a+nOVUY73rqb9vvvr88vbzcw6Xr4d30Gypk+/PzpdXJ9PJND/cx6Pry9DHrffT66M9PzOZceObJ/nIQft/yxzLe269kwkvP9/ut9NpOwxni+Nhf/f+ee78cZvWVyz7s3tYXRCe7WNY/7atri7jszx1/nhY3hP3H+cfXs5HT653PuH17z9es4vPMXX/MV/q1eP32+UhswV8Yx9339jt9+vz7SfvP8a9jfcfDw/O6+M/+Pnl7i5jdaKfr3cx4fj92O2Hp/12fym4OiOt7q3u53087MazXn0k+YY+xnkvbjVyjcPhX3x+GS6g2/bo/DFscNy25ZVrmPrsOj47pk+tdzZhv1//Vzy4Gfokffwr9MHP0wdFH5RJH4YxDP+/gaAPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDog6IPij4o+qDogzL0YRj346QNo4crC0UflP8AW5JBsTW3xXoAAAAASUVORK5CYII=" alt="" />
IE6和IE7的CSS
#containingBlock {height: 200px; position: relative; overflow: hidden;}
#containingBlock div { position: absolute; top: 50%;}
#containingBlock div p {position: relative; top: -50%;}
显示为
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA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vDs8fsPuAe7tF2uXpawx+c3sk01RCaOf6GzW2c53HY7ASn4bY+Ub+wN9yHdOP0OuyP52aXv8Mabu/81N7wZ2u4n5UaQjMn3zesvduTa8vPfm94HIjV42/j8pCjKv3O5us27q+LnF69Wd7z8xpufgjrq0T/Mb087l/VEBo6+/b1bZwfftW3P43Prykf92vJxfo67MGW8Us1/Mr3DZfXPSvS+iP8OH60N9wechy/Nb2j/bMaQjP+Z17X1BCaUcOuqSE0o4ZdU0NoRg27pobQjBp2TQ2hmX0Nje6GGkIT87qGr54MB6wLtKGGvbMu0IYa9s66QBtq2DvrAm2oYe+sC7Shhr2zLtCGGvbOukAbatg76wJtqGHvrAu0oYa9sy7Qhhr2zrpAG2rYO+sCbahh76wLtKGGvbMu0IYa9s66QBtq2DvrAm2oYe+sC7Shhr2zLtDGpoZGn+PV/0jgT5jvNXz1TABeSQ0BEjUEKGoIkKghQFFDgEQNAYoaAiRqCFDUECBRQ4CihgCJGgIUNQRI1BCgqCFAooYARQ0BEjUEKGoIkKghQFFDgEQNAYoaAiRqCFDUECBRQ4CihgCJGgIUNQRI1BCgqCFAooYARQ0BEjUEKGoIkKghQFFDgEQNAYoaAiRqCFDUECBRQ4CihgCJGgIUNQRI1BCgqCFAooYARQ0BEjUEKGoIkKghQFFDgEQNAYoaAiRqCFDUECBZ19AwDOOPD7tCgCT5B9HApXLhzK05AAAAAElFTkSuQmCC" alt="" />
IE6和IE7的hack
//Vertical Alignment Table Display Hack
#containingBlock {display:table; height: 200px; position: relative; overflow: hidden; }
#containingBlock div {*position: absolute; top: 50%; display: table-cell; vertical-align: middle;}
#containingBlock p {*position: relative; top: -50%;}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAADNCAIAAAAZurEGAAAHfElEQVR4nO3d4XHiOACG4dRCQ5RDNa7FpVCK70cItmzJFoEkfHvPM5652ywBybZeFHZv7uMEEOLjrwcA0Otj+nA4HI53P+Zg/f1YHA6Ho31Mq2D93RYPYI9gATEE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AExBAuIIVhADMECYggWEEOwgBiCBcQQLCCGYAExBAuIIVhADMECYggWEEOwgBiCBcQQLCCGYAExBAuIIVhADMECYggWEEOwgBiCBcQQLCCGYAExBAuIIVhADMECYggWEEOwgBiCBcQQLCCGYAExBAuIIVhADMECYggWEEOwgBiCBcQQLCCGYAExBAuIIVhADMECYggWEEOwgBiCBcQQLCCGYAExBAuIIVhADMECYggWEEOwgBiCBcQQLCCGYAExBAuIUQmWw+FwvO0xLYPlcDgcb37cgvU3mzyAx/0HUU2ctT1LnOIAAAAASUVORK5CYII=" alt="" />
补充: 如果想实现流式布局,加入如下CSS
html, body, #containingBlock {height: 100%; position:relative; }
#containingBlock div {height: 50%; position: absolute; top: 25%; }
3. 垂直居中所用到的参数
值 |
描述 |
---|---|
length |
Raises or lower an element by the specified length. Negative values are allowed |
% |
Raises or lower an element in a percent of the “line-height” property. Negative values are allowed |
baseline |
Align the baseline of the element with the baseline of the parent element. This is default |
sub |
Aligns the element as it was subscript |
super |
Aligns the element as it was superscript |
top |
The top of the element is aligned with the top of the tallest element on the line |
text-top |
The top of the element is aligned with the top of the parent element’s font |
middle |
The element is placed in the middle of the parent element |
bottom |
The bottom of the element is aligned with the lowest element on the line |
text-bottom |
The bottom of the element is aligned with the bottom of the parent element’s font |
inherit |
Specifies that the value of the vertical-align property should be inherited from the parent element |
参考:
Vertical Centering With CSS