cssconf.asia - laying out the future

83
Laying out the future. Rachel Andrew, CSSConf.asia 2016

Upload: rachel-andrew

Post on 09-Jan-2017

340 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: CSSConf.asia - Laying out the future

Laying out the future.

Rachel Andrew, CSSConf.asia 2016

Page 2: CSSConf.asia - Laying out the future

Rachel Andrew

rachelandrew.co.uk

@rachelandrew

CSS Working Group Invited Expert

Google Developer Expert for Web Technologies

Co-founder Perch CMS: https://grabaperch.com

Contact: [email protected]

Page 3: CSSConf.asia - Laying out the future

Modern CSS Layout?

• Floats

• Inline-block

• display: table

• Absolute & Relative positioning

• Frameworks … lots of frameworks

Page 4: CSSConf.asia - Laying out the future

Our great hopes for layout

• Flexboxhttps://drafts.csswg.org/css-flexbox/

• CSS Grid Layouthttps://drafts.csswg.org/css-grid/

• Box Alignmenthttps://drafts.csswg.org/css-align/

Page 5: CSSConf.asia - Laying out the future

The new CSS for Layout

Page 6: CSSConf.asia - Laying out the future

Items in our layouts understand themselves as part of a complete layout.

Page 7: CSSConf.asia - Laying out the future

http://alistapart.com/article/fauxcolumns

Page 8: CSSConf.asia - Laying out the future

http://colintoh.com/blog/display-table-anti-hero

Page 9: CSSConf.asia - Laying out the future

Flexbox

Full height columns with flexbox, taking advantage of default alignment values.

.wrapper { display: flex; } .sidebar { flex: 1 1 30%; } .content { flex: 1 1 70%; }

Page 10: CSSConf.asia - Laying out the future

Grid Layout

Full height columns in CSS Grid Layout.

.wrapper { display: grid; grid-template-columns: 30% 70%; } .sidebar { grid-column: 1; } .content { grid-column: 2; }

Page 11: CSSConf.asia - Laying out the future

Separation of source and display

Page 12: CSSConf.asia - Laying out the future

Flexbox

The flex-direction property can take a value of row to display things as a row or column to display them as a column.

nav ul{ display: flex; justify-content: space-between; flex-direction: row; }

Page 13: CSSConf.asia - Laying out the future

Flexbox

The visual order can be switched using row-reverse or column-reverse.

nav ul{ display: flex; justify-content: space-between; flex-direction: row-reverse; }

Page 14: CSSConf.asia - Laying out the future

Flexbox

Adding display: flex to our container element causes the items to display flexibly in a row.

.wrapper { display: flex; }

Page 15: CSSConf.asia - Laying out the future

Flexbox

The order property means we can change the order of flex items using CSS.

This does not change their source order.

li:nth-child(1) { order: 3; } li:nth-child(2) { order: 1; } li:nth-child(3) { order: 4; } li:nth-child(4) { order: 2; }

Page 16: CSSConf.asia - Laying out the future

Grid Layout

I have created a grid on my wrapper element.

The grid has 3 equal width columns.

Rows will be created as required as we position items into them.

.wrapper { display: grid; grid-template-columns: 1fr 1fr 1fr; }

Page 17: CSSConf.asia - Laying out the future

Grid Layout

I am positioning my elements using CSS Grid Layout line-based positioning.

Setting a column and a row line using the grid-column and grid-row properties.

li:nth-child(1) { grid-column: 3 ; grid-row: 2 ; }

li:nth-child(2) { grid-column: 1 ; grid-row: 2 ; }

li:nth-child(3) { grid-column: 1 ; grid-row: 1 ; } li:nth-child(4) { grid-column: 2 ; grid-row: 1 ; }

Page 18: CSSConf.asia - Laying out the future
Page 19: CSSConf.asia - Laying out the future

CSS Grid automatic placement

https://drafts.csswg.org/css-grid/#grid-auto-flow-property

https://rachelandrew.co.uk/archives/2015/04/14/grid-layout-automatic-placement-and-packing-modes/

Page 20: CSSConf.asia - Laying out the future

Grid Layout

A list of colours.<ul class="colors">

<li style="background-color:#FFFFFF;color:black" class="white">FFF FFF </li> <li style="background-color:#CCCCCC;color:black">CCC CCC </li> <li style="background-color:#999999;color:black" class="span3">999 999 </li>

</ul>

Page 21: CSSConf.asia - Laying out the future

Grid Layout

Creating a flexible grid with a flexible number of columns.

.colors { display: grid; grid-template-columns: repeat(auto-fill,minmax(80px, 1fr)); grid-gap: 2px; grid-auto-rows: minmax(80px, auto);

}

Page 22: CSSConf.asia - Laying out the future
Page 23: CSSConf.asia - Laying out the future

Grid Layout

Positioning the white and black blocks to stretch across the grid.

