tags:

views:

52

answers:

2

I have a id based CSS file where I noticed I'm mostly repeating myself. What I'd like to do is specify that an item with an id follows a certain class + some other rules. For example: I'd like id id1 to have all the properties of class LinkButton applied as well as the value back-color: green. Is there a way to do this in the css file? Is there a better way to do what I'm looking at?

+4  A: 

You can specify multiple selectors on a given set of properties, like this:

.LinkButton , #id1 { color: red; border: solid 1px blue; } /* Both get these */
#id1 { back-color: green; } /* Adding rules */

It can also override any of the properties if you need:

.LinkButton , #id1 { color: red; back-color: red; } /* Both get these */
#id1 { back-color: green; } /* Changing rules */

Or you give the element the class and ID:

<div id="id1" class="LinkButton ">
Nick Craver
Can't you set an ID and a class on one element, thus giving you the ability to have all of them follow the class you specified with all the same rules and then have CSS give additional properties by selecting the ID?
animuson
@animuson - Yes I have this example, however keep in mind whichever rules were specified last in the CSS win in this case.
Nick Craver
You could always add !important to the ID selected styles to make sure they override any styles in the main class.
animuson
@animuson - Agreed, though I tend to stay away from this and try to make the CSS clearer...personal taste with !important varies I believe.
Nick Craver
+3  A: 

There's no need to work with ID based style sheets. Avoiding repetition is exactly what classes are there for. Why not use classes instead of IDs? They are much more flexible, and take away nothing. (You can still target speficic elements.)

You can combine multiple CSS classes wonderfully (except for IE6, whose interpretation of multiple classes is broken.)

<span class="info extrabig highlight"> Important hint </span>

if you have a particular element that needs really specific rules, then give it a class named after its ID:

 <span id="really_special" class="id_really_special info extrabig highlight">

and define the unique properties of the element in the .id_really_special class.

IDs are there to access elements through the DOM imo. Styling should really be left to classes.

Pekka