6
<div class="big_box">haha</div>
<div class="small_box">haha</div>

This (type 1) seems workable :-

.big_box, .small_box { border:1px solid #ccc; /* lengthy attributes*/}
.big_box { width:150px; height:150px; }
.small_box { height:140px; width:140px; }

This (type 2) also works :-

.big_box { border:1px solid #ccc; width:150px; height:150px; /* same lengthy attributes*/}
.small_box { border:1px solid #ccc; width:150px; height:150px; /* same lengthy attributes* }

Of course, type 2 is lengthy and repeating for most of the common attributes (with same value),
is there any issue for using type 1 ?
(or simply this is allowed ?)

P.S type 3 works too (but I find is hard to manage) ... if

 <div class="box big">haha</div>

And

.box { border:1px solid #ccc; /* lengthy attributes*/}
.big { width:150px; height:150px;}
4
  • 4
    Yes! It is 100% allowed! It is a common practice to do it. Commented Feb 24, 2012 at 14:39
  • oh, really? Cause I have the impression like declaring same class is not allowed in programming Commented Feb 24, 2012 at 14:40
  • 2
    Thats just not how css works. It's supposed to cascade. Notice though, that you're not even redeclaring the same selector multiple times .big_box, .small_box is a different selector than .big_box. But it'd be fine if you had. Commented Feb 24, 2012 at 14:45
  • 1
    FYI: What you call attribute is actually property. CSS property. Pair of property and its value is called declaration. Commented Feb 24, 2012 at 16:02

5 Answers 5

9

type 1 is actually very common when declaring multiple classes with some share the same attributes and some have their owned unique attributes. type 2 is a bit dirty to maintain while type 3 is similar to type 1.

it is all works, just a matter of coding style and ease of maintenance

Sign up to request clarification or add additional context in comments.

Comments

4

There is no issue using the first syntax.

It is actually useful, as you noticed to avoid repeating the same time the same styling.

It is also useful to separate the types of styling: positionning, fonts, colors.

You can for instance have in one file the styling that positions the elements and in another file the styling for the colors/backgrounds, allowing you to change the "theme" of your site by just changing the css file responsible for your colors, without breaking the layout itself.

This is for instance what is used in jQuery UI CSS Framework. You have:

  • the jquery ui base css
  • all the jquery themes files

Comments

1

All three possibilities are allowed. I'd prefer the third for its conciseness.

Comments

0

I would use option 3 so that both boxes inherit the class box and then you can define whether that box is small or large.

However, there are a number of ways you can do this but this would certainly be my recommendation.

1 Comment

i actually need the dimension to be different
0

I prefer hyphenated classes.

This way you could do something like this:

<div class="box-big">haha</div>
<div class="box-small">haha</div>

div[class|=box]{
      /* shared attributes*/
}

.box-big{
    /* stuff */
}
.box-small{
    /* stuff */
}

1 Comment

Is this consider as an expression? div[class|=box]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.