Forum Moderators: not2easy

Message Too Old, No Replies

Shorthand Image Class in DIV

         

Jeremy_H

2:54 am on Sep 22, 2005 (gmt 0)

10+ Year Member



I have a DIV in my document with a corresponding CSS definition to set the section to float left, and to have a specific width.

The DIV has only paragraphs and images in it.

<DIV class="123">
<p><img class="abc">Alpha</p>
<p><img class="abc">Beta</p>
...
<p><img class="abc">Omega</p>
</DIV>

All the images between the DIV tags use class "abc" to define their width, alignment and boarder. No images outside the DIV use class abc.

Is it possible to somehow define the images in the DIV to have a certain class, without having to call that class each time I call an image?

Like...

<DIV class="123">
<p><img>Alpha</p>
<p><img>Beta</p>
...
<p><img>Omega</p>
</DIV>

Thanks

createErrorMsg

3:06 am on Sep 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it possible to somehow define the images in the DIV to have a certain class, without having to call that class each time I call an image?

Yes, however first we have to clear up a minor problem with the code sample you posted. CSS id's and classes cannot begin with a number, so the div cannot have a class="123". Let's change that to class="xyz" and take it from there...

<DIV class="xyz">
<p><img>Alpha</p>
<p><img>Beta</p>
...
<p><img>Omega</p>
</DIV>

Given the above code, you can target styles to any image within the XYZ class by using the descendant selector [w3.org]...

.xyz img{
float:left;
width:200px;
}

This rule block says, "Apply these styles to any image nested anywhere inside of an element with the class 'xyz'."

cEM

Jeremy_H

3:50 am on Sep 22, 2005 (gmt 0)

10+ Year Member



Thank you so much, it works great!

As you might have already guessed, the 123 was just an example. The real class was something else, but thanks for the number rule clarification.

Robin_reala

12:52 pm on Sep 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cEM - a small correction. Only ids cannot start with a number; classes can. This is because a class name is just CDATA, but an id name is of type NAME [w3.org].

createErrorMsg

1:14 pm on Sep 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ooo! Thank you, Robin. I didn't know that. That was my new thing to learn today! :)