将Div设置为垂直填充父级

时间:2014-06-24 18:27:03

标签: jquery html css

我现在已经玩了一段时间,但似乎无法弄清楚如何正确设置它。

鉴于此HTML:

<div class="item-container bottom-spacer">
        <div id="my-friends"><div id="2" class="user-profile-card">
        <img src="">

        <div><h3>John</h3></div>
        <h3>Details</h3><br>
        Date: Tuesday, June 24th 2014, 11:47 am<br>
        Val1: 22<br>
        Val2: 28<br>
        <div>
            <h3>Notes</h3><br>
            Just some notes...
        </div>
        <div class="user-profile-li-buttons"><button class="delete-button" onclick="friendsPage.deleteFriend(2)">X</button></div>

</div><div id="4" class="user-profile-card">
        <img src="">

        <div><h3>Mr User One</h3></div>
        <h3>Details</h3><br>
        <div>
            <h3>Notes</h3><br>
            Just some notes...
        </div>
        <div class="user-profile-li-buttons"><button class="delete-button" onclick="friendsPage.deleteFriend(4)">X</button></div>
</div><div id="6" class="user-profile-card">
        <img src="">

        <div><h3>Mr User Two</h3></div>
        <h3>Details</h3><br>
        Date: Tuesday, June 24th 2014, 6:30 pm<br>
        Val1: 31<br>
        Val2: 20<br>
        <div>
            <h3>Notes</h3><br>
            Just some notes...
        </div>
        <div class="user-profile-li-buttons"><button class="delete-button" onclick="friendsPage.deleteFriend(6)">X</button></div>
</div></div>
        <div class="clear-fix"></div>
    </div>

还有一些CSS:

.item-container {
    padding: 20px;
    border: 0 #fff solid;
    background: #ccc;
    box-sizing: border-box;
    -ms-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    border-radius: 0.3ex;
    -webkit-border-radius: 0.3ex;
    -moz-border-radius: 0.3ex;
    overflow: auto;
    height:100%;
}

#my-friends {
    height: inherit;
}

.user-profile-card {
    overflow: hidden;
    background: #333;
    color: #fff;
    border-radius: 0.3ex;
    -webkit-border-radius: 0.3ex;
    -moz-border-radius: 0.3ex;
    margin: 5px;
    float: left;
    font-size: 0.8em;
    width: 160px;
    padding: 5px;
    height: 100%;
}

我原本以为是因为我是用jQuery动态添加HTML内容但是在构建了一个JsFiddle后,我似乎也错了。

如何让这些内部div正确地垂直拉伸以填充父div?

小提琴在这里:

http://jsfiddle.net/

2 个答案:

答案 0 :(得分:0)

只需将display:table添加到#myfriends

即可

<强> jsFiddle example

答案 1 :(得分:0)

高度100%仅在父元素应用了高度时才有效。

因此您需要将高度应用于html,body元素。

<强> Fiddle Demo

html,body{
    height:100%
}
.item-container {
    padding: 20px;
    border: 0 #fff solid;
    background: #ccc;
    box-sizing: border-box;
    -ms-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    border-radius: 0.3ex;
    -webkit-border-radius: 0.3ex;
    -moz-border-radius: 0.3ex;
    overflow: auto;
    height:100%;
}
#my-friends {
    height: inherit;
}
.user-profile-card {
    overflow: hidden;
    background: #333;
    color: #fff;
    border-radius: 0.3ex;
    -webkit-border-radius: 0.3ex;
    -moz-border-radius: 0.3ex;
    margin: 5px;
    float: left;
    font-size: 0.8em;
    width: 160px;
    padding: 5px;
    height: 100%;
}