html - CSS 中垂直对齐的问题

标签 html css vertical-alignment

简而言之,我无法让我的内容 div 在页面内垂直对齐 - 有人告诉我,为了让它工作,我的容器必须有一个不是自动的高度值,所以我去修复了它,但它仍然无法正常工作,有人可以帮助我吗?

HTML

<div class="units-container" style="overflow: hidden;">
    <div id="home" class="section">
        <div id="home-content">
            <section id="intro">
                <section>
                    <h3>Lorem Ipsum Dolor Amet (and some other latin things too)</h3>
                </section>
        </div>
    </div>
    <div id="portfolio"></div>
    <div id="about"></div>
    <div id="blog"></div>
    <div id="contact"></div>
</div>

CSS

#home {
    background-color: #1a1a1a;
    color: #fff;
}
.section {
    padding: 0;
    margin: 0;
    display: block;
    overflow: hidden;
}
#home #home-content {
    vertical-align: middle;
    text-align: center;
}
#intro {
    width: 60%;
    margin: 0 auto;
}
#home-content h3 {
    font-family:'amblelight_condensed', 'Courier';
    display: block;
    color: #fff;
    font-size: 2.17em;
    line-height: normal;
}

JQuery(如果需要)

jQuery(document).ready(function ($) {

    /*--------------------------
    Resize Div(s) when Window Resizes
    ---------------------------*/

    $(".section").css("height", $(window).height());
    $(".section").css("width", $(window).width());
    $(window).resize(function () {
        $('.section').css('height', $(window).height());
        $(".section").css("width", $(window).width());
    });
});

http://jsfiddle.net/sWeLw/

最佳答案

相对于您的样本。添加以下 CSS:

#home, #home-content {position:relative;}

和JS:

$('#home-content').css("top", ($('#home').height() - $('#home-content').height()) / 2);

关于html - CSS 中垂直对齐的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18169121/

相关文章:

javascript - jQuery,形式上的变量数学

HTML 按钮列表悬停错误

css - DIV 中的垂直和水平对齐

CSS vertical-align 不适用于以百分比表示的尺寸

jquery - 使用 jQuery 的图像标题

javascript - 倾斜后 DIV 的位移

javascript - 无法使用 JavaScript 更新网站图标以使其旋转

javascript - jQuery:即使有滚动,如何使对话框保持在右上角?

javascript - 如何在包含背景的顶部放置具有宽度,高度的div?

html - 如何使按钮和输入保持在表格单元格内的同一行?