get on the grid - brendastorer.com · …with one exception. ie. prefixed version of grid in ie...

47
GET ON THE GRID Using CSS Grid in the Real World Brenda Storer @brendamarienyc Generate NYC – April 26, 2018 © Disney

Upload: others

Post on 14-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

GET ON THE GRIDUsing CSS Grid in the Real World

Brenda Storer@brendamarienyc

Generate NYC – April 26, 2018

© Disney

Page 2: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

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

Page 3: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

You?

Page 4: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

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

Page 5: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

Why can’t we have nice things? 😭

Page 6: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

GRID IS NICE

Page 7: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

Why?Bye Bye Vendor Prefixes 😮

Hello Feature Flags 🤗

Page 8: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

January 27, 2017

Page 9: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

Today

Page 10: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

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

Page 11: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid
Page 12: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

Step by Step Guide to writing CSS Grid

Page 13: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

Step 1: Identify a Good Use Case

Page 14: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

Flexbox vs. Grid

Page 15: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

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

Page 16: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

…or columns

Page 17: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

Uh oh.

Page 18: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

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

Page 19: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid
Page 20: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid
Page 21: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid
Page 22: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

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

consider Grid. '

Page 24: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

Step 2: Write Grid Code

Page 25: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

<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 26: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

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

Page 27: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

Step 3: Fallback gracefully with feature queries

Page 28: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

@supports

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

Page 29: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

@supports Support

Page 30: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

.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 31: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

.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 32: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

.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 33: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

.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 34: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

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

Page 35: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

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 36: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

Step 4: Ship it

Page 37: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

Everyday examples!*

Page 38: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

“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 39: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

Beyond media queriesResponsive based on content size

rather than screen size

Page 40: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

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

Control placement around unknowns. Using media queries with grid.

Page 41: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

Graceful DegradationWho made the rules that web pages must

look the same in every browser?

Page 42: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

It’s a Grid partyCentered logo in header

Using fr units instead of margin auto

Page 43: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

RESOURCES

Page 44: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

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 46: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

Resources from MeCodepen Collection of CSS Grid Examples

A Fearless Guide to Using CSS Grid Today

Page 47: GET ON THE GRID - brendastorer.com · …with one exception. IE. Prefixed version of Grid in IE only -ms Early, not fully baked version of Grid

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

http://brendastorer.com/presentations/2018-04-GenerateNYC.pdf