I'll show you CSS3 Grid Layout, and I do, in fact, use it in those slides. Unfortunately, it is not widely supported.

Make sure your prowser supports it. For example, in Chrome you can enable it through the "experimental Web Platform features" flag in chrome://flags

CSS Grid Layout

Tomek Wytrębowicz

@tomalecpl

tomalec

the only grid framework you need to know

live-coding video: youtu.be/8xQWMf0NH_s

The Grid

@tomalecpl
@tomalecpl

Gestalt

That's the way we perceive visual information

@tomalecpl

How we can do it?

@tomalecpl
<table>
  • conceptually similar
  • works!
  • mixes content with styles
  • layout is not a tabular data
  • spacer gifs & co.
  • table layout may be unpredictable

W3C: CSS Layout HOWTO

@tomalecpl

Monthy Pyton, video from youtu.be/YgYEuJ5u1K0

position: absolute;
  • concerns separated! ..almost
  • div-soup 🍜
  • hard to achieve
  • taking responsibility for document flow
  • fixed position are not very responsive
@tomalecpl
float: left | right;
  • great to float an image
  • job security
  • hard to achieve
  • hard to maintain
  • clear fixes hell
  • div-soup 🍜
@tomalecpl
display: table;
display: table;
  • concerns separated
  • cheating ...
  • all problems related to <table>, except separation
  • no row-/colspans
@tomalecpl

Monthy Pyton, video from youtu.be/YgYEuJ5u1K0

display: flex;
  • cool & fresh
  • easy
  • meaningful
  • responsive
  • cross-browser support
  • single dimension
  • requires nesting 🍜
  • no row-/colspans
@tomalecpl
CSS frameworks
  • +∞ to choose from
  • not your problem
  • cheating...
    same hacks = same problems
  • yet another class langauge to learn
  • maintenance
  • vendor locking
  • mixability
  • overhead
@tomalecpl

What is the right way?

@tomalecpl
#container {
  display: 
grid;
}

It works!

@tomalecpl

CSS Grid Layout

https://www.w3.org/TR/css-grid-1/

  • no div-soup
  • semantic HTML
  • meaningful CSS
  • responsive
  • easy to maintain
  • easy to replace
  • no vendor locking
  • no dependencies
  • no glue code
@tomalecpl

The right way


<div class="container">
  <div>Logo</div>
  <div>Header</div>
  <div>Nav</div>
  <div>Main content</div>
</div>
                    
Logo
Header
Nav
Main content

Non-live coding hint

.container{
  display: grid;
  grid-template-columns: 4em 1fr;
  grid-template-rows: 2em 1fr;
}
Then resize the result box.
@tomalecpl

CSS is finally aware of 2D layout concepts!

@tomalecpl

Example

 

Header
Main
Nav
Footer
@tomalecpl

Grid Lines

 

Header
Main
Nav
Footer
1
2
3
1
2
3
4
@tomalecpl

Grid Tracks

Rows

Header
Main
Nav
Footer
1
2
3
@tomalecpl

Grid Tracks

Columns

Header
Main
Nav
Footer
1
2
@tomalecpl

Grid Cells

 

Header
Main
Nav
Footer
@tomalecpl

Grid Areas

 

Header
Main
Nav
Footer
@tomalecpl

The code!

Grid container


<div class="container">
  <div>A</div>
  <div>B</div>
</div>
<div class="smth"></div>
                    
A
B
inline block

Non-live coding hint

.container {
  display: inline-grid; /* grid */
  border: 3px solid #FFC;
}
.smth{
  display: inline-block;
}
  • Separate content and layout
@tomalecpl

Track sizing


<div class="container">
  <div class="a">A</div>
  <div class="b">B</div>
  <div class="c">C</div>
  <div class="d">D</div>
  <div class="e">E</div>
</div>
                    
A
B
C
D
E

Non-live coding hint

.container{
  display: grid;
  grid-template-columns: minmax(150px, 1fr) 20% minmax(min-content, 2fr);/* px/em/%/.. auto, fr, minmax()*/
  grid-template-rows: auto 4em;
  /* direction: rtl; */
  /* grid-template: 2em 1fr / 4em 1fr; */
  /* grid: 2em 1fr / 4em 1fr;  */
}
Then resize/shrink the result box.
  • No div soup
  • No layout-specific HTML nodes (spacers, clearfixes, wrappers, etc.)
@tomalecpl

Items placement


<div class="container">
  <div class="a">A</div>
  <div class="b">B</div>
  <div class="c">C</div>
  <div class="d">D</div>
  <div class="e">E</div>
</div>
                    
A
B
C
D
E

Non-live coding hint

.a{ order: 1;} /* 1 is grater than nothing */
.c{ order: 2;}
.b{ grid-column: 3;}
.c{ grid-column: 1 / span 2;}
.d{ grid-row: 1/3; /* grid lines not tracks */}
  • No HTML manipulations/JS needed to sort, re-arrange things
  • Accessibility, SEO
  • Row- & colspans
  • Precise position controlled by browser
@tomalecpl

Numbers, numbers..
Do I really have to know the math?!

Named grid lines


<div class="container">
  <div class="a">A</div>
  <div class="b">B</div>
  <div class="c">C</div>
  <div class="d">D</div>
  <div class="e">E</div>
</div>
                    
A
B
C
D
E

Non-live coding hint

.container{
	display: grid;
	grid-template-columns: [left] 4em [center] 1fr [right prawo];
	grid-template-rows: [top] 1.5em [hero] 3em [content] 1fr [bottom];
}
.a{
  grid-column: left / right;
}
.b{
  grid-column: center / 3;
  grid-row: 2;
}
.c{
  grid-row: 2 / content;
}
  • Easy and meaningful code
  • No need for layout related classes in HTML
@tomalecpl

Named areas


<div class="container">
  <header>Header</header>
  <nav>Nav</nav>
  <div>Content</div>
  <footer>Footer</footer>
</div>
                    
Header
Content
Footer

Non-live coding hint

.container {
  display: grid;
  grid: 1.5em 1fr / 5em 1fr; /* auto in 3rd row is comes as default  */
  grid-template-areas: "head head"
                       "nav main"
                       "foot foot";
}
.container header{
  grid-area: head;
}
.container nav{
  grid-area: nav;
}
.container div{
  grid-area: main;
}
.container footer{
  grid-area: foot;
}
Then resize the result box.
  • Easy and meaningful code
  • No need for layout related classes in HTML
  • You can control entire layout at single point and single propery
@tomalecpl
@tomalecpl

Grid framework?

@tomalecpl

Write your own

@tomalecpl

Can I use it?

Production

source: caniuse.com/#feat=css-grid

@tomalecpl

Development

IE/Edge
IE/Edge
Prefixed with -ms Old spec/syntax, but quite stable
Chrome/Opera
Chromium
⚑ behind the flag
experimental-web-platform-features
Latest spec
Firefox
Firefox
⚑ behind the flag
layout.css.grid.enabled
Webkit
Webkit
Prefixed with -webkit Enabled in Webkit nightlies
Polyfillgithub.com/FremyCompany/css-grid-polyfill
@tomalecpl

Learn and contribute

Place to start fiddling with it

Make the awesome happen!

@tomalecpl

Thanks!

Tomek Wytrębowicz

tomalec

@tomalecpl

My other talks:

Specs:

Place to start fiddling with it

CSS project:

Other: