Am I subclassing my CSS correctly?
Asked Answered
H

4

5

I am making a set of buttons for my site, and I am in need of some professional insight.

In order to reduce CSS bloat, I want to subclass my buttons for different colors, ex .button.blue .

Will the following incur issues in the future? (assuming I don't make a class of just .blue) Do I have to use something like .button.button-blue instead?

.button {
  display:inline-block;
  padding: 9px 18px;
  margin: 20px;
  text-decoration: none;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 12px;
  font-weight: bold;
  text-align: center;
  background: #FFE150;
}
.button.blue {
  background: #49b8e7;
  border:1px solid #54abcf;
  border-bottom:1px solid #398fb4;
  color:#FFF
  text-shadow: 0 1px 0 rgba(255,255,255, 0.5);
}
.header{
  height: 50px;
}
.header.blue {
  background: blue;
  color: #fff;
}
Hejira answered 9/2, 2012 at 14:46 Comment(1)
There's no concept of subclassing in CSS. You can just add additional classes.Afflict
M
8

What you have there with the multi-classes will work fine assuming you want them to work like so:

<div class="button blue">
Will use .button and .button.blue
</div>

<div class="button">
Will only use .button
</div>

<div class="header blue">
Will  use .header and .header.blue
</div>

<div class="header">
Will only use .header
</div>

<div class="blue">
Will use neither of the .blue declarations because it doesn't contain header or button.
</div>
Muriel answered 9/2, 2012 at 15:0 Comment(2)
Thanks, this really got me thinking... the real con to my code above is if another dev makes a .blue class it will wreck the entire site. Where as .btn-blue will only be contained to that "module".Hejira
Exactly. It will definitely cause some issues if a standalone .blue is created.Muriel
P
1

A selector like .button.blue actually selects for an element with that has both "blue" and "button" as classes, not a class called .button.blue. See http://www.w3.org/TR/CSS21/selector.html#class-html.

You can use the .button.blue style rule you have listed, but you'll need to rearrange your HTML so that you have something like <button type="button" class="button blue"/>. However, you don't really need to have a button class since it being a button (or <input type="submit">, etc.) is enough to use in your selector. You could write a CSS rule that is simply button.blue, input[type=submit].blue{}

Pectinate answered 9/2, 2012 at 14:51 Comment(1)
Cool, I didn't think about adding the input[type=submit] like that =)Hejira
V
0

Seems like button.blue is enough.

The only difference between the two is if you use <button class="button blue">, or <button class="button button-blue">.

You even don't need to duplicate the painting in blue... You can just do something like this:

.button
{
// button style
}

.header
{
// header style
}

.blue
{
   background: blue; 
   color: #fff; 
}

Of course if you add the blue class to each of them. (<div class="header blue">and<button class="button blue">)

Velum answered 9/2, 2012 at 14:55 Comment(0)
C
0

Combine the classes applying the color you want to theme.

HTML:

<input type="text" class="text-field-required default" .../>
<select class="autocomplete-drop-down blue">...</select>
<a href="#" class="button-link green" .../>

CSS:

.text-field-required {
    //component css theme without colors
}
.default {
    //default color css theme for any component
}
.blue {
    //blue css theme for any component
}
.green {
    //green css theme for any component
}
Crichton answered 9/2, 2012 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.