Spanning tracks for some other items.

.white { grid-column: 1 / -1; grid-row: 3; } .black { grid-column: 1 / -1; grid-row: 6; } .span2 { grid-column-end: span 2; grid-row-end: span 2; } .span3 { grid-column-end: span 3; grid-row-end: span 3; } .tall4 { grid-row-end: span 4; }

Page 24: CSSConf.asia - Laying out the future
Page 25: CSSConf.asia - Laying out the future

Grid Layout

Change the packing mode to dense.

.colors { display: grid; grid-template-columns: repeat(auto-fill,minmax(80px, 1fr)); grid-gap: 2px; grid-auto-rows: minmax(80px, auto); grid-auto-flow: dense; }

Page 26: CSSConf.asia - Laying out the future
Page 27: CSSConf.asia - Laying out the future

With great power comes responsibility.

Page 28: CSSConf.asia - Laying out the future

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 29: CSSConf.asia - Laying out the future

https://drafts.csswg.org/css-flexbox/#order-accessibility

Authors must use order only for visual, not logical, reordering of content. Style sheets that use order to perform logical reordering are non-conforming.

Page 30: CSSConf.asia - Laying out the future

Léonie Watson | On CSS accessibility and drinking tea | CSS Day 2016

https://vimeo.com/180566024

Page 31: CSSConf.asia - Laying out the future

Control of alignment

Page 32: CSSConf.asia - Laying out the future

CSS Box Alignment Module Level 3

“This module contains the features of CSS relating to the alignment of boxes within their containers in the various CSS box layout models: block layout, table layout, flex layout, and grid layout.” - https://drafts.csswg.org/css-align/

Page 33: CSSConf.asia - Laying out the future

It’s 2016. We can now centre things.

Page 34: CSSConf.asia - Laying out the future

Box Alignment Properties

justify-content

align-content

justify-self

align-self

justify-items

align-items

Page 35: CSSConf.asia - Laying out the future

Flexbox

The justify-content property is set to space-between.

The items at each end are placed against the container and the remaining space distributed evenly.

nav ul{ display: flex; justify-content: space-between; flex-direction: row; }

Page 36: CSSConf.asia - Laying out the future

Grid

If there is space in the grid container after all column and row tracks have been added.

Use space-around and space-between to space the tracks.

.wrapper { width: 500px; height: 400px; display: grid; grid-gap: 10px; grid-template-columns: repeat(4, 80px); grid-template-rows: repeat(3,100px); align-content: space-around; justify-content: space-between; }

Page 37: CSSConf.asia - Laying out the future

I can create this same layout with flexbox or Grid.

With flexbox the items are laid out in a row.

.wrapper { display: flex; }

.wrapper li { flex: 1 0 25%; }

Page 38: CSSConf.asia - Laying out the future

The first item is at the default stretch and as the tallest item is dictating the height of the flex container.

The second is entered in the container.

The third aligned to the start.

The fourth aligned to the end.

.wrapper li:nth-child(2) { align-self: center; }

.wrapper li:nth-child(3) { align-self: flex-start; }

.wrapper li:nth-child(4) { align-self: flex-end; }

Page 39: CSSConf.asia - Laying out the future

For Grid I use a single row, 4 column Grid.

.wrapper { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; }

Page 40: CSSConf.asia - Laying out the future

Grid alignment properties for the three landscape images.

.wrapper li:nth-child(2) { align-self: center; }

.wrapper li:nth-child(3) { align-self: start; }

.wrapper li:nth-child(4) { align-self: end; }

Page 41: CSSConf.asia - Laying out the future

Responsive by default

Page 42: CSSConf.asia - Laying out the future

Ethan Marcotte, Fluid Grids

“… every aspect of the grid—and the elements laid upon it—can be expressed as a proportion relative to its container.”

Page 43: CSSConf.asia - Laying out the future

target ÷ context = result

h1 {

margin-left: 14.575%; /* 144px / 988px = 0.14575 */ width: 70.85%; /* 700px / 988px = 0.7085 */

}

Page 44: CSSConf.asia - Laying out the future

Flexbox

The most simple flexbox example demonstrates the inherent flexibility.

The items will be displayed as a row, with equal space between each item.

nav ul{ display: flex; justify-content: space-between; }

Page 45: CSSConf.asia - Laying out the future

The flex property

• flex-grow - add space

• flex-shrink - remove space

• flex-basis - the initial size before any growing or shrinking

Page 46: CSSConf.asia - Laying out the future

Flexbox

flex: 1 1 300px;

flex-grow: 1

flex-shrink: 1;

flex-basis: 300px;

The initial width of our li is 300 pixels, however it can grow larger and shrink smaller than 300 pixels.

.wrapper { display: flex; } .wrapper li { flex: 1 1 300px; min-width: 1px; }

Page 47: CSSConf.asia - Laying out the future
Page 48: CSSConf.asia - Laying out the future

