开发者

Don't apply CSS to <p> containing an <em> [duplicate]

This question already has answers开发者_运维知识库 here: Closed 11 years ago.

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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