【问题标题】:Div content alignment query [duplicate]div内容对齐查询[重复]
【发布时间】:2012-01-22 08:14:40
【问题描述】:

可能重复:
Vertically align div (no tables)

我有一个div,我想对齐这个div 中的所有内容 - 目前是一个h1 标签垂直居中我该怎么做?

HTML:

<div>
   <h1>Content</h1>
</div>

CSS:

div{
  width:100%;
}
div h1{
  margin:0 auto;
}

【问题讨论】:

标签: css html alignment vertical-alignment


【解决方案1】:

我认为没有跨浏览器的解决方案可以让你的 DIV 容器垂直居中,但是有一个 jQuery 插件可以让你这样做:

(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
return this.each(function(i){
var ah = $(this).height();
var ph = $(this).parent().height();
var mh = (ph - ah) / 2;
$(this).css('margin-top', mh);
});
};
})(jQuery);

然后你可以添加这个Javascript:

$(document).ready(function() {
$('div h1').vAlign();
});

【讨论】:

    猜你喜欢
    • 2016-05-04
    • 1970-01-01
    • 2010-10-15
    • 2021-09-22
    • 1970-01-01
    • 2020-11-06
    • 2016-12-20
    • 2012-07-30
    相关资源
    最近更新 更多