Forum Moderators: not2easy

Message Too Old, No Replies

Applying a class to everything inside a <div>

         

jimcanoa

10:40 am on Jul 18, 2007 (gmt 0)

10+ Year Member



Hello,
I want to apply a class to everything inside a <div> (or <span>) but I can only get it to work using 'ID=' instead of 'class='. My problem is that I don't want to use 'ID=' because I will have many blocks of the same class within the page. Here's an example of what I want and how it doesn't work.


<html><head>
<style type="text/css">
p.a {color: blue}
.a {color: red}
#b p {color: blue}
#b {color: red}
</style>
</head><body>
<span class='a'>
i want this to be red - it's red<br>
<p>i want this to be blue - but the browser renders this red</p>
</span>
<div id='b'>
i want this to be red - it's red<br>
<p>i want this to be blue - it's blue</p>
</div>
</body></html>

Why does it work with class and not with ID? Is there a way to do this?

DanA

10:52 am on Jul 18, 2007 (gmt 0)

10+ Year Member



You should use valid html (p is a block element which shouln't be in a span which is an inline element)

[edited by: DanA at 10:53 am (utc) on July 18, 2007]

sonjay

10:56 am on Jul 18, 2007 (gmt 0)

10+ Year Member



Your selector for the p within the a is incorrect. Try this:

.a p [
color: blue;
]

Edit: Dan beat me,and he's right, too. Use div instead of span. But you also need to fix the selector syntax.

jimcanoa

11:34 am on Jul 18, 2007 (gmt 0)

10+ Year Member



Thank you guys, it works beautifully. Nevertheless, why does .a p work and not p.a? In the css tutorials I've read if you have a class and you want to specify a style you do it like this:


CSS file:
p.first { background-color: gray; }
html:
<p class="first">This is the p.first paragraph</p>

So, is that wrong? if it isn't, why is my case different?

sonjay

12:35 pm on Jul 18, 2007 (gmt 0)

10+ Year Member



Because you're not applying the class directly to the p -- you're applying it to the containing element (div or span in this case). The ".a p" syntax means "apply this style to all paragraphs that are contained within an element with the class a"

The other way "p.a" means "apply this style to all paragraphs with the class a" -- for that you would do <p class="a">


<div class="a">
<p></p>
<p></p>
<p></p>
</div>

vs

<p class="a"></p>
<p class="a"></p>
<p class="a"></p>

Marcia

12:43 pm on Jul 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Because you're not applying the class directly to the p -- you're applying it to the containing element (div or span in this case). The ".a p" syntax means "apply this style to all paragraphs that are contained within an element with the class a"

The other way "p.a" means "apply this style to all paragraphs with the class a" -- for that you would do <p class="a">


Isn't that what's called specificity?

jimcanoa

12:44 pm on Jul 19, 2007 (gmt 0)

10+ Year Member



Thank you sonjay, i think i understand now better how it works...

sonjay

10:43 pm on Jul 19, 2007 (gmt 0)

10+ Year Member



Marcia, specificity can come into play in that type of css selector. But this particular syntax is called a "descendent selector." jimcanoa is applying his style to paragraphs that are descendents of a div with a particular class assigned to it.

There are also ways to specify only direct descendents or siblings, but those aren't supported by all browsers (cough cough IE cough cough).