CSS: The Specificity Wars

A presentation at DC Digital Experiences Meetup in May 2016 in Washington, DC, USA by Ben Hong

Slide 1

Slide 1

CSS : the specificity wars Benjamin Hong Front End Developer @bencodezen

Slide 2

Slide 2

“Two CSS properties walk into a bar. A bar stool in a completely different bar falls over.” - Thomas Fuchs

Slide 3

Slide 3

Slide 4

Slide 4

css Crazy Specific styles

Slide 5

Slide 5

“It doesn’t matter how well-considered your source order is; how well you’re utilizing the cascade; what naming conventions you use; specificity can undo everything.

  • Harry Roberts (@csswizardry)

Slide 6

Slide 6

what’s specificity?

Slide 7

Slide 7

main.css 3,000 - 5,000 lines of code

Slide 8

Slide 8

main.css Credit to @csswizardry

Slide 9

Slide 9

Nested selector with ID? Nested classes with !important? Specificity An ID? Location in Stylesheet Credit to @csswizardry

Slide 10

Slide 10

Slide 11

Slide 11

Instead of building beautiful products, you’re wasting time and energy fixing someone’s poorly written code.

Slide 12

Slide 12

why now? • Why is this so important all of a sudden? • Everything is “agile” • Websites have evolved • Explosion of interest in coding

Slide 13

Slide 13

all css is not created equally

Slide 14

Slide 14

p { color: red; font-size: 1em; line-height: 1.5; } html body p { color: red; font-size: 1em; line-height: 1.5; }

Slide 15

Slide 15

common mistakes • Defaulting to ID for selecting elements • • #hero instead of .hero Selectors that are more than 3 layers deep • section #hero ul li a.current-page { … } • !important • a:link

Slide 16

Slide 16

Nested selector with ID? Nested classes with !important? Specificity An ID? Location in Stylesheet Credit to @csswizardry

Slide 17

Slide 17

Specificity Location in Stylesheet Credit to @csswizardry

Slide 18

Slide 18

modular architecture

Slide 19

Slide 19

Architectur e css modular architecture preprocessor Naming convention

Slide 20

Slide 20

Slide 21

Slide 21

my workflow preprocessor Architectur e Naming convention bem

Slide 22

Slide 22

my architecture • main.css 1. settings - configuration (i.e., colors, fonts, mixins, etc.) 2. vendor - third party styles (i.e., normalize, reset, etc.) 3. base - unclassed HTML elements (i.e., global, html, body, etc.) 4. layout - generic page layout styles (i.e., grid, spacing, etc.) 5. components - UI modules of the site (i.e., buttons, carousels, etc.) 6. pages - custom page layouts (i.e., short marketing campaign) 7. overrides - takes priority over everything else (i.e., 508, etc.)

Slide 23

Slide 23

naming convention block Element modifier .hero__button--primary

Slide 24

Slide 24

Problem: http://bit.ly/1TKP5FC Solution: http://bit.ly/1OeWOaP

Slide 25

Slide 25

final thoughts

Slide 26

Slide 26

general rules 1. Always choose classes over IDs for selectors* 2. Avoid using general element selectors (i.e., h1, h2, p, span, etc.)* 3. If you have more than 3 selectors in a declaration, you’re probably doing something wrong* 4. If your BEM class name is more than one level deep, you need to break it up into something small. 5. In the event you have to use !important or a very specific selector, add a quick comment so that future developers can make an informed decision.

Slide 27

Slide 27

things to remember 1. You won’t get it right the first time. 2. “Simpler is better” when writing good CSS 3. Discipline to write beautiful CSS is one of the hardest parts 4. Remember to pick and choose what works best for your team and projects. 5. This philosophy is just as much about improving code quality as it is for keeping your mental sanity

Slide 28

Slide 28

tools inventory • • • Pre-Processors • Sass (SCSS) - http://sass-lang.com/ • Stylus - http://stylus-lang.com/ Architecture • ITCSS - http://itcss.io/ • SMACSS - https://smacss.com/ Naming Conventions • BEM - http://getbem.com/ • Atomic Design - http://atomicdesign.bradfrost.com/ • OOCSS - https://www.smashingmagazine.com/2011/12/anintroduction-to-object-oriented-css-oocss/

Slide 29

Slide 29

Additional Resources • Managing CSS Projects with ITCSS by Harry Roberts https://speakerdeck.com/dafed/managing-css-projects-with-itcss • Atomic OOBEMITSCSS by Una Kravetz http://www.sitepoint.com/atomic-oobemitscss/ • An Introduction to BEM http://getbem.com/introduction/ • Installing Sass http://sass-lang.com/install

Slide 30

Slide 30

Q&A

Slide 31

Slide 31

Thank You! If you have any additional questions, feel free to reach out to me @bencodezen!