2011-04-19 60 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

相关问题