48
You might not need media queries How to Use CSS Grid Today Brenda Storer @brendamarienyc PiterCSS – 8 June, 2018 © Disney

You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

You might not need media queries

How to Use CSS Grid TodayBrenda Storer

@brendamarienyc PiterCSS – 8 June, 2018

© Disney

Page 2: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Me@brendamarienyc Designer at thoughtbot (Front-End Dev too!)

Page 3: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

You?

Page 4: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Emotional Reaction to new CSS SpecsThe minute when browser compatibility is mentioned 😧😢😡

Page 5: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Why can’t we have nice things? 😭

Page 6: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

GRID IS NICE

Page 7: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Why?Bye Bye Vendor Prefixes 😮

Hello Feature Flags 🤗

Page 8: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

January 27, 2017

Page 9: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Today

Page 10: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

…with one exception. IE.Prefixed version of Grid in IE only -ms Early, not fully realized version of Grid

Page 11: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started
Page 12: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Step by Step Guide to writing CSS Grid

Page 13: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Step 1: Identify a Good Use Case

Page 14: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Flexbox vs. Grid

Page 15: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Flexbox is great for laying out items in one direction. In rows….

Page 16: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

…or columns

Page 17: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Uh oh.

Page 18: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Grid allows you to lay out elements in two directions, rows & columns

Page 19: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started
Page 20: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started
Page 21: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started
Page 22: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

When normally reaching for a layout property like Flexbox, floats, or positioning,

consider Grid. '

Page 24: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started
Page 25: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Step 2: Write Grid Code

Page 26: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

<ul class="search-results">  <li class="search-results__result">    <img class="search-results__photo" src="path-to-image.jpg">  </li>  <li class="search-results__result">    <img class="search-results__photo" src="path-to-image.jpg">  </li>  … more <li>s </ul>

The Markup (simplified)

Page 27: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

The CSS 🍾.search-results { display: grid; grid-gap: 10px; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); padding: 10px; }

Page 28: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Step 3: Fallback gracefully with feature queries

Page 29: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

@supports

@supports (display: grid) {  .search-results {    display: grid;    grid-gap: 10px;    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); padding: 10px;  } }

Page 30: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

@supports Support

Page 31: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

.search-results {

 display: flex;

 flex-wrap: wrap;

 padding: 5px;

}

.search-results__result {

 margin: 5px;

}

@media only screen and (min-width: 500px) {

 .search-results__result {

   width: calc(50% - 10px);

 }

}

@media only screen and (min-width: 800px) {

 .search-results__result {

   width: calc(33.3333% - 10px);

 }

}

@media only screen and (min-width: 1200px) {

 .search-results__result {

   width: calc(25% - 10px);

 }

}

Fallin’ back with Flexbox

Page 32: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

.search-results {

 display: flex;

 flex-wrap: wrap;

 padding: 5px;

}

@supports (display: grid) {  .search-results {

   display: grid;

   grid-gap: 10px;

   grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));

   padding: 10px

 }

}

Page 33: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

.search-results__result {  margin: 5px;

}

@media only screen and (min-width: 500px) {

 .search-results__result {

   width: calc(50% - 10px);  }

}

@media only screen and (min-width: 800px) {

 .search-results__result {

   width: calc(33.3333% - 10px);

 } }

@media only screen and (min-width: 1200px) {

 .search-results__result {

   width: calc(25% - 10px);

 } }

@supports (display: grid) {

 .search-results__result {

   margin: 0;

   width: auto;

 } }

Page 34: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

.search-results {

 display: flex;

 flex-wrap: wrap;

 padding: 5px;

}

@supports (display: grid) {

 .search-results {

   display: grid;

   grid-gap: 10px;

   grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));

   padding: 10px

 }

}

.search-results__result {

 margin: 5px;

}

@media only screen and (min-width: 500px) {

 .search-results__result {

   width: calc(50% - 10px);

 }

}

@media only screen and (min-width: 800px) {

 .search-results__result {

   width: calc(33.3333% - 10px);

 }

}

@media only screen and (min-width: 1200px) {

 .search-results__result {

   width: calc(25% - 10px);

 }

}

@supports (display: grid) {

 .search-results__result {

   margin: 0;

   width: auto;

 }

}

Grid code with a Flexbox fallback: Ready for production!

Page 35: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Order mattersFallback first as the default Grid code last, so it can override the default styles

Page 36: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

A few considerations…Disable Grid in Autoprefixer – This is the default for Autoprefixer v7.0 and greater

Some syntax not supported in Sass until v3.5 – Sass-rails only running Sass v3.1

Page 37: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Step 4: Ship it

Page 38: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Everyday examples!*

Page 39: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

“It’s ok that when I look back at my old code I sometimes say, ‘Whatever was I thinking?!!’ That means I’m still learning & growing.”

*disclaimer

– Brenda Storer

Page 40: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Beyond media queriesResponsive based on content size

rather than screen size

Page 41: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

What things can CSS Grid do that CSS couldn’t do before?

Control placement around unknowns. Using media queries with grid.

Page 42: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Graceful DegradationWho made the rules that web pages must

look the same in every browser?

Page 43: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

It’s a Grid partyCentered logo in header

Using fr units instead of margin auto

Page 44: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

RESOURCES

Page 45: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Layout Land - Jen Simmons

– Basics of CSS Grid: The Big Picture

Labs - Jen Simmons

Grid By Example - Rachel Andrew

– Get Started Guide

– Flexible Sized Grids with auto-fill and minmax

– The fr unit

From Bootstrap to CSS Grid - Natalya Shelburne

Page 47: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Resources from MeCodepen Collection of CSS Grid Examples

A Fearless Guide to Using CSS Grid Today

Page 48: You might not need media queries - Brenda Storer · Layout Land - Jen Simmons – Basics of CSS Grid: The Big Picture Labs - Jen Simmons Grid By Example - Rachel Andrew – Get Started

Thank you!@brendamarienyc http://brendastorer.com

http://brendastorer.com/presentations/2018-06-PiterCSS.pdf