tags:

views:

64

answers:

4

So let's say I have the following in 'foo.css':

.border { border : solid 1px; }
#foo { color : #123; }
#bar { color : #a00; }

Now let's say that I have two divs I want borders for, so I do:

<div id="foo" class="border">Foo</div>
<div id="bar" class="border">Bar</div>

This works fine, but I find that when defining #foo and #bar in my css file, I would rather give them the characteristics of .border than give the div's the class, like so:

.border { border : solid 1px; }
#foo {
  <incantation to inherit from .border>
  color : #123;
}
#bar {
  <incantation to inherit from .border>
  color : #a00;
}

and then my html would just be:

<div id="foo">Foo</div>
<div id="bar">Bar</div>

Anybody know what that magic incantation is?

+7  A: 

That is not supported by css. The best you can do is something like:

#foo, #bar, .border { border : solid 1px; }
#foo { color : #123; }
#bar { color : #a00; }
wsanville
Off-topic, I would add that when using the `border` shorthand, the correct order of the values is `border-width`, then `border-style`, then `border-color`. Thus, `border: solid 1px` is invalid CSS, it should be `border: 1px solid`. Right now, browsers are very forgiving about errors of this kind, but that's not something to rely on indefinitely.
RegDwight
Good point, I simply copied the OP's styles without modifying the actual rules.
wsanville
I can't really do the #foo,#bar,.border{border:solid 1px} thing since the real definition I want is in a jquery-ui css file I'm hot linking to which is why I needed the solution outlined above. Thanks for the reply, it's helpful to know it just can't be done.Also, I didn't know about the border order thing. thanks.
Jason
Unless you're referencing jquery-ui from a CDN like Google, then you should consider hosting the css files you need and modifying them accordingly.
wsanville
+1  A: 

You might be interested in mixins with Sass. Sass lets you write css style sheets in a more efficient way, using tricks like this. Mixins let you define a group of attributes (say, to do with borders), and then include those attributes within certain css classes.

Peter
Hmmm, interesting. I'm already using haml so using sass for my css stuff would be a natural next step and solve this particular css shortcoming. Thanks to each of you for your response!
Jason
A: 

As Wsanville said, you can't use the class. But normal CSS inheritance does work - say if your html was

<div class="border">
    <div id="foo">
       hello
    </div>
    <div id="bar">
       world
    </div>
</div>

You could say

.border {border: 1px solid #f00;}
#foo {border:inherit;}

Which in some cases might be good enough

Marcus
A: 

If you're looking to push your CSS further instead of using some of the tricks outlined in earlier posts, you should look into CSS Compilers. They take CSS-like code you've writen, usually CSS with a few tricks added in, and turn them into normal CSS for the web.

David Ziegler wrote about some of the cool featured CSS compilers offer:

  • Variables - Good programmers don’t like to hardcode. In many cases you can avoid this in CSS by using good inheritence, but sometimes it’s unavoidable. With variables, changing your color scheme means updating one variable instead of 13 attributes.

  • Math - This goes hand in hand with variables. Say your left column is 100px, your right column is 500px, and your wrapper div is 600px. Well, maybe you decide to change it to 960px. Wouldn’t it be awesome if the width of your columns adjusted automatically? The answer is yes.

  • Nested Styles - This is probably the most important. CSS is flat, which means complex sites end up with CSS that is a pain to go through.

You can read about popular compilers in his blog post on the subject, or do some searching and find one that works best for you.

Robert Kuykendall