An Event Apart: “CSS Grid Layout”

Rachel Andrew speaking at An Event Apart Orlando 2016 at Disney’s Contemporary Resort in Walt Disney World on October 4, 2016.

Since the early days of the web, designers have been trying to lay out web pages using grid systems. Likewise, almost every CSS framework attempts to implement some kind of grid system, using floats and often leaning on preprocessors. The CSS Grid Layout module brings us a native CSS Grid system for the first time—a grid system that does not rely on document source order, and can create complex layouts which are easily redefined with media queries. Following along with practical examples, you’ll learn how Grid works, and how it can be used to implement modern layouts and responsive designs.

Notes

  • Falling Cubes by Gregor Adams
  • CSS3 Working Clock by Ilia
  • Modern CSS Layout?
    • Floats
    • Inline-block
    • display: table
    • Absolute and relative positioning
    • Frameworks… lots of frameworks
  • Things we use now for layout were never designed to be used for layout
  • Our great hopes for layout:
  • Defining a grid
    • With a grid defined on the parent element, all direct children become grid items
      • display: grid; and display: inline-grid;
    • With these properties we define an explicit grid
      • grid-template-columns and grid-template-rows
      • Example — Line-based Positioning 1: 3 column tracks and 3 row tracks
        • .cards { display: grid; grid-template-columns: 250px 250px 250px; grid-template-rows: 200px 200px 200px; }
    • We can create a gap between rows and columns; This gap acts much like column-gap in multiple column layout
    • The fr unit is a fraction unit, representing a fraction of the available space in the container
      • Example: 3 equal width columns, each 1 fraction of the available space
        • .cards { display: grid; grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 200px 200px 200px; grid-gap: 20px; }
      • Example: 3 columns, the units add up to 4; The space is spilt into 4 equal parts, the first 2 tracks are given 1 part, the fine track 2 parts
        • .cards { display: grid; grid-template-columns: 1fr 1fr 2fr; grid-template-rows: 200px 200px 200px; grid-gap: 20px; }
      • Example — Defining a Grid with fr units: You can mix fraction units with other length units; Any tracks with a fraction unit share the space left after fixed size tracks and the gaps have been defined
        • .cards { display: grid; grid-template-columns: 500px 1fr 2fr; grid-template-rows: 200px 200px 200px; grid-gap: 20px; }
    • The repeat syntax lets us define a repeating pattern of tracks
    • The explicit grid is the one we define with rows and columns; If we didn’t define rows however grid would great implicit row tracks for us
    • We can define the size of implicit rows and column with the properties grid-auto-rows and grid-auto-columns
    • Use the auto-fill keyword and grid will create as many tracks that will fit into the container
    • The minmax() function enables the creation of flexible grids; The first value is the minimum size of the Grid Track, the second the max size – set that to 1fr to allow the track to take up remaining space
  • Placing items on the grid
    • Grid track
      • A Grid Track is the space between two Grid Lines
      • Tracks can be horizontal or vertical (rows or columns)
    • Grid lines
      • Lines can be horizontal or vertical
      • They are referred to by number and can be named
      • Grid lines relate to writing mode; In a right to left language such as Arabic the first column line is the right-hand line
    • Grid cell
      • The smallest unit on our grid, a Grid Cell is the space between four Grid Lines
      • It’s just like a table cell
    • Grid area
      • Any area of the Grid bound by 4 Grid Lines
      • It can contain many Grid Cells
  • Using line numbers
    • To place an item on the grid specify start and end lines using grid-column-start, grid-column-end, grid-row-start, and grid-row-end
    • These can be expressed as shorthand using grid-column and grid-row
    • They can be expressed as one line using grid-area; the order of the values is: grid-row-start, grid-column-start, grid-row-end, grid-column-end
      • Example: grid-area: 1 / 2 / 3 / 4;
  • Using line names
    • We can name lines when creating a grid; the name goes in square brackets
      • .cards { display: grid; grid-gap: 20px; grid-template-columns: [side-start] 1fr [main-start] 1fr 1fr [main-end]; grid-template-rows: [main-start] 200px 200px [main-end]; }
    • Use the name instead of the line number as the value of the placement properties
  • Lines define grid areas
    • By creating lines named main-start and main-end for rows and columns, grid has created a named grid area called main that can be used to position an element rather than the line numbers or names
  • Defining grid areas
    • This doesn’t name lines, just gives each element a name:
      • .card:nth-child(1) { grid-area: main; }
      • .card:nth-child(2) { grid-area: side1; }
      • .card:nth-child(3) { grid-area: side2; }
    • Names can then be used to describe layout as the value of grid-template-areas
  • Line-based placement
  • Grid is “table like” however…
    • Unlike a table for layout Grid does not rely on your content being a particular order in the source
    • Being entirely described in CSS we can move things around the Grid at different breakpoints, introduce or redefine a Grid for any breakpoint
  • Named grid lines
  • Named areas
  • Implicit named grid lines
    • This creates 4 implicit named lines and can be used in the same way as lines explicitly named:
      • grid-template-areas: "header header" "sidebar content" "footer footer";
  • Items on the grid can be layered with the z-index property
  • Bootstrap and other frameworks rely on describing the layout in the markup — With CSS Grid Layout we describe the layout in the CSS and can redefine that description at any breakpoint
  • When using CSS Grid Layout we have no need to describe our grid in markup
  • With Grid Layout we can easily span rows just like columns, something grid frameworks can’t do
  • Example — Layout on the 12 column grid
    • Header and footer span full grid; content and panel display side by side
  • We don’t need a Grid Layout based grid framework — It is a grid framework
  • Grid Item Placement Algorithm
    • “The following grid item placement algorithm resolves automatic positions of grid items into definite positions, ensuring that every grid item has a well-defined grid area to lay out into.” — CSS Grid Layout Module Level 1
  • grid-auto-flow
    • The default value is sparse
    • Grid will move forward placing items skipping cells if items do not fit
    • With dense mode, grid will move items out of source order to backfill spaces
  • Grid vs. Flexbox
  • Grid and Accessibility
  • Nested Grids and Subgrids
    • Example — Nested Grids
      • Make parent item a grid, then position the children
      • Children take their grid lines from the grid declared in the parent
    • Example — Subgrid
      • If we declare that this grid is a subgrid, we can then position the children of this element on the same grid their parent is placed on
    • Subgrid has not been implemented in any browsers
      • “The following features are at-risk, and may be dropped during the CR period:
  • the subgrid value” — CSS Grid Layout Module Level 1
    – Without subgrid we create the potential for accessibility problems. Authors may remove semantic markup in order to use grid layout.
    – “Subgrids are essential to the adoption of grids. I hope they’ll be implemented as soon as possible, and before grids are pushed into public release channels.” — Eric Meyer, “Subgrids Considered Essential”
  • Status of CSS Grid Layout
  • Why are we looking at something I can’t use yet?
    • Get involved with developing specs!
      • While a spec is being developed your feedback is wanted and can be included in the spec
      • Wait until browsers ship and you lose that chance
      • It just got easier. CSS Spec issues are now on GitHub
    • Do a good deed for your future self

Speaker Links and Resources

Related Links