css grid layout for topconf, linz
TRANSCRIPT
![Page 1: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/1.jpg)
CSS Grid Layout
Rachel Andrew
Topconf Linz, February 2016
![Page 2: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/2.jpg)
Rachel Andrew
http://rachelandrew.co.uk
@rachelandrew
http://grabaperch.com
![Page 3: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/3.jpg)
CSS in 2015 is amazing.
![Page 4: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/4.jpg)
The trouble with CSS layout
• Floats and clearfix hacks
• Absolute positioning means elements are taken out of document flow and risk overlaps
• Redundant markup and positioning oddities with display: table
• White space issues with inline-block
![Page 5: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/5.jpg)
https://www.flickr.com/photos/zervas/2810241612
Flexbox?
![Page 6: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/6.jpg)
Seeing Flexbox as the silver bullet for layout issues is likely to lead us down
another path of layout hacks.
![Page 7: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/7.jpg)
The cost of taming layout methods
• Developer hours spent learning non-obvious concepts.
• Compromises in terms of document semantics in order to achieve responsive layouts.
• Needing to lean on frameworks to help with complex math.
• Adding markup to create grids
• Using preprocessors to abstract layout hacks
![Page 8: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/8.jpg)
We need a designed for purpose layout system for the sites and applications we develop today.
![Page 9: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/9.jpg)
CSS Grid Layout
![Page 10: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/10.jpg)
Our HTML consists of a div with a class of wrapper and two child elements, a and b.
<div class="wrapper"> <div class="a">A</div> <div class="b">B</div> </div>
![Page 11: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/11.jpg)
To create a grid we use a new value of the display property.
display: grid
.wrapper { display: grid;
}
![Page 12: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/12.jpg)
We describe the grid using the new properties:
grid-template-columns
grid-template-rows
.wrapper { display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: auto auto; }
![Page 13: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/13.jpg)
We position items using the new properties:
grid-column-startgrid-column-endgrid-row-startgrid-row-end
.a { grid-column-start: 1; grid-column-end: 2; grid-row-start: 1; grid-row-end: 2; }
![Page 14: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/14.jpg)
To position an item bottom centre, I start at column line 2 and end at column line 3.
.b { grid-column-start: 2; grid-column-end: 3; grid-row-start: 2; grid-row-end: 3; }
![Page 15: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/15.jpg)
To span more tracks we just change the end row or column line.
.b { grid-column-start: 2; grid-column-end: 4; grid-row-start: 2; grid-row-end: 3; }
http://bit.ly/aeasf-simple
![Page 16: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/16.jpg)
The longhand for line-based placement means up to 4 properties to position each element. .a {
grid-column-start: 1; grid-column-end: 2; grid-row-start: 1; grid-row-end: 2; }
.b { grid-column-start: 2; grid-column-end: 4; grid-row-start: 2; grid-row-end: 3; }
![Page 17: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/17.jpg)
Declare start and end values with grid-column and grid-row.
Values are separated by a / symbol.
.a { grid-column: 1 / 2; grid-row: 1 / 2; }
.b { grid-column: 2 / 4; grid-row: 2 / 3; }
![Page 18: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/18.jpg)
Declare all 4 values using the grid-area property.
.a { grid-area: 1 / 1 / 2 / 2; }
.b { grid-area: 2 / 2 / 3 / 4; }
![Page 19: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/19.jpg)
Grid lines relate to writing mode. In a right to left language such as
Arabic the first column line is the right-hand line.
![Page 20: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/20.jpg)
Grid Terminology
![Page 21: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/21.jpg)
Grid Lines
Lines can be horizontal or vertical. They are referred to by number and can be
named.
Highlighted is Column Line 2.
![Page 22: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/22.jpg)
Grid Track
A Grid Track is the space between two Grid Lines. Tracks can be horizontal or
vertical (rows or columns).
The highlighted Grid Track is between Row Lines 2 and 3.
![Page 23: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/23.jpg)
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.
The highlighted Grid Cell is between row lines 2 and 3 and column lines 2 and 3.
![Page 24: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/24.jpg)
Grid Area
Any area of the Grid bound by 4 Grid Lines. It can contain many Grid Cells.
The highlighted Grid Area is between row lines 1 and 3, column lines 2 and 4.
![Page 25: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/25.jpg)
All examples can be found at http://gridbyexample.com. Use Chrome. Enable “Experimental Web Platform Features” flag.
![Page 26: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/26.jpg)
Line-based placement
![Page 27: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/27.jpg)
![Page 28: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/28.jpg)
The HTML around my page content.
The various areas of my page are child elements of a div with a class of wrapper.
<div class="wrapper">
<header class="mainheader"></header>
<div class="panel"></div>
<div class="content"></div>
</div>
![Page 29: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/29.jpg)
![Page 30: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/30.jpg)
Declaring a grid on wrapper.
The grid has two columns and two rows, making 3 column lines and 3 row lines.
.wrapper { width: 100%; max-width: 960px; margin: 0 auto; display: grid; grid-template-columns: 2fr 4fr; grid-template-rows: auto auto; grid-column-gap: 2em; grid-row-gap: 20px; }
![Page 31: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/31.jpg)
The fr unit
• Assigns to the track a fraction of the available space in the container.
• Before fractions are calculated the space assigned to any fixed width tracks and gaps is removed.
• Acts in a similar way to flex-grow in Flexbox.
![Page 32: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/32.jpg)
Grid Layout
I am creating three grid column tracks, all 1fr in width.
This gives me three equally sized column tracks.
.wrapper { display: grid; grid-template-columns: 1fr 1fr 1fr; }
![Page 33: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/33.jpg)
Grid Layout
If I create the first column as 600 pixels and then have two 1fr columns the 600 pixel track is removed from the available space and the remainder is distributed equally between the two columns.
.wrapper { display: grid; grid-template-columns: 600px 1fr 1fr; }
![Page 34: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/34.jpg)
Grid Layout
With a 600 pixel column, a 1fr and a 3fr column. The 600 pixels is removed from the available space then the remaining space is divided by 4.
The 1fr column gets 25% and the 3fr column 75%.
.wrapper { display: grid; grid-template-columns: 600px 1fr 3fr; }
![Page 35: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/35.jpg)
http://alistapart.com/article/holygrail
Three columns. One fixed-width sidebar for your navigation, another for, say, your Google Ads or your Flickr photos—and, as in a fancy truffle, a liquid center for the real substance.
![Page 36: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/36.jpg)
Grid Layout
CSS Grid “Holy Grail”. //css
.wrapper { display: grid; grid-template-columns: 300px 1fr 300px; grid-template-rows: auto; grid-column-gap: 20px; }
.header { grid-column: 1 / 4; }
.content { grid-column: 2 / 3; grid-row: 2 / 3; }
.side1 { grid-column: 1 / 2; grid-row: 2 / 3; }
.side2 { grid-column: 3 / 4; grid-row: 2 / 3; }
.footer { grid-column: 1 / 4; grid-row: 3 / 4; }
//html <div class="wrapper"> <header class="header">This is the header</header> <article class="content"></article> <div class="side1"></div> <div class="side2"></div> <footer class="footer"></div> </div>
![Page 37: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/37.jpg)
![Page 38: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/38.jpg)
Declaring a grid on wrapper.
The grid has two columns and two rows, making 3 column lines and 3 row lines.
.wrapper { width: 100%; max-width: 960px; margin: 0 auto; display: grid; grid-template-columns: 2fr 4fr; grid-template-rows: auto auto; grid-column-gap: 2em; grid-row-gap: 20px; }
![Page 39: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/39.jpg)
![Page 40: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/40.jpg)
Positioning our elements using the grid-column and grid-row shorthand.
This is all we need to do to create our layout.
.mainheader { grid-column: 1 / 3; grid-row: 1 / 2; }
.panel { grid-column: 1 / 2; grid-row: 2 / 3; }
.content { grid-column: 2 / 3; grid-row: 2 / 3; }
![Page 41: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/41.jpg)
![Page 43: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/43.jpg)
I can add a footer to this layout.
<div class="wrapper">
<header class="mainheader"></header>
<div class="panel"></div>
<div class="content"></div>
<footer class="mainfooter"></footer>
</div>
![Page 44: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/44.jpg)
Positioning the footer between row lines three and four.
.mainfooter { grid-column: 1 / 3; grid-row: 3 / 4; }
![Page 46: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/46.jpg)
Our grid only has 3 row lines specified - yet we placed an item between row lines 3 and 4.
Grid creates an implicit grid line for us.
.wrapper { display: grid; grid-template-columns: 2fr 4fr; grid-template-rows: auto auto; grid-column-gap: 2em; grid-row-gap: 20px; }
.mainfooter { grid-column: 1 / 3; grid-row: 3 / 4; }
![Page 47: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/47.jpg)
Grid lines can be explicit or implicit
• Explicit grid lines are those specified using grid-template-rows or grid-template-columns.
• Implicit lines are created when you place something into a row or column track outside of the explicit grid.
• Default behaviour is those tracks are auto sized. You can specify a size with the grid-auto-columns and grid-auto-rows properties.
![Page 48: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/48.jpg)
For many layouts you may be able to not specify grid-template-rows at all. Grid will create rows as you position content.
.wrapper { display: grid; grid-template-columns: 2fr 4fr; grid-column-gap: 2em; grid-row-gap: 20px; }
![Page 49: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/49.jpg)
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.
![Page 50: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/50.jpg)
Using Grid to order the page elements in a single column for narrow screen widths.
.wrapper { display: grid; grid-row-gap: 10px; } .mainheader { grid-row: 1 / 2; }
.content { grid-row: 2 / 3; }
.panel { grid-row: 3 / 4; }
.mainfooter { grid-row: 4 / 5; }
![Page 52: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/52.jpg)
Redefine the Grid at min-width 550 pixels.
Position items as in the earlier example.
@media (min-width: 550px) { .wrapper { grid-template-columns: 2fr 4fr; grid-column-gap: 2em; grid-row-gap: 20px; } .mainheader { grid-column: 1 / 3; grid-row: 1 / 2; } .panel { grid-column: 1 / 2; grid-row: 2 / 3; } .content { grid-column: 2 / 3; grid-row: 2 / 3; } .mainfooter { grid-column: 1 / 3; grid-row: 3 / 4; } }
![Page 53: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/53.jpg)
Named Grid Lines
![Page 55: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/55.jpg)
Name lines with the name in square brackets.
Remember we are naming grid lines and not grid tracks. .wrapper {
display: grid; grid-row-gap: 10px; grid-template-rows: [row-header-start] auto [row-content-start] auto [row-panel-start] auto [row-footer-start] auto [row-footer-end]; }
![Page 56: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/56.jpg)
Here we are positioning based on line numbers.
.mainheader { grid-row: 1 / 2; }
.content { grid-row: 2 / 3; }
.panel { grid-row: 3 / 4; }
.mainfooter { grid-row: 4 / 5; }
![Page 57: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/57.jpg)
Here we are positioning by named lines.
.mainheader { grid-row: row-header-start; }
.content { grid-row: row-content-start; }
.panel { grid-row: row-panel-start; }
.mainfooter { grid-row: row-footer-start; }
![Page 58: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/58.jpg)
Named Areas
![Page 60: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/60.jpg)
We assign a name to the elements on our page.
I am doing this outside of any Media Queries.
.mainheader { grid-area: header; }
.content { grid-area: content; }
.panel { grid-area: sidebar; }
.mainfooter { grid-area: footer; }
![Page 61: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/61.jpg)
Describe the layout on the parent element using the grid-template-areas property.
.wrapper { display: grid; grid-row-gap: 10px; grid-template-areas: "header" "content" "sidebar" "footer"; }
![Page 62: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/62.jpg)
![Page 63: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/63.jpg)
![Page 64: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/64.jpg)
Redefining the template areas for the wider layout. @media (min-width: 550px) {
.wrapper { grid-column-gap: 2em; grid-row-gap: 20px; grid-template-columns: 2fr 4fr; grid-template-areas: "header header" "sidebar content" "footer footer" }
}
![Page 65: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/65.jpg)
![Page 66: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/66.jpg)
![Page 67: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/67.jpg)
Repeating the name of an area causes the area to span across those grid cells.
This can be seen in the header and footer of this layout.
@media (min-width: 550px) {
.wrapper { grid-column-gap: 2em; grid-row-gap: 20px; grid-template-columns: 2fr 4fr; grid-template-areas: "header header" "sidebar content" "footer footer" }
}
![Page 68: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/68.jpg)
Repeating the name of an area causes the area to span across those grid cells.
This can be seen in the header and footer of this layout.
@media (min-width: 550px) {
.wrapper { grid-column-gap: 2em; grid-row-gap: 20px; grid-template-columns: 2fr 4fr; grid-template-areas: "header header" "sidebar content" " . footer" }
}
![Page 69: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/69.jpg)
![Page 70: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/70.jpg)
Implicit Named Grid Lines
![Page 71: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/71.jpg)
Named grid areas create four implicit named lines. You can use these in the same way as lines you have explicitly named.
.wrapper { .wrapper { grid-column-gap: 2em; grid-row-gap: 20px; grid-template-columns: 2fr 4fr; grid-template-areas: "header header" "sidebar content" "footer footer" } }
.test { z-index: 100; background-color: red; grid-column: content-start / content-end; grid-row: content-start / footer-end; }
![Page 72: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/72.jpg)
![Page 73: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/73.jpg)
Items on the Grid can be layered using the z-index property.
![Page 74: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/74.jpg)
A 12 column, flexible grid
![Page 75: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/75.jpg)
The Bootstrap grid, and those in other frameworks relies on our describing the layout in the markup.
<!-- Stack the columns on mobile by making one full-width and the other half-width --> <div class="row"> <div class="col-xs-12 col-md-8">.col-xs-12 .col-md-8</div> <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div> </div>
<!-- Columns start at 50% wide on mobile and bump up to 33.3% wide on desktop --> <div class="row"> <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div> <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div> <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div> </div>
<!-- Columns are always 50% wide, on mobile and desktop --> <div class="row"> <div class="col-xs-6">.col-xs-6</div> <div class="col-xs-6">.col-xs-6</div> </div>
![Page 76: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/76.jpg)
With CSS Grid Layout we describe the layout in the CSS and can redefine that description at any breakpoint.
![Page 78: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/78.jpg)
You can use the repeat keyword to repeat all or part of the grid definition.
This would create 4 200 pixel wide tracks, separated by a 100 pixel wide track.
grid-template-columns: repeat(4, 200px 100px);
![Page 79: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/79.jpg)
We can give multiple grid lines the same name.
This means we can use the span keyword to span n number of lines, rather than specifying a specific grid line.
.wrapper { grid-template-columns: repeat(4, [col-a] 200px [col-b] 100px); }
.content { grid-column: col-a 2 / span 2; }
.aside { grid-column: col-a 2 / span col-b 2; }
![Page 80: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/80.jpg)
The markup used to create the Grid using the Skeleton framework.
Like the Bootstrap Grid and other similar frameworks it requires classes that describe the grid to be added to the markup.
<div class="container"> <h1>Skeleton Grid</h1> <div class="example-grid"> <div class="row"> <div class="four columns">Four columns</div> <div class="four columns">Four columns</div> <div class="four columns">Four columns</div> </div> <div class="row"> <div class="eight columns">Eight columns</div> <div class="four columns">Four columns</div> </div> <div class="row"> <div class="three columns">Three columns</div> <div class="three columns">Three columns</div> <div class="three columns">Three columns</div> <div class="three columns">Three columns</div> </div> <div class="row"> <div class="six columns">Six columns</div> <div class="six columns">Six columns</div> </div> </div>
![Page 81: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/81.jpg)
![Page 82: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/82.jpg)
When using CSS Grid Layout we have no need to describe our grid in markup.
<div class="wrapper skeleton"> <h1 class="header">CSS Grid Layout Version</h1> <div class="box1">Four columns</div> <div class="box2">Four columns</div> <div class="box3">Four columns</div> <div class="box4">Eight columns</div> <div class="box5">Four columns</div> <div class="box6">Three columns</div> <div class="box7">Three columns</div> <div class="box8">Three columns</div> <div class="box9">Three columns</div> <div class="box10">Six columns</div> <div class="box11">Six columns</div> </div>
![Page 83: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/83.jpg)
Defining the 12 column grid.
The repeat keyword repeats the pattern of columns or rows the number of times specified before the comma.
.wrapper { display: grid; grid-template-columns: repeat(12, [col] 1fr ); grid-template-rows: repeat(5, [row] auto) ; grid-column-gap: 1em; grid-row-gap: 15px; }
![Page 84: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/84.jpg)
Placing box1 on the grid.
Multiple lines have the same name. This means we can use the span keyword. Here I place box1 starting at the first line named col, spanning to the 4th line.
The box is placed in the first line named row and spans 1 line - the default.
.box1 { grid-column: col / span 4; grid-row: row ; }
![Page 85: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/85.jpg)
Placing box8 on the grid.
Starting on column line 7, spanning 3 lines.
In the 3rd row named row, spanning 1 line.
.box8 { grid-column: col 7 / span 3; grid-row: row 3 ; }
![Page 86: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/86.jpg)
![Page 87: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/87.jpg)
With Grid Layout we can easily span rows just like columns.
.box1b { grid-column: col / span 4; grid-row: row / span 2; }
.box2b { grid-column: col 5 / span 4; grid-row: row / span 3; }
![Page 89: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/89.jpg)
The header and footer span the full grid.
The content and panel display side by side.
.mainheader { grid-column: col / span 12; grid-row: row ; }
.mainfooter { grid-column: col / span 12; grid-row: row 3 ; }
.content { grid-column: col 5 / span 8; grid-row: row 2 ; }
.panel { grid-column: col / span 4; grid-row: row 2 ; }
![Page 91: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/91.jpg)
The header and footer span the full grid.
The content and panel display side by side.
.mainheader { grid-column: col / span 12; grid-row: row ; }
.mainfooter { grid-column: col / span 12; grid-row: row 3 ; }
.content { grid-column: col 5 / span 8; grid-row: row 2 ; }
.panel { grid-column: col / span 4; grid-row: row 2 ; }
![Page 92: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/92.jpg)
I change three values to make our panel extend to the foot of the page.
.mainheader { grid-column: col / span 12; grid-row: row ; }
.mainfooter { grid-column: col 5 / span 8; grid-row: row 3 ; }
.content { grid-column: col 5 / span 8; grid-row: row 2 ; }
.panel { grid-column: col / span 4; grid-row: row 2 / span 2 ; }
![Page 93: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/93.jpg)
![Page 94: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/94.jpg)
Grid and Accessibility
![Page 95: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/95.jpg)
With great power comes responsibility.
![Page 96: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/96.jpg)
Power and responsibility
• Good = creating the most accessible source order and using Grid or Flexbox to get the optimal display for each device.
• Bad = using Grid or Flexbox as an excuse to forget about the source.
• Terrible - stripping out semantic elements to make everything a grid or flex item.
![Page 97: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/97.jpg)
https://drafts.csswg.org/css-grid/#source-independence
Grid item placement and reordering must not be used as a substitute for correct source ordering, as that can ruin the accessibility of the document.
![Page 98: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/98.jpg)
http://adrianroselli.com/2015/10/html-source-order-vs-css-display-order.html
![Page 99: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/99.jpg)
https://rachelandrew.co.uk/archives/2015/07/28/modern-css-layout-power-and-responsibility/
![Page 100: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/100.jpg)
Nested Grids and Subgrids
![Page 101: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/101.jpg)
In this markup the boxes e, f and g are children of the element with a class of d.
<div class="wrapper"> <div class="box a">A</div> <div class="box b">B</div> <div class="box c">C</div> <div class="box d"> <div class="box e">E</div> <div class="box f">F</div> <div class="box g">G</div> </div> </div>
![Page 102: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/102.jpg)
I have declared a grid on the wrapper div, and positioned the immediate children - the elements with classes a to d.
.wrapper { display: grid; grid-gap: 10px; grid-template-columns: repeat(4, [col] 150px); repeat(2, [row] auto); } .a { grid-column: col / span 2; grid-row: row; } .b { grid-column: col 3 / span 2; grid-row: row; } .c { grid-column: col / span 2; grid-row: row 2; } .d{ grid-column: col 3 / span 2; grid-row: row 2; }
![Page 103: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/103.jpg)
![Page 104: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/104.jpg)
To make box d a grid itself I declare a grid as normal then position the children of this element.
They take their grid lines from the grid declared on box d.
.d{ grid-column: col 3 / span gutter 2; grid-row: row 2; display: grid; grid-gap: 10px; grid-template-columns: 1fr 1fr; }
.e { grid-column: 1 / 3; grid-row: 1; }
.f { grid-column: 1; grid-row: 2; }
.g { grid-column: 2; grid-row: 2; }
![Page 105: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/105.jpg)
![Page 106: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/106.jpg)
Declaring a subgrid
![Page 107: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/107.jpg)
In our existing layout we are creating a completely new grid on box d.
.d{ grid-column: col 3 / span 2; grid-row: row 2; display: grid; grid-gap: 10px; grid-template-columns: 1fr 1fr; }
![Page 108: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/108.jpg)
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. .d{
grid-column: col 3 / span 2; grid-row: row 2; display: grid; grid-template-columns: subgrid; grid-template-rows: subgrid; }
![Page 109: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/109.jpg)
http://dev.w3.org/csswg/css-grid/
“The following features are at-risk, and may be dropped during the CR period:
the subgrid value of grid-template-columns and grid-template-rows, and its component parts individually”
![Page 110: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/110.jpg)
Without subgrid we create the potential for accessibility problems. Authors may remove semantic markup in order to use grid layout.
![Page 111: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/111.jpg)
Grid needs your feedback!
Enable Experimental Web Platform Features in Chrome.
Play with my examples and think up ways you would use Grid.
Follow the CSS Grid conversation on www-style by searching for [css-grid].
See the current issues in the Editor’s Draft http://dev.w3.org/csswg/css-grid/#issues-index
![Page 112: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/112.jpg)
![Page 113: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/113.jpg)
Browser Support
All my examples work in Chrome unprefixed - you need to enable the Experimental Web Platform Features flag.
You can also use Webkit nightlies, with the -webkit prefix.
The work in Blink and Webkit is being done by Igalia, sponsored by Bloomberg.
IE10 and up has support for the old syntax, with an -ms prefix.
Grid is on the Edge backlog, marked as High Priority.
Mozilla are currently implementing Grid in Firefox. Some examples work in Firefox Nightlies, unprefixed.
![Page 114: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/114.jpg)
Browser Support
Things change rapidly as Grid is being implemented RIGHT NOW in browsers.
I try to keep track and update http://gridbyexample.com/browsers/
![Page 115: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/115.jpg)
All examples can be found at http://gridbyexample.com. Use Chrome. Enable “Experimental Web Platform Features” flag.
![Page 116: CSS Grid Layout for Topconf, Linz](https://reader036.vdocument.in/reader036/viewer/2022062311/58705d7c1a28aba2118b6f5d/html5/thumbnails/116.jpg)
Thank you!
https://rachelandrew.co.uk/presentations/css-grid
@rachelandrew
Curated weekly CSS layout news at http://csslayout.news