mu *_*ort 11
你也可以劫持CSS display: table并display: table-cell为此目的.HTML将是这样的:
<body>
<div id="body">
<!-- Content goes here -->
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
而CSS:
html,
body {
width: 100%;
height: 100%;
}
html {
display: table;
}
body {
display: table-cell;
vertical-align: middle;
}
Run Code Online (Sandbox Code Playgroud)
如果你想要水平居中,那么添加:
#body {
max-width: 1000px; /* Or whatever makes sense for you. */
margin: 0 auto;
}
Run Code Online (Sandbox Code Playgroud)
有人会称之为滥用CSS但是:
参考文献:
MicroTut上有一篇很棒的文章可以做到这一点http://tutorialzine.com/2010/03/centering-div-vertically-and-horizontally/
以CSS为中心:
.className{
width:300px;
height:200px;
position:absolute;
left:50%;
top:50%;
margin:-100px 0 0 -150px;
}
Run Code Online (Sandbox Code Playgroud)
以JQuery为中心:
$(window).resize(function(){
$('.className').css({
position:'absolute',
left: ($(window).width() - $('.className').outerWidth())/2,
top: ($(window).height() - $('.className').outerHeight())/2
});
});
// To initially run the function:
$(window).resize();
Run Code Online (Sandbox Code Playgroud)
你可以在这里看到一个演示
| 归档时间: |
|
| 查看次数: |
19072 次 |
| 最近记录: |