modular html & css

55
TACTICAL HTML & CSS Shay Howe @shayhowe shayhowe.com MODULAR HTML & CSS

Upload: shay-howe

Post on 21-Jan-2017

79 views

Category:

Design


2 download

TRANSCRIPT

Page 1: Modular HTML & CSS

TACTICALHTML & CSS

Shay [email protected]

MODULAR

HTML & CSS

Page 2: Modular HTML & CSS

@shayhoweModular HTML & CSS

SHAY HOWEshayhowe.com – @shayhowe

Page 3: Modular HTML & CSS

The Gust by Willem van de Velde the Younger

Page 4: Modular HTML & CSS

@shayhoweModular HTML & CSS

COMMON PROBLEMS• Websites have difficulty scaling

• Code becomes brittle

• Files and code bases begin to swell

Page 5: Modular HTML & CSS

@shayhoweModular HTML & CSS

WHAT’S WRONG• Best practices aren’t exactly best practices

• Standards need to evolve

Page 6: Modular HTML & CSS

@shayhoweModular HTML & CSS

BEST BAD PRACTICES• Avoid extra elements

• Avoid classes

• Leverage type selectors

• Leverage descendent selectors

Page 7: Modular HTML & CSS

@shayhoweModular HTML & CSS

BEST BAD PRACTICESAvoiding classessection  ul#nav  li  {...}section:nth-­‐child(2)  div:nth-­‐child(7)  >  a  {...}

Leveraging selectorsa.btn  {...}#main  a.btn  {...}#main  div.feature  a.btn  {...}

Page 8: Modular HTML & CSS

@shayhoweModular HTML & CSS

BEST BAD PRACTICESBad#contact  li:nth-­‐child(1)  input,#contact  li:nth-­‐child(2)  input  {    width:  160px;}#contact  li:nth-­‐child(3)  textarea  {    width:  280px;}

Page 9: Modular HTML & CSS

@shayhoweModular HTML & CSS

BEST BAD PRACTICESGood.col-­‐1  {    width:  160px;}.col-­‐2  {    width:  280px;}

Page 10: Modular HTML & CSS

@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

Page 11: Modular HTML & CSS

Parade of the Black Sea Fleet by Ivan Aivazovsky

Page 12: Modular HTML & CSS

@shayhoweModular HTML & CSS

MAINTAINABILITYCode must be...

• Organized

• Modular

• Performant

Page 13: Modular HTML & CSS

@shayhoweModular HTML & CSS

ORGANIZATION

Page 14: Modular HTML & CSS

@shayhoweModular HTML & CSS

APPROACH• Stop thinking about pages

• Start thinking about components

• Take visual inventory

Page 15: Modular HTML & CSS

@shayhoweModular HTML & CSS

TECHNIQUEBase

• Core styles for entire siteNormalize (Reset), Default Elements, Grid, Variables

Components

• User interface concepts & design patternsAlerts, Buttons, Forms, List, Pagination, Tooltips

Modules

• Business logicAside, Header, Footer

Page 16: Modular HTML & CSS

@shayhoweModular HTML & CSS

TECHNIQUE

Page 17: Modular HTML & CSS

@shayhoweModular HTML & CSS

MODULARITY

Page 18: Modular HTML & CSS

@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

Page 19: Modular HTML & CSS

@shayhoweModular HTML & CSS

