There are a million ways to write HTML and CSS, and everyone has their own, but is there a right way? Our code needs to be well structured, written in an organized manner, and performance driven. Sharing code amongst a team should be a joyful experience, not absolute terror.
Shay talks about how to how to write tactical HTML and CSS, crafting code that is maintainable, flexible, and extensible. Covering new methodologies such as OOCSS and SMACSS learn how to architect websites which are manageable and performant.
4. @shayhoweModular HTML & CSS
COMMON PROBLEMS
• Websites have difficulty scaling
• Code becomes brittle
• Files and code bases begin to swell
5. @shayhoweModular HTML & CSS
WHAT’S WRONG
• Best practices aren’t exactly best practices
• Standards need to evolve
6. @shayhoweModular HTML & CSS
BEST BAD PRACTICES
• Avoid extra elements
• Avoid classes
• Leverage type selectors
• Leverage descendent selectors
7. @shayhoweModular HTML & CSS
BEST BAD PRACTICES
Avoiding classes
section
ul#nav
li
{...}
section:nth-‐child(2)
div:nth-‐child(7)
>
a
{...}
Leveraging selectors
a.btn
{...}
#main
a.btn
{...}
#main
div.feature
a.btn
{...}
8. @shayhoweModular HTML & CSS
BEST BAD PRACTICES
Bad
#contact
li:nth-‐child(1)
input,
#contact
li:nth-‐child(2)
input
{
width:
160px;
}
#contact
li:nth-‐child(3)
textarea
{
width:
280px;
}
9. @shayhoweModular HTML & CSS
BEST BAD PRACTICES
Good
.col-‐1
{
width:
160px;
}
.col-‐2
{
width:
280px;
}
10. @shayhoweModular HTML & CSS
SPECIFICITY?
• Specificity determines which styles are applied
• Each selector has a specificity weight
• High specificity beats low specificity
• Low specificity is key
18. @shayhoweModular HTML & CSS
ASSEMBLE LAYOUT
• Separate presentation (or theme) from layout
• Create an object layer for layout
• Create a skin layer for theme
• Use a grid
24. @shayhoweModular HTML & CSS
AVOID PARENT DEPENDENCY
• Remove parent container dependency
• Decouple CSS from HTML
• Create components to be used anywhere
34. @shayhoweModular HTML & CSS
WATCH SPECIFICITY
Good
.gallery
{
text-‐transform:
uppercase;
}
.gallery-‐media
{
background:
#ccc;
}
35. @shayhoweModular HTML & CSS
USE CLASSES
• Write understandable class names
• Avoid unnecessary nesting
• Use same strength specificity
36. @shayhoweModular HTML & CSS
USE CLASSES
Bad
.feat-‐box
.callout
.pr
{
font-‐size:
12px;
}
.feat-‐box
.callout
.pr
.un
{
color:
#39f;
}
37. @shayhoweModular HTML & CSS
USE CLASSES
Good
.feat-‐box
.price
{
font-‐size:
12px;
}
.feat-‐box
.unit
{
color:
#39f;
}
38. @shayhoweModular HTML & CSS
USE CLASSES
Bad
.btn.large
{
font-‐size:
24px;
padding:
15px
30px;
}
<div
class="btn
large">...</div>
39. @shayhoweModular HTML & CSS
USE CLASSES
Good
.btn-‐large
{
font-‐size:
24px;
padding:
15px
30px;
}
<div
class="btn-‐large">...</div>
40. @shayhoweModular HTML & CSS
METHODOLOGIES
OOCSS
• Object-Oriented CSS
From Nicole Sullivan – oocss.org
SMACSS
• Scalable and Modular Architecture for CSS
From Jonathan Snook – smacss.com
52. Ships on the Roadstead by Willem van de Velde the Younger
53. @shayhoweModular HTML & CSS
GETTING STARTED
Build a styleguide
• Twitter Bootstrap, Zurb Foundation
Review methodologies
• OOCSS, SMACSS
Test your code
• CSS Lint, Inspector, PageSpeed