2014-08-29 78 views
0

我有一個父div,它可以垂直擴展和縮小。裏面有兩個孩子的div。我想根據父div的高度來定位孩子:如何將兩個div放在水平位置,如果父div中沒有​​足夠的空間

  • vertical:如果父div的高度允許兩個孩子垂直放置。

    • 父------------------------------------------- --------------------------------------
      | -child1 ------- ----- |
      | ---------------------- |
      | -child2 --------------------------------- |
      | ------------------------------------------- |
      |
      |
      | ----------------------------------------------- ------------------------------------------
  • horizo​​ntal :如果父母的身高沒有足夠的空間讓孩子垂直放置。

    • 父------------------------------------------- --------------------------------------
      | -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; 
} 

任何想法?

謝謝

+1

你試過嗎?給兩個divs float:left' – Lal 2014-08-29 13:59:00

+3

如果你還可以發佈提琴,它會容易得多,我們可以幫助你。 – Lal 2014-08-29 13:59:31

+0

是的請將html和css粘貼到jsfiddle.net – Jay 2014-08-29 14:04:50

回答

0

如果您對於他們應該與父DIV收縮母公司的寬度孩子div的寬度。例如HTML:

<div id="parent"> 
<div id="child1"></div> 
<div id="child2"></div> 
</div> 

CSS:

#parent { 
    width: 100%; 
    height: 100%; 
} 

#child1{ 
    float: left; 
    width: 50%; 
    height: 50%; 
} 

#child2{ 
    float: right; 
    width: 50%; 
    height: 50%; 
} 

這僅僅是概念的代碼,將需要修改,以滿足您的需求。

+0

事實上,父級擴展和縮小了VERTICALLY,而不是水平。 – xagutxu 2014-08-29 14:21:56

+0

嘗試設置一個固定的高度像素和寬度百分比,應該停止它。 – Celt 2014-08-29 14:25:40

+0

父母縮小時,他們不會內聯,但它們會重疊... – xagutxu 2014-08-29 14:27:53

相關問題