1

Possible Duplicate:
CSS selector for “foo that contains bar”?

All the <p> on my site get margin-bottom of 20px.

I don't want to apply this margin-bottom to <p> which contain an <em> element.

Is it possible to this without classes or id's.

CSS3 can be used.

Community
  • 1
  • 1
Hedge
  • 16,142
  • 42
  • 141
  • 246

6 Answers6

2

To apply style to all p not containing an em:

p:not(:has(em)) {
    margin-bottom: 20px;
}

I'm afraid this isn't possible with pure CSS.

Mikael Dúi Bolinder
  • 2,080
  • 2
  • 19
  • 44
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
1

Unfortunately, there is no way to do this with pure css. See Is there a CSS parent selector?

You could use some jQuery though.

$('em').parent().css('marginBottom','0');

http://jsfiddle.net/jasongennaro/5pPGF/

Community
  • 1
  • 1
Jason Gennaro
  • 34,535
  • 8
  • 65
  • 86
1

The only way I could see doing this in pure CSS is with a parent-node selector. Unfortunately, such a thing does not exist in CSS2 or CSS3.

Community
  • 1
  • 1
aroth
  • 54,026
  • 20
  • 135
  • 176
1

What you're describing is basically an "ascendent" selector, selecting some element based upon its descendents. This isn't possible using just CSS, you would have to also use JavaScript.

dbb
  • 1,100
  • 6
  • 12
1

Pure CSS does not do that (yet) as far as I know, but you can achieve this by smart use of jQuery:

$(function() {
    $("em").parent("p").addClass("nomargin")
})

Or something like that...

Roland Tepp
  • 8,301
  • 11
  • 55
  • 73
0

Your question is unclear. Pleae recheck your question. But you can set margins, paddings for global tags like

p{margin:0}

or

*{margin:0; padding:0 } for all elements then change it for exact divs, classes. In that case all other ones still will set to margin:0, padding:0

Tural Ali
  • 22,202
  • 18
  • 80
  • 129