A Practical Introduction
to CSS Grid

A Brief History
of Layout
on the Web

  1. Tables (HTML)
    Pros
    • It worked back then
    Cons
    • It's a table
    • Not responsive
    • Edit = rewrite markup
  2. Inline-Block
    Pros
    • Not a table
    • Precise widths
    Cons
    • Each column needed CSS width
    • Complicated math
    • White space created tiny gaps
    • Hack: HTML changes or font-size: 0
  3. Float
    Pros
    • Not a table
    • Precise widths
    Cons
    • Each column needed CSS width
    • Complicated math
    • Parent height collapsed
    • Hack: clearfixes
  4. Tables (CSS)
    Pros
    • Not an HTML table
    Cons
    • Needs more complicated markup (row elements)
    • Can’t mix fluid and fixed easily
  5. CSS Columns
    Pros
    • Not a table
    • Precise widths
    • Supports gaps & dividers
    Cons
    • Inner content breaks across columns
    • Browser bugginess
  6. Flexbox
    Pros
    • Not a table
    • Precise widths
    • Flexible columns sizing
    Cons
    • Each column needed CSS width / flex-basis
    • No gaps
    • Only 1-dimension (columns or rows)

Wishlist

  • CSS: responds to media queries (not markup)
  • Intentional: no float, font-size, white-space “hacks”
  • Precision: mix fluid & fixed sizes
  • 2D: both rows & columns

CSS GRID FTW

Grid gives us

  • a responsive CSS-only layout method
  • that requires only grid styles,
  • allows column/row/gap control, and
  • can put children at any column/row position

Grid
The Short
Happy Path

Grid Properties & Values

grid-template-columns

grid-template-rows

grid-template-areas

grid-template

grid-auto-columns

grid-auto-rows

grid

gap

justify-content

justify-items

align-content

align-items


grid-column-start

grid-column-end

grid-column

grid-row-start

grid-row-end

grid-row

grid-area

order

align-self

justify-self


fr

min-content

max-content

fit-content()

repeat()

minmax()

auto-fit

auto-fill

span

Today’s Grid Properties & Values

grid-template-columns

grid-template-rows

grid-template-areas

gap


grid-area


fr

repeat()

minmax()

Grid
The Bare
Minimum

.site {
  display: grid;
  grid-template-columns:
    1fr minmax(65%, 40em)
      minmax(35%, 20em) 1fr;
  grid-template-areas:
    "header header  header  header"
    "  .    content sidebar    .  "
    "footer footer  footer  footer";
  gap: 12px 16px;
}
.site__header {
  grid-area: header;
}
.site__main {
  grid-area: content;
}
.site__aside {
  grid-area: sidebar;
}
.site__footer {
  grid-area: footer;
}

Grid
Container
Properties

grid-template-columns

Set the proportions for tracks along the inline axis of the grid container

Value: a series of valid length values

  • 1fr 50em 35em 1fr
  • 30% 150px 30%

grid-template-rows

Set the proportions for tracks along the block axis of the grid container

Value: a series of valid length values

  • 1fr 2fr
  • 50px repeat(10, 100px)

grid-template-areas

  • Assign names to grid areas
  • White space doesn’t matter
  • . creates a “blank” area

grid-template-areas

.grid {
  grid-template-areas:
    "header header header  header"
    "   .   body   sidebar   .  "
    "footer footer footer  footer";
}

grid-template-areas

.grid {
  grid-template-areas:
    "👱‍♀️   👱‍♀️"
    "📄   📶"
    "🦶   🦶";
}

BONUS! grid-template

Assign rows, columns, and areas

.grid {
  grid-template:
    "header header"  auto
    "body   sidebar" auto
    "footer footer"  auto /
     40em   20em;
}

gap

  • Creates gaps between columns and rows
  • Formerly called grid-gap
  • Shorthand: row-gap column-gap
  • gap: 20px 30px

Grid
Container
Values

fr

One “fraction” of the grid’s free space

Calculated after non-fr space is used

Based on total number of fr in the row or column

300px 1fr

1fr 50em 25em 1fr

1fr 2fr 3fr

Header
Main Content
Footer

repeat()

Repeats a certain value a specified number of times

repeat(<count>, <length>)

repeat(12, 1fr)

minmax()

Finds a value between two lengths

minmax(<min>, <max>)

minmax(10px, 1fr)

Grid
Child
Properties

grid-area

Values:

  • A grid-template-areas name
  • Shorthand for
    row-start / column-start / row-end / column-end

Congrats! You know
Grid!

Practical
Grid
Examples

