2011-04-19 81 views
3

在下面的圖像中。我給了容器10px的填充,因爲大多數時候我需要填充。只有標題(藍色)我不需要填充。如何擴展標題的填充?如何將元素添加到給定的用CSS填充

http://i.stack.imgur.com/DeSHZ.jpg

我不希望給個別填充到容器內的每個元素。在標題

enter image description here

+3

感謝您抽出寶貴的時間來創建一個圖形!讓生活變得輕鬆。 – jessegavin 2011-04-19 05:26:30

+0

任何人都可以提出更好的標題這個問題? – 2011-04-19 07:41:28

回答

6

緣陰性。

查看的jsfiddle:http://jsfiddle.net/f2cdJ/

CSS

div { 
    background: red; 
    padding:10px; 
    width:200px; 
} 
p { 
    background: green; 
} 
h2 { 
    margin: 0 -10px; 
    background: blue; 
} 

HTML

<div> 
    <p>This is the paragraph. This is the paragraph.</p> 
    <h2>This is a heading</h2>  
    <p>This is the paragraph. This is the paragraph.</p> 
    <h2>This is a heading</h2> 
    <p>This is the paragraph. This is the paragraph.</p> 
</div> 
+0

是唯一的方法嗎?右邊的負邊距?實際上佈局是流動的,所以如果我給%的邊際? – 2011-04-19 05:20:03

+0

您可以使用%或px的邊距。 – jessegavin 2011-04-19 05:23:37

+0

但我想在兩側不僅在一側拉伸標題。 '#容器'的寬度是多少,標題應該自動延伸到'#容器'牆 – 2011-04-19 05:26:13

0

正如jessegavin說,使用切緣陰性。

負邊界很時髦,但它們正常工作。你必須包括10px填充兩次(一次爲左,其他時間爲正確的填充)爲標題的width,然後做一個負margin-left

margin-left: -10px; 
1

使用相同的代碼jessegavin確實。我用相對和絕對定位

看到它的jsfiddle here

CSS

div { 
background: red; 
padding:10px; 
width:200px; 
position:relative; 
} 
p { 
background: green; 
} 
h2 { 
background: blue; 
position:absolute; 
left:0; 
width:100%; 
} 

HTML

<div> 
    <p>This is the paragraph. This is the paragraph. This is the paragraph. This is the paragraph. </p> 
    <h2>This is a heading</h2>  
    <p>This is the paragraph. This is the paragraph. This is the paragraph. This is the paragraph. </p> 
    <h2>This is a heading</h2> 
    <p>This is the paragraph. This is the paragraph. This is the paragraph. This is the paragraph. </p> 
</div> 
+0

+1爲你的努力 – 2011-04-19 07:40:33

+0

非常感謝你:) – 2011-04-19 11:20:05

相關問題