views:

1022

answers:

6

Hi,

I want space between my <p>content</p> tags. Not before and not after <p> tags. Fx my code is:

<div>
   <h1>A headline</h1>
   <p>Some text</p>
   <p>Some text</p>
</div>
Something

I don't want space between h1 and p which is done with zero margin on h1. But I don't want space after the last <p> tag. Is this possible without :last-child or some js/jQuery?

I can't set class="last" on the last tag because it is a CMS system.

A: 
<div>
   <h1>A headline</h1>
   <p>Some text</p>
   <div class="paragraph-space"></div>
   <p>Some text</p>
</div>

?

Rafal Ziolkowski
Thanks for reply, but I can't control the text that way. (CMS)
lasseespeholt
+2  A: 

Set a default bottom-margin to p, then give the last tag a class with no bottom-margin.

da5id
the IE safe way
annakata
I can't do that because the text is rendered with CMS.
lasseespeholt
OK then, how about setting margin-top to "div p" and negative margin-bottom to "div h1"?
da5id
I have not tested it but cletus solution with p + p works fine for IE7+. That will do... I hope ;)Thanks :)
lasseespeholt
+9  A: 
p + p {
  margin-top: 1.5em;
}

(Although this requires a browser with better support for CSS than IE6)

David Dorward
the way it *should* be done
annakata
Nice, I'll try that. I'm aiming for IE7+, Chrome, Safari 4+, FireFox 2+
lasseespeholt
works perfectly :) thanks.
lasseespeholt
A: 
<div>
  <h1>A headline</h1>
  <p>Some text</p>
  <p style="margin-bottom: 0;">Some text</p>
</div>
James Skidmore
Thanks for reply, but I can't control the text that way. (CMS)
lasseespeholt
+3  A: 

If you're not required to support IE6 you can use:

div, h1, p { margin: 0; }
p + p { margin-top: 12px; }

If you need to support IE6, this is a dirty hack but it works without Javascript:

div, h1, p { margin: 0; }
h1 { margin-bottom: -12px; }
p { margin-top: 12px; }

The disadvantage of this method is that you can't simply use, say, 1em for the balancing margins as they have different font sizes. You can either manually adjust as required or use pixel widths.

cletus
But the problem with the last method is, that if I have a list after the h1 then they will float into eachother. I will go with IE7+.
lasseespeholt
If you can go with IE7+ then you are sorted. I'm just pointing this out for completeness because many do need to support IE6 still (sadly).
cletus
Also, it's worth pointing out that I wouldn't suggest doing this for all divs. I'd associate a class to put on the div where you want to do this where you know the format of the contents to avoid the situation like you mention. The same heading followed by list issue has the same problem with using the + selector.
cletus
Okay, thanks :)
lasseespeholt
A: 

If you want to make it more browser compatible, you could also use:

p { margin-top: 24px; }
h1 { margin-bottom: -24px; } // play with this value as necessary

Negative margins will pull elements toward them. It's messier than I like, but when you are at the mercy of CMS generated code with no way to add classes, you have to be creative.

Jason Rhodes
Yes, cletus also showed this but thanks.
lasseespeholt