2014-06-24 71 views
0

我一直在玩这个片刻,但似乎无法弄清楚如何正确设置它。设置垂直填充家长

鉴于此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%; 
} 

我本来以为这是因为我是动态地添加HTML内容与jQuery,但已经建立了一个的jsfiddle似乎后我也错了。

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

的小提琴是在这里:

http://jsfiddle.net/

+0

不知道这是你的意思,但你有没有尝试添加'明确:两者;'在CSS'。用户瞩目,card'?你可以添加'margin:5px auto;'来居中它们。 – invot

+0

“正确垂直拉伸”是什么意思?你想要每个'.user-profile-card'来填充窗口的高度? – showdev

+0

这不起作用,它将它们堆叠在彼此之上而不是并排。 – Jammer

回答

0

只需添加display:table#myfriends

jsFiddle example

+0

Ahhh,适用于Chrome,但不适用于FireFox – Jammer

+0

应该在Firefox中工作,但IE <8不支持。 http://caniuse.com/css-table – showdev

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%; 
} 
+0

我同意你的结论。但我相信OP希望所有'user-profile-card'元素的高度相同 - 最高的高度。但不是整个窗口的高度。虽然可能是错的。 – showdev

+0

@showdev在这种情况下,我的错误,让我想办法解决这个问题。 –