将子div与响应式网站中的父div垂直对齐

时间:2014-06-02 14:30:49

标签: html css vertical-alignment

我想将子div垂直对齐到父div的底部。这是一个响应式网站,所以我知道有一些方法可以使用表格,但似乎没有一个方法可以为我提供一个确切的答案,可以在现代浏览器(包括iOS)中以正确的方式执行。任何帮助都会很棒!感谢

#parent {
width: 32%;
height: 242px;
background-color: red;
}

#child {
width: 100%;
height: 57px;
background-color: green;
}

#text {
background-color: yellow;
width: 100%;
height: 40px;
}


<div class='box' id='parent'>
    <div class='box' id='child'>
        <div class='box' id='text'></div>
    </div>
</div>

4 个答案:

答案 0 :(得分:2)

您可以使用:解决后。

<强> HTML

<div class='box' id='parent'>
    <div class='box' id='child'>
        <div class='box' id='text'></div>
    </div>
</div>

<强> CSS

#parent {
    width: 32%;
    height: 242px;
    background-color: red;
    position: relative;
    white-space: nowrap;
}
#parent:after {
    content:'';
    width: 1px;
    height: 100%;
    display: inline-block;
    vertical-align: bottom;
}
#child {
    width: 100%;
    height: 57px;
    background-color: green;
    white-space: normal;
    display: inline-block;
    vertical-align: bottom;
}

#text {
    background-color: yellow;
    width: 100%;
    height: 40px;
}

这里是DEMO

答案 1 :(得分:1)

父母的

position:relative;和孩子的position:absolute; bottom:0;

jsFiddle

#parent {
    position: relative;    
width: 32%;
height: 242px;
background-color: red;
}

#child {
    position: absolute;
    bottom:0;
width: 100%;
height: 57px;
background-color: green;
}

答案 2 :(得分:0)

为家长position: relative;和孩子position: absolute以及bottom: 0;

#parent {
    position: relative;
    width: 32%;
    height: 242px;
    background-color: red;
}

#child {
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 57px;
    background-color: green;
}

实施例: http://jsfiddle.net/skeurentjes/rLhdc/

答案 3 :(得分:0)

我有一个整洁的方式垂直(和水平,如果需要)对齐父母总是响应。子元素不需要明确的宽度或高度,因此如果需要,它也可以缩放。缺点是你必须在dom中添加一个额外的元素。我已经使用它多年了,甚至可以通过一点点黑客攻击IE 5.5。

.parent {
    width: 50%;
    height: 500px;          /* height is needed */
    text-align: center;     /* adjust to suit */
    font-size: 0;
}
.parent > * {
    *display: inline;       /* fix for IE 7 and below */ 
    display: inline-block;
    vertical-align: bottom; /* adjust to suit */
    white-space: nowrap;
}
.align {
    width: 0;
    height: 100%;
}
.child {
    max-width: 100%;
    font-size: 1rem;        /* remember to set font-size on html tag */
}

和标记:

<div class="parent">
    <span class="align"></span>
    <img class="child" src="http://placehold.it/150x150" />
</div>
相关问题