【问题标题】:How to vertically center my container div如何垂直居中我的容器 div
【发布时间】:2014-10-18 16:50:20
【问题描述】:

我可以将元素水平居中,但不能垂直居中。我在 stackoverflow 上发现了几个关于同一问题的问题,但其中的建议由于某种原因不起作用。有什么建议?这就是我想要的:http://i62.tinypic.com/n4gzcw.png

这是我目前所拥有的:

    <style>

.container {
    width: 100%;
    height: 100%;
    background-color: red;

    position: absolute;
    top:0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
}

.datetime {
    width: 50%;
    margin-left: auto; 
    margin-right: auto;
    margin-top: 15%;
    background-color: white;
    text-align: center;
}

.main {
    width: 100%;
    margin-left: auto; 
    margin-right: auto;
    margin-top: 3%;
    background-color: yellow;
    text-align: center;
}

.left {
    width: 25%;
    float: left;
}

.left img {
    width: 50%;
    height: 50%;
    max-width: 80px;
    max-height: 80px;
}

.mid {
    width: 50%;
    text-align: center;
    float: left;
    padding-top: 5%;
    font-size: 1em;
}

.right {
    width: 25%;
    float: left;
}

.right img {
    width: 50%;
    height: 50%;
    max-width: 80px;
    max-height: 80px;
}

.stadium {
    width: 50%;
    margin-left: auto; 
    margin-right: auto;
    background-color: white;
    text-align: center;    
}
</style>


<div class="container">
    <div class="datetime">26 August 2014<br />12:00pm</div>
    <div class="main">
        <div class="left"><img src="http://dejaniskra.comze.com/ManchesterUnitedUpcomingFixture/logos/mkdons.png" /></div>
        <div class="mid">MK Dons vs Manchester United</div>
        <div class="right"><img src="http://dejaniskra.comze.com/ManchesterUnitedUpcomingFixture/logos/manchesterunited.png" /></div>
    </div>
    <div class="stadium">Stadium:mk - Milton Keynes</div>
</div>

【问题讨论】:

    标签: html css


    【解决方案1】:

    您可以使用 .container-inner 包装您的内容并添加样式:

    .container-inner{
        height: 150px;
        position: absolute;
        top: 50%;
        margin-top: -75px;
        width: 100%;
    }
    

    您可以在此处找到工作示例:

    http://jsfiddle.net/Lkfdxm13/

    【讨论】:

      【解决方案2】:

      创建部分 CSS 布局,然后尝试将其调整为其他策略可能会充满问题。我的建议是创建一组只有背景颜色的干净 html 包装器,并使用您找到的解决方案之一,例如How to verticaly align into the center of the content of a div with defined width/height?。有了工作布局后,您就可以将内容移入。
      如果您遇到无法解决的问题,则可以发布特定的 CSS 问题,答案可能会更多。

      【讨论】:

        猜你喜欢
        • 2012-06-03
        • 1970-01-01
        • 2013-03-18
        • 2015-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-28
        相关资源
        最近更新 更多