Am I properly subclassing my CSS?

I am creating a set of buttons for my site, and I need professional insight.

To reduce CSS bloat, I want to subclass my buttons for different colors, e.g. .button.blue.

Will the following problems arise in the future? (assuming I'm not making the class just .blue) Should I 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;
}
+5
source share
4 answers

What you have with multiple classes will work just fine if you want them to work like this:

<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>
+8
source

, .button.blue, , "", "w290" , .button.blue. . http://www.w3.org/TR/CSS21/selector.html#class-html.

.button.blue, , HTML, - <button type="button" class="button blue"/>. , ( <input type="submit"> ..), . CSS, button.blue, input[type=submit].blue{}

+1

, button.blue .

, <button class="button blue"> <button class="button button-blue">.

... - :

.button
{
// button style
}

.header
{
// header style
}

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

, . (<div class="header blue"> <button class="button blue">)

0

, , .

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
}
0

All Articles