SMACSS – TIPS & TRICKS – How our client did their CSS?



SMACSS – TIPS & TRICKS – How our client did their CSS?

0 0


get-away-css


On Github richard-tirta / get-away-css

Stylin the old way

<table>
  <tr>
    <td align="left" onClick="donothing()">
      <center>
        <font face="verdana">
          <b>
            <p>
              Hello World
            </p>
          </b>
        </font>
      </center>
    </td>
  </tr>
</table>
Oh hey, these are some notes. They'll be hidden in your presentation, but you can see them if you open the speaker notes window (hit 's' on your keyboard).

FLASH

CSS2

p {
  font-size: 30px;                        // we set global font size here
  @media all and (max-height: 768px){     // now we have media query
    font-size: 20px;
  }
}

#section-1 {
  p {
    font-size: 20px;                      // apparently section-1 needs to be specific
    @media all and (max-height: 568px){   // and so their media query
      font-size: 10px;
    }
    .callout-p {
      font-size: 15px;                    // section 1 callout is specific too
      @media all and (max-height: 568px){
        font-size: 20px;
      }
      .description {
        font-size: 12px;
        @media all and (max-height: 568px){
          font-size: 10px;
        }
      }
    }
  }
}

BEM

block, element, modifier

SMACSS

Scalable and Modular Architecture for CSS

OOCSS

Object oriented CSS

Disclaimer

  • I'm not ( yet ) a CSS Evangelist/ Architect.

  • We discuss this as a style guide and standard practice techniques.

  • We will be strictly speaking about solutions within CSS.

  • BEM, SMACSS & OOCSS are not rivals.

No production exp, hoping we can standarized.

Some frameworks around.

Angular directives or Shadow DOM using Web Components.

They have competing arguments and abstractions, and thus this isn’t about benchmarking them. Different ideas can be mixed and match or cherry picked

BEM

BEM – meaning block, element, modifier – is a front-end naming methodology.

It is a smart way of naming your CSS classes to give them more transparency and meaning to other developers.

BEM

  • Basically it’s a naming convention
  • Direct CSS-DOM relation (minimize use of selector descendant)
  • Great for small project
  • Great for big projects WITH caveats
  • Requires introduction to those who are new to project.
  • -
  • less selector > performance
  • that doesn’t need verbose CSS structure but make it much more manageable.
  • but it requires other technique to be used otherwise it will be messy.
  • Requires those that are new to the project to be introduced to the system.
.block {}               /*higher level of an abstraction or component */
.block__element {}      /*descendent of block, usually would be .block .element */
.block--modifier {}     /*different state version of block, usually would be .block.modifier */

/* module overview */
.module-overview {}
            

Hypen and Underscores?

/* paragraph descendant inside module overview */ 
.module-overview__p {}

/* module overview with current state*/
.module-overview--current {}

/* paragraph descendant inside module overview in which the paragraph is in current state*/
.module-overview__p--current {}  
          
We make DOUBLE hypen and underscores so it can be chained.

HTML with regular CSS

<section class="module-overview current"><!-- the current class can have hidden properties -->     
  <p>Content 1</p>
  <div class="content"><!-- this content class is very general and easy to confuse -->
    <p class="current">Content 2</p>
  </div>
</section>
            

BEM

<section class="module-overview module-overview--current">
  <p class="module-overview__p">Content 1</p>
  <div class="module-overview__content">
    <p class="content__p--current">Content 2</p>
  </div>
</section>
            

current class can have hidden properties not for module-overview

It looks ugly.

And if you overdo it

.module-1--show-content{
  min-height: 280px;
  transform: translatey(0);
}

.module-1__languageDropdown{
  @include clearfix;
  pointer-events: auto;
  position: absolute;
  top: 0;
  right: 0;
  z-index: 9;
}

.module-1__content {
  padding-top: 1px;
  position: relative;
  pointer-events: none;
  width: 100%;
  margin: 0 auto;
  transition: opacity 1s;
  z-index: 5;
}

.module-1__content--hide-content {
  opacity: 0;
}

.module-1__content__logo {
  margin: 0 auto 5px auto;
}

          

It can get really ugly

.module-1__content__headline-container {
  position: relative;
}

.module-1__content__headline-container__headline {
  width: 100%;
  overflow: hidden;
  transition: opacity 1s;
  text-align: center;
}

.module-1__content__headline-container__headline__main-headline {
  display: block;
}

.module-1__content__headline-container__headline__alt-headline {
  display: none;
}

.module-1__content__headline-container__headline--show-headline__headline-0 {
  opacity: 1; height: auto;
}

.module-1__content__headline-container__headline--show-headline__headline-1 {
  opacity: 0; height: 0;
}

.module-1__content__headline-container__headline--show-headline__headline-2 {
  opacity: 0; height: 0;
}