Flexbox

flex: 1 1 300px;

flex-grow: 1

flex-shrink: 1;

flex-basis: 300px;

If we allow the flex items to wrap we can see how flex-basis works by dragging the window smaller.

.wrapper { display: flex; flex-flow: row wrap; justify-content: space-around; } .wrapper li { flex: 1 1 300px; min-width: 1px; }

Page 49: CSSConf.asia - Laying out the future
Page 50: CSSConf.asia - Laying out the future

Flexbox

flex: 1 1 300px;

flex-grow: 1;

flex-shrink: 1;

flex-basis: 300px;

The 3rd item has

flex: 0 1 300px;

so cannot grow.

.wrapper { display: flex; } .wrapper li { flex: 1 1 300px; min-width: 1px; }

.wrapper li:nth-child(3) { flex: 0 1 300px; }

Page 51: CSSConf.asia - Laying out the future
Page 52: CSSConf.asia - Laying out the future

Flexbox

If we set the 3rd item to flex-grow: 2

This item will be assigned twice of much of the available free space after we have reached the 300 pixel initial width.

.wrapper { display: flex; } .wrapper li { flex: 1 1 300px; min-width: 1px; }

.wrapper li:nth-child(3) { flex: 2 1 300px; }

Page 53: CSSConf.asia - Laying out the future
Page 54: CSSConf.asia - Laying out the future

http://madebymike.com.au/demos/flexbox-tester/

Page 55: CSSConf.asia - Laying out the future

The CSS Grid Layout fr unit

Page 56: CSSConf.asia - Laying out the future

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 57: CSSConf.asia - Laying out the future

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 58: CSSConf.asia - Laying out the future

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 59: CSSConf.asia - Laying out the future
Page 60: CSSConf.asia - Laying out the future

Flexbox for 1 dimensional layout.

CSS Grid is for 2 dimensional layout.

Page 61: CSSConf.asia - Laying out the future
Page 62: CSSConf.asia - Laying out the future

The value of the grid-template-columns property says:

repeat this track listing, auto-filing as many columns with a minimum width of 300 pixels and a maximum of 1fr.

.wrapper { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }

Page 63: CSSConf.asia - Laying out the future
Page 64: CSSConf.asia - Laying out the future

Bringing it all together

Page 65: CSSConf.asia - Laying out the future
Page 66: CSSConf.asia - Laying out the future
Page 67: CSSConf.asia - Laying out the future
Page 68: CSSConf.asia - Laying out the future

Using the minmax() function with grid-auto-rows.

.home-hero { display: grid; grid-gap: 1px; grid-auto-rows: minmax(150px, auto); }

Page 69: CSSConf.asia - Laying out the future

An item on the grid can become a grid or flex container itself.

In this case I am using flexbox and auto margins to push my content to the bottom of the box.

.special { display: flex; flex-direction: column; }

.special h3{ margin-top: auto; }

Page 70: CSSConf.asia - Laying out the future
Page 71: CSSConf.asia - Laying out the future

http://codepen.io/collection/DVKrKJ/

Page 72: CSSConf.asia - Laying out the future

http://gridbyexample.com

Page 73: CSSConf.asia - Laying out the future

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

Page 74: CSSConf.asia - Laying out the future

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

Page 75: CSSConf.asia - Laying out the future

http://gridbyexample.com/browsers

Page 76: CSSConf.asia - Laying out the future

Feature Queries - your new best friend

Page 77: CSSConf.asia - Laying out the future

Feature Queries

Test for support of property: value pairs.

@supports (display: grid) { .has-grid { /* CSS for grid browsers here */

} }

Page 78: CSSConf.asia - Laying out the future

Using Feature Queries

• Write CSS for browsers without support

• Override those properties inside the feature queries

• See https://hacks.mozilla.org/2016/08/using-feature-queries-in-css/

Page 79: CSSConf.asia - Laying out the future

Grid tips for Feature Queries

• Floated items that become grid or flex items lose their float behaviour

• vertical-align has no effect on a grid item

• Items set to display: inline-block or block become grid items

• Your overrides mostly change widths, margins and padding.

• If grid tracks or flex-basis seem to be using a size you didn’t expect, check your item widths!

Page 80: CSSConf.asia - Laying out the future

http://codepen.io/rachelandrew/pen/WGNrPW

Page 81: CSSConf.asia - Laying out the future

http://caniuse.com/#feat=css-featurequeries

Page 82: CSSConf.asia - Laying out the future

Anything new in CSS you can use feature queries to detect support.

Page 83: CSSConf.asia - Laying out the future

Thank you

Slides & Resources: https://rachelandrew.co.uk/speaking/event/cssconf-asia-2016

http://csslayout.news - sign up for my weekly CSS Layout email

@rachelandrew | [email protected]

https://rachelandrew.co.uk | https://grabaperch.com