Share
Embed
Scalable, Modular CSS Architecture
Building web sites and web applications you don't hate
TJ Draper
August 05, 2015
Transcript
50 slides
-
Scalable, Modular CSS Architecture Building web sites and web applications
you like don't hate -
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 -
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 -
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 -
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) -
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. -
Organizing CSS 1. Organize your files (remember, the maintainer is
a violent psychopath) (remember, the maintainer is a violent psychopath) -
Be Consistent Find the file structure that works for you
and/or your team and stick with it. -
Organizing CSS 1. Organize your files (remember, the maintainer is
a violent psychopath) 2. Organize within your files -
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 -
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. -
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 -
Object Oriented CSS .ninja { color: black; visibility: hidden; }
.ninja.--white { color: white; } -
CSS has some problems we’re not going to solve them
all today this might feel familiar -
• 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 -
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 -
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 -
BEM .block { property: value; } .block.--modifier { background: #000;
} .block__element { max-width: 1200px; } Block, Element, Modifier -
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; } -
BEM Block, Element, Modifier • Block A block is the
sole root of a component .listing {} -
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> -
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: -
BEM Block, Element, Modifier • Block • Element A component
part of the Block .listing__img {} -
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’ 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> -
BEM Block, Element, Modifier • Block • Element • Modifier
Variant / extension of a block or element .listing.--featured {} -
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’ 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> -
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) -
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 -
Advantages BEM Methodology header a {} Poor greedy selector .header__promo-link
{} or .header__link.--promo {} Better input.btn {} Poor overly qualified -
BEM and Markup <div class="profile pro-user"> <img class="avatar image"> <p
class="bio">...</p> </div> -
BEM and Markup <div class="profile --is-pro-user"> <img class="avatar profile__image"> <p
class="profile__bio">...</p> </div> -
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 -
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