SMACSS


Scalable & Modular Architecture for CSS

A Quick Selector Refresher

Element
body, div, a
Class
.class

ID

#id

A QUICK SELECTOR REFRESHER

Descendant
.ascendant .descendant
Child
.parent > .child
Pseudo Class
:hover, :active



About the author

Jonathan Snook


The Problem

Overly specific rules

  • Inflexible
  • Hard to maintain

.ribbon-item-large.ribbon-splitbutton:hover div.ribbon-splitbutton-icon {
    border-bottom: 1px solid orange;
}

Loose rules

Need to be overridden in unrelated parts of the CSS
/* Standard font */
html, body, table, td, input, select, button, textarea, iframe, div
{
    font-family: Verdana;
    font-size: 11px;
}

table.pt td div
{
    /* Layout.css defines a general font-family and font-size rule 
       that we need to override for printing
       font-family and font-size must be specified in a separate
       descendant div rule for every kind of pivot table style
       text-decoration does not cascade and also must be specified
     */
    font-family: "Segoe UI";
    font-size: 12px;
}


solutions

  • Categorisation of CSS rules
  • Naming conventions
  • Limiting "depth of applicability"

Categorisation of css rules



  • Base
  • Layout
  • Modules
  • State
  • Theme

Base Rules

Default styling for elements for the entire page
  • Element selectors
  • Pseudo classes
  • No class or ID selectors

Examples

body
{
    background: white;
    margin: 0;
}
fieldset
{
    border: 1px solid #D5DFE5;
    border-radius: 3px;
}

fieldset[disabled="disabled"] > legend
{
    filter: alpha(opacity=40);
    opacity: 0.4;
}

Layout Rules

Page-level or generally applicable layout
  • ID selectors
  • Class names, prefixed with l-

Examples

Page-level

#header {
    position: relative;
    height: 55px;
    line-height: 55px;
}

General layout

.l-grid {
    margin: 0;
    list-style-type: none;
}

.l-grid > li {
    display: inline-block;
    margin: 0 0 10px 10px;
}

Modules

Discrete, stand-alone components of the page
  • Class selectors
  • Child selectors
  • Pseudo classes
  • Avoid element selectors

Namespacing

instead of

.menu
.menu .item
.menu .control

use

.menu
.menu-item
.menu-control                            

Subclassing

Avoid parent selectors
.treelist td {
    border-bottom: solid 1px grey;
}

.cubeconfig {
    color: blue;
}

.cube-config-view .treelist td {
    border: none;
    color: #3e3e3e; /* from Common.css */
}

Subclassing

.treelist td {
    border-bottom: solid 1px grey;
}

.treelist-cube td {
    border: none;
}

HTML will have both classes

<table class="treelist treelist-cube">

State Rules

Augments and overrides all other styles
Can modify layouts or modules
Applied via JavaScript

  • Class selectors
    • Prefixed with is-
    • Namespaced to match modules
  • !important is OK

Examples

.button
{
    border: 1px solid grey;
    border-radius: 3px;
    color: black;
}

.is-button-disabled
{
    color: grey;
}

Examples

.statusribbon-icon
{
    width: 16px;
    height: 16px;
    background-repeat: no-repeat;
}

.is-statusribbon-error .statusribbon-icon
{
    background-image: url('error.png');
}

.is-statusribbon-info .statusribbon-icon
{
    background-image: url('info.png');
}

Depth of Applicability

Don't affect other modules

Avoid
  • Descendant selectors                            
  • Element selectors

Use
  • Class selectors
  • Child selectors
    • Child element selectors are OK


Questions?




SMACSS: smacss.com

smacss

By soxtoby

smacss

  • 46