$30 off During Our Annual Pro Sale. View Details »

Scalable, Modular CSS Architecture

Scalable, Modular CSS Architecture

Building web sites and web applications you don't hate

Avatar for TJ Draper

TJ Draper

August 05, 2015
Tweet

Other Decks in Programming

Transcript

  1. TJ Draper • Worked with HTML/CSS in some form since

    2003 • Always trying to figure out ways of optimizing CSS architecture and workflow • During tenure at Caddis, have helped to organize and set our CSS standards and finalize our Object Oriented approach Senior Developer at Caddis
  2. What we’ll cover today • Organizing your CSS • Object

    Oriented approach to CSS • How we can use something like BEM to accomplish this • How all this impacts our markup • How to effectively use preprocessing
  3. Why does this matter to you? • Front end dev

    or not, most of us are going to need to work with CSS in some way or another • We should apply good development principles across the board — even to a loose language like CSS • We should care about our craft • We should always be refining what we do to make ourselves better
  4. Remember this! Always code as if the one who maintains

    the product will be a violent, homicidal maniac who knows where you live. (note to self: this slide will work for nearly any development presentation)
  5. Have a plan! • Don’t go in assuming everything will

    work itself out • If you don’t have a plan, your CSS isn’t going to organize itself and you won’t be able to find anything. EVER. • Have a principle behind why your CSS is organized the way it is. • Use a build process so you can split up your files in a way that makes the most sense organizationally.
  6. Organizing CSS 1. Organize your files
 (remember, the maintainer is

    a violent psychopath) (remember, the maintainer is a violent psychopath)
  7. Be Consistent Find the file structure that works for you

    and/or your team
 and stick with it.
  8. Organizing CSS 1. Organize your files
 (remember, the maintainer is

    a violent psychopath) 2. Organize within your files
  9. Coding Style Guides • Coding Style Guides are a must

    for teams — even for individuals to help maintain consistency • They help you build and maintain projects over time • They make it easy to work across projects, pull code from one project to another • They promote an environment of code quality • They increase productivity
  10. Organize within rule-sets • Adhere to a standard that works

    for you and your team • At Caddis, we organize alphabetically to cut down on guesswork • Makes it easy to find something specific in rule-set • Repeatable; Easy to maintain — anyone can come in and take over maintenance and know exactly how to do it.
  11. Organizing CSS 1. Organize your files
 (remember, the maintainer is

    a violent psychopath) 2. Organize within your files 3. Organize your third party CSS files
  12. CSS has some problems we’re not going to solve them

    all today this might feel familiar
  13. • CSS is global in nature • Everything you do

    has the potential to affect everything else • This global nature can cause specificity and selector wars with yourself • CSS was not conceived and built to do what we do with it today CSS has some problems
  14. CSS is Problematic “CSS is not a pretty language. While

    it is simple to learn and get started with, it soon becomes problematic at any reasonable scale.” — Harry Roberts, cssguidelin.es
  15. What do we do about this problem? “There isn’t much

    we can do to change how CSS works, but we can make changes to the way we author and structure it.” — Harry Roberts, cssguidelin.es
  16. BEM .block {
 property: value;
 }
 .block.--modifier {
 background: #000;


    }
 .block__element {
 max-width: 1200px;
 } Block, Element, Modifier
  17. A Very Simple Example .btn { border: 1px solid green;

    border-radius: 4px; color: green; display: inline-block; padding: 16px; text-align: center; text-decoration: none; vertical-align: middle; } .btn:hover { background-color: green; color: white; } .btn.--red { border-color: red; color: red; } .btn.--red:hover { background-color: red; } <a class="btn —is-active"> Buy Now </a> <a class="btn --red"> Buy Now </a> <a class="btn"> <span class="btn__price"> $29.99 </span> <span class="btn__text"> Buy Now! </span> </a> .btn__price { display: block; margin-bottom: 4px; padding-left: 20px; padding-right: 20px; } .btn__text { font-weight: bold; }
  18. BEM Block, Element, Modifier • Block A block is the

    sole root of a component .listing {}
  19. Block .listing { background-color: #f6f6f6; border: 1px solid #ddd; display:

    block; color: #8c8a7e; font-family: Arial; min-height: 100px; padding: 8px; text-decoration: none; } .listing.--featured { background-color: #ffd1d1; border-color: #910000; } <a class="listing"></a> <a class="listing --featured"></a>
  20. Block A Block context starts at the most logical, self-contained,

    discrete location. .page {} .content {} .sub-content {} .footer {} .footer__copyright {} Correct: .page {} .page__content {} .page__sub-content {} .page__footer {} .page__copyright {} Incorrect:
  21. Element .listing__img { float: left; margin-right: 14px; max-width: 100%; max-height:

    100px; } .listing__title { color: #68665b; font-weight: bold; margin-bottom: 10px; } <a href="#" class="listing"> <img src="property.jpg" class=“listing__img" alt=""> <h2 class="listing__title"> Will Primos&rsquo; Famous Rivers Run Farm </h2> <p class="listing__excerpt"> Rivers Run is truly one of those once in a lifetime places that God only created a few of in this country. </p> </a>
  22. BEM Block, Element, Modifier • Block • Element • Modifier

    Variant / extension of a block or element .listing.--featured {}
  23. Modifier .listing.--featured { background-color: #ffd1d1; border-color: #9100; } <a href="#"

    class="listing --featured"> <img src="property.jpg" class=“listing__img" alt=""> <h2 class="listing__title"> Will Primos&rsquo; Famous Rivers Run Farm </h2> <p class="listing__excerpt"> Rivers Run is truly one of those once in a lifetime places that God only created a few of in this country. </p> </a>
  24. Advantages BEM Methodology Performance With BEM, there is very little

    selector qualification body.home div.header ul {} Poor Performance Example • find all ul elements on the DOM • see if they are anywhere inside the .header class • see if .header class exists on a div element • see if any of that is anywhere inside any elements with a class of .home • see if .home exists on a body element CSS reads right to left (why yes, that does seem a little insane)
  25. Advantages BEM Methodology Selector Intent With BEM, you always select

    exactly what you want regardless of the type of element header ul {} Poor selector intent • Your intent is probably to style the main navigation menu • But this selector will style any ul inside any header element • Not reusable or context independent Swapping element types is easy Refactoring is not a nightmare .site-nav {} Good selector intent
  26. Advantages BEM Methodology header a {} Poor greedy selector .header__promo-link

    {} or .header__link.--promo {} Better input.btn {} Poor overly qualified
  27. BEM and Markup • BEM will add more classes to

    your markup • Will give you a better sense of how elements are connected to each other • Show you the logical starting point of blocks and their groupings Adding to markup by way of classes But that’s not bad. It will give you a better look at what’s going on and enhance readability
  28. Nesting .listing { background-color: #f6f6f6; border: 1px solid #ddd; display:

    block; color: #8c8a7e; font-family: Arial; min-height: 100px; padding: 8px; text-decoration: none; } .listing.--featured { background-color: #ffd1d1; border-color: #9100; } .listing__img { float: left; margin-right: 14px; max-width: 100%; max-height: 100px; } .listing__title { color: #68665b; font-weight: bold; margin-bottom: 10px; } .listing { background-color: #f6f6f6; border: 1px solid #ddd; display: block; color: #8c8a7e; font-family: Arial; min-height: 100px; padding: 8px; text-decoration: none; &.\--featured { background-color: #ffd1d1; border-color: #91000; } &__img { float: left; margin-right: 14px; max-width: 100%; max-height: 100px; } &__title { color: #68665b; font-weight: bold; margin-bottom: 10px; } } CSS Less