2012-08-30 81 views
-1

在下面簡單的CSS代碼的直筒,我想的h1p底部平直的底部(h1不應該p下,像在我的代碼)。邊距兩個元素

<div id="sqrs" style="width:200px; height:200px; border:1px solid BLACK;"> 
<p style="width:100px; height:100px; background-color:BLUE; float:right;">blue</p> 
<h1 style="background-color:YELLOW; margin-top:100px;">yellow</h1> 
</div> 

P風格不能改變,我不知道h1height(因爲它可以h2h3 ...)

我試過margin-top:100%(而不是margin-top:100px),但是這不是解決方案。

感謝,

+1

的jsfiddle或網址,請.. – supersaiyan

+0

感謝您的回覆,但我無法找到答案。 – blsn

+0

我還沒有回答!你有任何網頁的實時網址,以便我可以檢查 – supersaiyan

回答

0

http://jsfiddle.net/3sWEt/3/

您可以設置position:relative到容器和position:absolutebottom:0<h1>

CSS:

#sqrs{ 
    width:200px; height:200px; border:1px solid black; 
    overflow:hidden; 
} 
#sqrs>*{ 
    height:100px; 
} 
#sqrs>p{ 
    width:100px; background-color:blue; float:right; 
} 
#sqrs>div{ 
    overflow:hidden; 
    position:relative; 
} 
#sqrs>div>h1{ 
    background-color:yellow; 
    position:absolute; 
    bottom:0; 
    width:100%; 
} 

HTML:

<div id="sqrs"> 
    <p>blue</p> 
    <div><h1>yellow</h1></div> 
</div> 
0

http://jsfiddle.net/3sWEt/1/

#sqrs{ 
    width:200px; height:200px; border:1px solid black; 
    line-height:100px; 
    text-align:right; 
} 
#sqrs>*{ 
    text-align:left; 
    line-height:normal; 
} 
#sqrs>p{ 
    width:100px; height:100px; background-color:blue; float:right; 
} 
#sqrs>h1{ 
    background-color:yellow; 
    display:inline-block; 
    vertical-align:bottom; 
} 

您可以使用相同的line-heightdisplay:inline-blockvertical-align:bottom

但問題是,<h1>不擴展它的寬度來覆蓋它的父母的剩餘寬度。

+0

感謝@Oriol回覆我,非常專業的答案。 – blsn