我有一個父div,它可以垂直擴展和縮小。裏面有兩個孩子的div。我想根據父div的高度來定位孩子:如何將兩個div放在水平位置,如果父div中沒有足夠的空間
vertical:如果父div的高度允許兩個孩子垂直放置。
- 父------------------------------------------- --------------------------------------
| -child1 ------- ----- |
| ---------------------- |
| -child2 --------------------------------- |
| ------------------------------------------- |
|
|
| ----------------------------------------------- ------------------------------------------
- 父------------------------------------------- --------------------------------------
horizontal :如果父母的身高沒有足夠的空間讓孩子垂直放置。
- 父------------------------------------------- --------------------------------------
| -child1 ------- ------- || -child2 --------------------------------- |
| ------------------------ || --------------------- ---------------------- |
| ----------------------------------------------- ------------------------------------------
- 父------------------------------------------- --------------------------------------
我試圖設置父母的位置相對和兒童的位置絕對其中之一頂= 0和另一個底部= 0,但它們重疊時,父div縮小。
HTML代碼:
<div id="screen_header">
<div id="sh_header">Header text</div>
<div id="sh_subheader">Explanation text</div>
</div>
CSS代碼:
#screen_header{
position: relative;
}
#sh_header{
font-size: 24px;
padding: 10px;
position: absolute;
top: 0px;
}
#sh_subheader{
padding-left: 10px;
position: absolute;
bottom: 0px;
}
任何想法?
謝謝
你試過嗎?給兩個divs float:left' – Lal 2014-08-29 13:59:00
如果你還可以發佈提琴,它會容易得多,我們可以幫助你。 – Lal 2014-08-29 13:59:31
是的請將html和css粘貼到jsfiddle.net – Jay 2014-08-29 14:04:50