为什么div不能水平垂直居中?

3
我正在尝试将徽标水平垂直居中。我已经有了以下代码...
<script type="text/javascript">
    $(document).ready(function(){
        $(window).resize(function(){
            $('#theLogo').css({
                position:'absolute',
                left: ($(window).width() - $('#theLogo').outerWidth())/2,
                top: ($(window).height() - $('#theLogo').outerHeight())/2
            });
        });
        $(window).resize();
    });
</script>

HTML

<div id="theLogo">
    <section id="responsiveLogo" class="logo">September</section>
</div>

我正在尝试将这部分居中显示:

<section id="responsiveLogo" class="logo">September</section>

Live版本可以在http://septemberstudio.co.uk/上查看。


2
  1. 对我来说它是居中的。
  2. 因为#responsiveLogo的宽度是100%?
- h2ooooooo
@h2ooooooo 它垂直居中了吗? - Reinstate Monica Cellio
@Archer 啊不是的 - 当然,这就是 OP 正在尝试做的事情。 - h2ooooooo
我在你的“实时版本”中找不到你的脚本……你确定你放进去了吗? - Oliboy50
@Oliboy50,抱歉,现在可以了。 - user2898276
11个回答

-3

为什么不能简单地使用...

#responsiveLogo {
    text-align: center;
}

2
它可能是因为它没有垂直对齐的原因吧? - Reinstate Monica Cellio
我明白了,那在“浏览器中间”这个要求上并不清楚。 - Chris Pickford
2
是的,这并不清楚,但尝试使用lefttop来定位元素使其变得更加清晰了 ;) - Reinstate Monica Cellio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接