Sample Post Title
April 10, 2019
Draft
Sample Post Title
April 3, 2019
Published
Sample Post Title
March 25, 2019
Published
Sample Post Title
March 11, 2019
Published
Sample Post Title
March 2, 2019
Private
Sample Post Title
February 19, 2019
Published
Sample Post Title
February 8, 2019
Published
Sample Post Title
January 30, 2019
Private
.row {
  display: grid;
  grid-template:
    "title   date"   auto
    "actions status" auto /
     1fr     max-content;
  gap: .5rem;
}
Rita Matthews
District Manager
[email protected] (555) 555-6789
Rita has been managing this district for 3 years. When she’s not working, she spends time on a bunch of cool hobbies.
Awards
  • Highest Revenue Increase
  • Most Exciting Screensaver
  • Coolest Hobbies
  • Best Voicemail Message
  • Strongest Team Player
  • Most Reflective Sunglasses
  • .employee {
      display: grid;
      grid-template:
        "photo header" auto
        "photo awards" auto
        "bio   awards" 1fr /
         30%   1fr;
      gap: 1rem 3rem;
    }
    
    Rita Matthews
    District Manager
    [email protected] (555) 555-6789
    Rita has been managing this district for 3 years. When she’s not working, she spends time on a bunch of cool hobbies.
    Awards
  • Highest Revenue Increase
  • Most Exciting Screensaver
  • Coolest Hobbies
  • Best Voicemail Message
  • Strongest Team Player
  • Most Reflective Sunglasses
  • .employee {
      display: grid;
      grid-template:
        "photo  header" auto
        "bio    bio"    auto
        "awards awards" 1fr /
         150px  1fr;
        gap: 1rem;
    }
    
    Rita Matthews
    District Manager
    [email protected] (555) 555-6789
    Rita has been managing this district for 3 years. When she’s not working, she spends time on a bunch of cool hobbies.
    Awards
  • Highest Revenue Increase
  • Most Exciting Screensaver
  • Coolest Hobbies
  • Best Voicemail Message
  • Strongest Team Player
  • Most Reflective Sunglasses
  • .employee {
      grid-template:
        "photo" auto
        "header" 1fr /
         1fr;
      gap: 1rem;
    }
    
    .employee__awards,
    .employee__bio {
      display: none;
    }
    
    

    Rita Matthews
    District Manager
    [email protected] (555) 555-6789
    Rita has been managing this district for 3 years. When she’s not working, she spends time on a bunch of cool hobbies.
    Awards
  • Highest Revenue Increase
  • Most Exciting Screensaver
  • Coolest Hobbies
  • Best Voicemail Message
  • Strongest Team Player
  • Most Reflective Sunglasses
  • 1 2 3 4 5 6 7 8 9
    .image-grid {
      grid-template:
        " i1  i1  i2  i3" 1fr
        " i1  i1  i4  i3" 1fr
        " i6  i7  i4  i5" 1fr
        " i6  i8  i8  i9" 1fr /
         1fr 1fr 1fr 1fr;
      gap: 10px;
    }
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    .image-grid {
      grid-template:
        "i35 i36 i37 i38 i39 i40 i41" 1fr
        "i34 i13 i14 i15 i16 i17 i18" 1fr
        "i33 i12  i1  i1  i1  i2 i19" 1fr
        "i32 i11  i1  i1  i1  i3 i20" 1fr
        "i31 i10  i1  i1  i1  i4 i21" 1fr
        "i30  i9  i8  i7  i6  i5 i22" 1fr
        "i29 i28 i27 i26 i25 i24 i23" 1fr /
         1fr 1fr 1fr 1fr 1fr 1fr 1fr;
      gap: 10px;
    }
    

    Tips,
    Tricks &
    Gotchas

    Named Grid Areas

    One rectangle

    Adjacent cells

    Strings, not integers

    Image Sizes

    img assigned to a grid-area obeys the grid cell size, not its intrinsic aspect ratio

    Wrap images in a container element

    Fixed Heights

    Fixed heights => overflow

    Flexible row height values: auto, min-content, max-content

    Animating Grids

    Some browsers animate

    grid-template-columns

    grid-template-rows

    gap

    Can’t animate elements between areas

    Grid vs Flexbox

    Grid controls 2 dimensions

    Grid allows more precise sizing

    Grid has gap

    IE 11

    Fallback layout CSS (if possible)

    Autoprefixer can help (if necessary)

    No gap support

    Resources

    Thanks, Y’all! 👍

    James Steinbach

    Senior UX Developer
    at DockYard

    jdsteinbach

    Twitter | Github | Blog

    jds.li/activategrid