Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Element Queries - Converge 2015

Element Queries - Converge 2015

Patrick Fulton

April 15, 2015
Tweet

More Decks by Patrick Fulton

Other Decks in Technology

Transcript

  1. “The web’s greatest strength, I believe, is often seen as

    a limitation, as a defect. It is the nature of the web to be flexible, and it should be our role as designers and developers to embrace this flexibility, and produce pages which, by being flexible, are accessible to all.” JOHN ALLSOPP “A Dao of Web Design” ALA #58
  2. “The web’s greatest strength, I believe, is often seen as

    a limitation, as a defect. It is the nature of the web to be flexible, and it should be our role as designers and developers to embrace this flexibility, and produce pages which, by being flexible, are accessible to all.” JOHN ALLSOPP “A Dao of Web Design” ALA #58
  3. Media Queries .product { background-color: #ede5e2; } ! @media (min-width:

    500px) { .product { background-color: green; } } ! @media (min-width: 700px) { .product { background-color: blue; } }
  4. IAN STORM TAYLOR “Media Queries are a Hack” http://bit.ly/1CRGquH “Writing

    modular code is about making small objects, and making them self-contained. Media queries don’t let you do that. In most cases, you don’t actually care about the width of the entire document (or screen), you care about the width of your element.”
  5. IAN STORM TAYLOR “Media Queries are a Hack” http://bit.ly/1CRGquH “Writing

    modular code is about making small objects, and making them self-contained. Media queries don’t let you do that. In most cases, you don’t actually care about the width of the entire document (or screen), you care about the width of your element.”
  6. .product { background-color: red; } ! .product (min-width: 27em) {

    background-color: orange; } @media (min-width: 500px) { .product { background-color: green; } } ! @media (min-width: 700px) { .product { background-color: blue; } }
  7. Polyfills! Rely on data attributes (sometimes) <div class="product" data-eq-pts="small: 300,

    medium: 500"> And attribute selectors (also, sometimes) .container[data-eq-state$="small"], .container[data-eq-state$="medium"] { font-size: 1em; }
  8. Polyfills! elementQuery Tyson Matanich http://bit.ly/1xmdvti ! ! “Media Queries Are

    Not the Answer: Element Query Polyfill”
 http://bit.ly/1DwQQzM
  9. elementQuery scans the document.styleSheets collection for CSS rules <nav class="menu"

    min-width="500px"> .menu[min-width~="500px"] { background-color: #eee; } Then, it appends attributes to the appropriate selector(s)
  10. BoomQueries Uses JS to target selectors, then applies classes at

    defined breakpoints boomQueries.add('.component', [ [480, "component--md"], [600, "component--lg"] ]); .component.component--md { background: red; } ! .component.component--lg { background: orange; } Regular, ol’ CSS
  11. eq.js Data attribute & values in HTML (data-eq-state gets appended

    on resize) <div data-eq-pts="small: 300, medium: 500” data-eq-state="small"> Uses attribute selectors to apply styles .container[data-eq-state$="small"], .container[data-eq-state$="medium"] { font-size: 1em; }
  12. Set Your Goals MEDIA QUERY Use when a module is

    always the full-width of viewport ! ELEMENT QUERY Use when a module appears in multiple contexts
  13. “Most every component we build has a basic and an

    enhanced experience, though many have one or more middle levels of enhancement as well. The level of enhancement that each component receives is simply based on the features that are supported by a browser” SCOTT JEHL “Grade the Components”
  14. Grade Components, Not Browsers Each component gets a basic experience

    and an enhanced experience. ! Levels of enhancement are based on features that the browser supports
  15. PE for EQs Consider element queries to be a first-level

    enhancement. ! Fallback to mobile-first designs that work well in a single-column layout.
  16. Recap •We ❤️ media queries (but they’re kind of a

    hack) •Viewport size reporting via media queries is sometimes too broad. •We can get a component’s specific context by taking advantage of element queries. •We can use element queries today with the help of polyfills & progressive enhancement.