开发者

How to take an element over to given padding with CSS

In the below image. I've given 10px padding to container because most of the time I need that padding. only the headings (blue) I don't need padding. How to extend heading over padding?

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

I don't wan开发者_如何学JAVAt to give individual padding to each element inside container.

How to take an element over to given padding with CSS


Negative margin on the headings.

See on 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>


Using the same code as jessegavin did . I used relative and absolute positioning

see it on 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>


As jessegavin said, use negative margins.

Negative margins are funky, but they work properly. You'd have to include the 10px padding twice (once for the left, another time for the right padding) into the width of the heading, and then do a negative margin-left:

margin-left: -10px;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