ASSEMBLE LAYOUTBad.news  {    background:  #eee;    color:  #666;    margin:  0  10px;    width:  400px;}

<div  class="news">...</div>

Page 20: Modular HTML & CSS

@shayhoweModular HTML & CSS

ASSEMBLE LAYOUTGood.grid-­‐4  {    margin:  0  10px;    width:  400px;}.feat-­‐box  {    background:  #eee;    color:  #666;}

<div  class="grid-­‐4  feat-­‐box">...</div>

Page 21: Modular HTML & CSS

@shayhoweModular HTML & CSS

ACCOMMODATE CONTENT• Separate content from container

• Stylize content regardless of container

Page 22: Modular HTML & CSS

@shayhoweModular HTML & CSS

ACCOMMODATE CONTENTBad.alert  {    background:  #f2dede;    border-­‐radius:  10px;    color:  #b94a48;    padding:  10px  20px;}

<div  class="alert">...</div>

Page 23: Modular HTML & CSS

@shayhoweModular HTML & CSS

ACCOMMODATE CONTENTGood.alert  {    border-­‐radius:  10px;    padding:  10px  20px;}.alert-­‐error  {    background:  #f2dede;    color:  #b94a48;}

<div  class="alert  alert-­‐error">...</div>

Page 24: Modular HTML & CSS

@shayhoweModular HTML & CSS

AVOID PARENT DEPENDENCY• Remove parent container dependency

• Decouple CSS from HTML

• Create components to be used anywhere

Page 25: Modular HTML & CSS

@shayhoweModular HTML & CSS

AVOID PARENT DEPENDENCYBad.feat-­‐box  {    background:  #eee;}article  .feat-­‐box  {    background:  #fff;}

<article>    <div  class="feat-­‐box">...</div></article>

Page 26: Modular HTML & CSS

@shayhoweModular HTML & CSS

AVOID PARENT DEPENDENCYGood.feat-­‐box  {    background:  #eee;}.feat-­‐box-­‐alt  {    background:  #fff;}

<article>    <div  class="feat-­‐box-­‐alt">...</div></article>

Page 27: Modular HTML & CSS

@shayhoweModular HTML & CSS

FAVOR SEMANTICS• Allow elements to adapt

• Uses individual classes to extend modules

Page 28: Modular HTML & CSS

@shayhoweModular HTML & CSS

FAVOR SEMANTICSBad.feat-­‐box  h2  {    color:  #f60;    font:  18px  Helvetica,  sans-­‐serif;}

<div  class="feat-­‐box">    <h2>...</h2></div>

Page 29: Modular HTML & CSS

@shayhoweModular HTML & CSS

FAVOR SEMANTICSGood.feat-­‐subhead  {    color:  #f60;    font:  18px  Helvetica,  sans-­‐serif;}

<div  class="feat-­‐box">    <h2  class="feat-­‐subhead">...</h2></div>

Page 30: Modular HTML & CSS

@shayhoweModular HTML & CSS

MEASURING SPECIFICITYFormula

• IDs, Classes/Pseudo-classes/Attributes, Elements

High Specificity (Bad)

#primary  #main  div.gallery  figure.mediaIDs: 2, Classes: 2, Elements: 2 — Compiled: 2–2–2

Low Specificity (Good)

.gallery-­‐mediaIDs: 0, Classes: 1, Elements: 0 — Compiled: 0–1–0

Page 31: Modular HTML & CSS

@shayhoweModular HTML & CSS

Page 32: Modular HTML & CSS

@shayhoweModular HTML & CSS

WATCH SPECIFICITY• Be explicit

• Keep specificity low

• Never use IDs or !important

• Avoid nested selectors (#main  .spotlight  strong  span)

Page 33: Modular HTML & CSS

@shayhoweModular HTML & CSS

WATCH SPECIFICITYBad#primary  #main  div.gallery  {    text-­‐transform:  uppercase;}#primary  #main  div.gallery  figure.media  {    background:  #ccc;}

Page 34: Modular HTML & CSS

@shayhoweModular HTML & CSS

WATCH SPECIFICITYGood.gallery  {    text-­‐transform:  uppercase;}.gallery-­‐media  {    background:  #ccc;}

Page 35: Modular HTML & CSS

@shayhoweModular HTML & CSS

USE CLASSES• Write understandable class names

• Avoid unnecessary nesting

• Use same strength specificity

Page 36: Modular HTML & CSS

@shayhoweModular HTML & CSS

USE CLASSESBad.feat-­‐box  .callout  .pr  {    font-­‐size:  12px;}.feat-­‐box  .callout  .pr  .un  {    color:  #39f;}

Page 37: Modular HTML & CSS

@shayhoweModular HTML & CSS

USE CLASSESGood.feat-­‐box  .price  {    font-­‐size:  12px;}.feat-­‐box  .unit  {    color:  #39f;}

Page 38: Modular HTML & CSS

@shayhoweModular HTML & CSS

USE CLASSESBad.btn.large  {    font-­‐size:  24px;        padding:  15px  30px;}

<div  class="btn  large">...</div>

Page 39: Modular HTML & CSS

@shayhoweModular HTML & CSS

USE CLASSESGood.btn-­‐large  {    font-­‐size:  24px;    padding:  15px  30px;}

<div  class="btn-­‐large">...</div>

Page 40: Modular HTML & CSS

@shayhoweModular HTML & CSS

METHODOLOGIESOOCSS

• Object-Oriented CSSFrom Nicole Sullivan – oocss.org

SMACSS

• Scalable and Modular Architecture for CSSFrom Jonathan Snook – smacss.com

Page 41: Modular HTML & CSS

@shayhoweModular HTML & CSS

PERFORMANCE

Page 42: Modular HTML & CSS

@shayhoweModular HTML & CSS

REUSE CODE• Do not duplicate code

• Remove old code

• Defer loading subsequent styles

Page 43: Modular HTML & CSS

@shayhoweModular HTML & CSS

REUSE CODEBad.news  {    background:  #eee;    color:  #666;}.social  {    background:  #eee;    color:  #666;}

Page 44: Modular HTML & CSS

@shayhoweModular HTML & CSS

REUSE CODEGood.news,.social  {    background:  #eee;    color:  #666;}

Better.feat-­‐box  {    background:  #eee;    color:  #666;}

Page 45: Modular HTML & CSS

@shayhoweModular HTML & CSS

MINIMIZE REQUEST• Combine like files (CSS & JS)

• Use image sprites

• Leverage data URIs

• Icon fonts

Page 46: Modular HTML & CSS

@shayhoweModular HTML & CSS

IMAGE SPRITES<span  class="icon  i-­‐twitter">...</span><span  class="icon  i-­‐facebook">...</span>

.icon  {    background:  url("icons.png")  0  0  no-­‐repeat;}.i-­‐twitter  {    background-­‐position:  0  -­‐16px;}.i-­‐facebook  {    background-­‐position:  0  -­‐32px;}

Page 47: Modular HTML & CSS

@shayhoweModular HTML & CSS

DATA URISHTML<img  height="100"  width="660"  alt="Pattern"  src="data:imagepng;base64,...">

CSS.pattern  {    background:  url("data:imagepng;base64,...")          repeat;}

Page 48: Modular HTML & CSS

@shayhoweModular HTML & CSS

COMPRESS & CACHE FILES• Utilize Gzip compression

• Losslessly compress images

• Cache common files

Page 49: Modular HTML & CSS

COMPRESS & CACHE FILES

Page 50: Modular HTML & CSS

@shayhoweModular HTML & CSS

COMPRESS & CACHE FILES

Page 51: Modular HTML & CSS

@shayhoweModular HTML & CSS

COMPRESS & CACHE FILES

Original 455kb Optimized 401kb

Page 52: Modular HTML & CSS

Ships on the Roadstead by Willem van de Velde the Younger

Page 53: Modular HTML & CSS

@shayhoweModular HTML & CSS

GETTING STARTEDBuild a styleguide

• Twitter Bootstrap, Zurb Foundation

Review methodologies

• OOCSS, SMACSS

Test your code

• CSS Lint, Inspector, PageSpeed

Page 54: Modular HTML & CSS
Page 55: Modular HTML & CSS

@shayhoweModular HTML & CSS

THANK YOU!Questions?

@shayhowehttp://learn.shayhowe.comhttp://bit.ly/mod-html-css