Don't apply CSS to <p> containing an <em> [duplicate]
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.
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.
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/
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.
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.
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...
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
精彩评论