.module-1__content__headline-container__headline--show-headline__headline-3 {
  opacity: 0; height: 0;
}
          

For starter, take it with a grain of salt and don't overdo it

// Embrace SASS

.module-1 {
  .show-headline__headline-3 {
    opacity: 0; 
    height: 0;
  }
}
           

You will start liking it.

SMACSS

Scalable and Modular Architecture for CSS.

It's a simple set of rules that guide you in laying out your CSS.

SMACSS

  • Modularizing CSS in standardized way.
  • Great for medium to large project as it organized your CSS.
  • Ok for small project but don’t over organize.
base, layout, module, state, theme, etc

SMACSS

5 Main Categories

  • BASE
  • LAYOUT
  • MODULE
  • STATE
  • THEME

BASE

Are basically, base styles.

html, body, form { 
  margin: 0; padding: 0;
}

a {
  color: black;
}
                
            

LAYOUT

Major components

#header, #article, #footer {
    width: 960px;
    margin: auto;
}

#article {
    border: solid #CCC;
    border-width: 1px 0 0;
}

.section {
  display: block;
  position: relative;
  width: 100%;
}
                
            

MODULE

Re-usable minor components

.callouts {
  background: rgb(100,100,100);
  color: white;
  display: block;
  width: 300px;
  height: 300px;
}

.product {
  width: 100%;
  height: 100px;
} 

.module > h2 {
    padding: 5px;
}

.module span {
    padding: 5px;
}
            

STATE

State that overrides default styles

.hide {
  opacity: 0;
}

.show {
  opacity: 1;
}

.tab {
    background-color: purple;
    color: white;
}

.is-tab-active {
    background-color: white;
    color: black;
}
            

THEME

Generally you won't use theme

But it's useful when you have multiple theme, typography and localization.

More of SMACSS

More details about CSS good practices: https://smacss.com/

SMACSS is about a guide and there're more details that can't be covered.

OOCSS

Object oriented CSS (OOCSS) is a methodology of writing reusable CSS that is fast, modular, scalable and maintainable.

This is where we make objects (in this case HTML elements), modular and location independent, able to be placed anywhere on a page and behave predictably.

OOCSS

  • Modularized CSS by separating functions from visuals.
  • Separate structure from skin.
  • Separate container from content/ elements.
  • IDs are only for html/js hooks, the rest are classes.
  • Great for small project and even greater for big project.
  • Works really well with use of SASS (mixins, maps, etc).
  • You might already do this unconsciously, easy to implement.

NOT DRY

#button {
  width: 200px;
  height: 50px;
  padding: 10px;
  border: solid 1px #ccc;                      /*NOT DRY*/
  background: linear-gradient(#ccc, #222);     /*NOT DRY*/
  box-shadow: rgba(0, 0, 0, .5) 2px 2px 5px;   /*NOT DRY*/
}

#box {
  width: 400px;
  overflow: hidden;
  border: solid 1px #ccc;                       /*NOT DRY*/
  background: linear-gradient(#ccc, #222);      /*NOT DRY*/
  box-shadow: rgba(0, 0, 0, .5) 2px 2px 5px;    /*NOT DRY*/
}
           

with OOCSS

.button {
  width: 200px;
  height: 50px;
}

.box {
  width: 400px;
  overflow: hidden;
}

.skin {
  border: solid 1px #ccc;
  background: linear-gradient(#ccc, #222);
  box-shadow: rgba(0, 0, 0, .5) 2px 2px 5px;
}
            

Even better with SASS

@mixin skin {
  border: solid 1px #ccc;
  background: linear-gradient(#ccc, #222);
  box-shadow: rgba(0, 0, 0, .5) 2px 2px 5px;
}

.button {
  @include skin;
  width: 200px;
  height: 50px;
}

.box {
  @include skin;
  width: 400px;
  overflow: hidden;
}
            

TIPS & TRICKS

  • Use these techniques according to your needs.
  • Mix, match and cherry pick them.
  • BE CONSISTENT with your team.

DOCSSA

docssa.info

How our client did their CSS?

SALESFORCE (2013)

  • Custom YUI CSS Frameworks
  • Lose OOCSS and without SASS

APPLE (2014)

  • Custom Foundation CSS Frameworks
  • Strict OOCSS with SASS

How AKQA SF do CSS?

Audi/ Visa

  • Custom Twitter Bootstrap
  • OOCSS with SASS

Others

  • Some OOCSS
  • SMACSS
So, we're already doing some of this! But we're just simply not aware of the system. Inconsistencies.

TL ; DR

  • Use BEM for good naming convention but make sure to use other technique too in medium to large project.
  • Use SMACSS for organizing and modularizing your CSS structure, but don’t over organize it in small projects.
  • Use OOCSS to make your project scalable and object oriented, use it for all projects.