the power of css flexbox

103
The Power of CSS Flexbox Gaurav Gupta @FrshBakedPixels #edui_flexbox

Upload: freshlybakedpixels

Post on 16-Apr-2017

1.690 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: The Power of CSS Flexbox

The Power of CSS FlexboxGaurav Gupta

@FrshBakedPixels #edui_flexbox

Page 2: The Power of CSS Flexbox

Slides tiny.cc/flexboxslides

@FrshBakedPixels #edui_flexbox

Page 3: The Power of CSS Flexbox

About me

IT Analyst

Division of Learning Innovation and Student Success

Virginia Commonwealth University

Page 4: The Power of CSS Flexbox

@FrshBakedPixels

#edui_flexbox

Page 5: The Power of CSS Flexbox

Evolution of layout methods

@FrshBakedPixels #edui_flexbox

Page 6: The Power of CSS Flexbox

Table Div + Float Grid ResponsiveWeb Design

Flexbox

Page 7: The Power of CSS Flexbox

float:left float: right

Table Div + Float Grid ResponsiveWeb Design

Flexbox

Page 8: The Power of CSS Flexbox

Table Div + Float Grid ResponsiveWeb Design

Flexbox

Page 9: The Power of CSS Flexbox

12 columns

4 columns

6 columns 6 columns

4 columns 4 columns

Table Div + Float Grid ResponsiveWeb Design

Flexbox

Page 10: The Power of CSS Flexbox

Media Queries

12 3

@media(max-width:600px){/*CSSforsmallscreens*/}

Table Div + Float Grid ResponsiveWeb Design

Flexbox

Page 11: The Power of CSS Flexbox

1

2

3

@media(max-width:600px){/*CSSforsmallscreens*/}

Table Div + Float Grid ResponsiveWeb Design

Flexbox

Media Queries

Page 12: The Power of CSS Flexbox

The CSS3 Flexible Box, or flexbox, is a layout mode providing for the arrangement of elements on a page such that the elements behave predictably when the page layout must accommodate different screen sizes and different display devices.

- Mozilla Developer Network

“Table Div + Float Grid Responsive

Web DesignFlexbox

Page 13: The Power of CSS Flexbox

Basics

@FrshBakedPixels #edui_flexbox

Page 14: The Power of CSS Flexbox

Demo code tiny.cc/flexboxdemo

@FrshBakedPixels #edui_flexbox

Page 15: The Power of CSS Flexbox

<divclass="parent"><divclass="childbox1">1</div><divclass="childbox2">2</div><divclass="childbox3">3</div><divclass="childbox4">4</div><divclass="childbox5">5</div></div>

Page 16: The Power of CSS Flexbox

12345

Page 17: The Power of CSS Flexbox

1 2 3 4 5

.parent{display:flex}

Page 18: The Power of CSS Flexbox

1 2 3 4 5

Main Axis

Cros

s Ax

is

Page 19: The Power of CSS Flexbox

1 2 3 4 5

Main Axis

Cros

s Ax

is

.parent{display:flex;flex-direction:row;/*default*/}

Page 20: The Power of CSS Flexbox

12345

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

Page 21: The Power of CSS Flexbox

12345

.parent{display:flex;flex-direction:row-reverse;}

Page 22: The Power of CSS Flexbox

12345

.parent{display:flex;flex-direction:column-reverse;}

Page 23: The Power of CSS Flexbox

Wrapping items

@FrshBakedPixels #edui_flexbox

Page 24: The Power of CSS Flexbox

1 2 3 4 5

Page 25: The Power of CSS Flexbox

1 2 3 4 5 6 7 8 9 0

Page 26: The Power of CSS Flexbox

1 2 3 4 5 6 78 9 0

.parent{display:flex;flex-wrap:wrap;}

Page 27: The Power of CSS Flexbox

1 2 3 4 5 6 78 9 0

.parent{display:flex;flex-wrap:wrap-reverse;}

Page 28: The Power of CSS Flexbox

1234567

890

.parent{display:flex;flex-direction:column;flex-wrap:wrap;}

Page 29: The Power of CSS Flexbox

Aligning items

@FrshBakedPixels #edui_flexbox

Page 30: The Power of CSS Flexbox

justify-content aligns items along main axis

align-content aligns rows of items along cross axis

align-items aligns items in a single row along cross axis

Page 31: The Power of CSS Flexbox

justify-content aligns items along main axis

align-content aligns rows of items along cross axis

align-items aligns items in a single row along cross axis

Page 32: The Power of CSS Flexbox

1 2 3 4 5

.parent{display:flex;justify-content:flex-start;/*default*/}

Page 33: The Power of CSS Flexbox

1 2 3 4 5

.parent{display:flex;justify-content:flex-end;}

Page 34: The Power of CSS Flexbox

1 2 3 4 5

.parent{display:flex;justify-content:center;}

Page 35: The Power of CSS Flexbox

1 2 3 4 5

.parent{display:flex;justify-content:space-between;}

Page 36: The Power of CSS Flexbox

1 2 3 4 5

.parent{display:flex;justify-content:space-around;}

Page 37: The Power of CSS Flexbox

justify-content aligns items along main axis

align-content aligns rows of items along cross axis

align-items aligns items in a single row along cross axis

Page 38: The Power of CSS Flexbox

1 2 34 5 67 8 9

.parent{display:flex;flex-wrap:wrap;align-content:flex-start;}

Page 39: The Power of CSS Flexbox

1 2 34 5 67 8 9

.parent{display:flex;flex-wrap:wrap;align-content:flex-end;}

Page 40: The Power of CSS Flexbox

1 2 34 5 67 8 9

.parent{display:flex;flex-wrap:wrap;align-content:center;}

Page 41: The Power of CSS Flexbox

1 2 3

4 5 6

7 8 9

.parent{display:flex;flex-wrap:wrap;align-content:space-between;}

Page 42: The Power of CSS Flexbox

1 2 3

4 5 6

7 8 9

.parent{display:flex;flex-wrap:wrap;align-content:space-around;}

Page 43: The Power of CSS Flexbox

1 2 3

4 5 6

7 8 9

.parent{display:flex;flex-wrap:wrap;align-content:stretch;/*default*/}

Page 44: The Power of CSS Flexbox

justify-content aligns items along main axis

align-content aligns rows of items along cross axis

align-items aligns items in a single row along cross axis

Page 45: The Power of CSS Flexbox

.parent{display:flex;align-items:flex-start;}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sit amet diam feugiat

ullamcorper nunc vel, sodales mi. Aliquam tempor enim at arcu finibus, et consectetur mi blandit. Vestibulum semper commodo tempor. Ut venenatis euismod

Proin ex lectus, pellentesque eget est sit amet, euismod maximus arcu

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Cras eu elementum dui. In feugiat feugiat turpis.

Page 46: The Power of CSS Flexbox

.parent{display:flex;align-items:flex-end;}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sit amet diam feugiat

ullamcorper nunc vel, sodales mi. Aliquam tempor enim at arcu finibus, et consectetur mi blandit. Vestibulum semper commodo tempor. Ut venenatis euismod

Proin ex lectus, pellentesque eget est sit amet, euismod maximus arcu

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Cras eu elementum dui. In feugiat feugiat turpis.

Page 47: The Power of CSS Flexbox

.parent{display:flex;align-items:center;}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sit amet diam feugiat

ullamcorper nunc vel, sodales mi. Aliquam tempor enim at arcu finibus, et consectetur mi blandit. Vestibulum semper commodo tempor. Ut venenatis euismod

Proin ex lectus, pellentesque eget est sit amet, euismod maximus arcu

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Cras eu elementum dui. In feugiat feugiat turpis.

Page 48: The Power of CSS Flexbox

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sit amet diam feugiat

ullamcorper nunc vel, sodales mi. Aliquam tempor enim at arcu finibus, et consectetur mi blandit. Vestibulum semper commodo tempor. Ut venenatis euismod

Proin ex lectus, pellentesque eget

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Cras eu elementum dui. In feugiat feugiat turpis.

.parent{display:flex;align-items:baseline;}

Page 49: The Power of CSS Flexbox

.parent{display:flex;align-items:stretch;/*default*/}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sit amet diam feugiat

ullamcorper nunc vel, sodales mi. Aliquam tempor enim at arcu finibus, et consectetur mi blandit. Vestibulum semper commodo tempor. Ut venenatis euismod

Proin ex lectus, pellentesque eget est sit amet, euismod maximus arcu

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Cras eu elementum dui. In feugiat feugiat turpis.

Page 50: The Power of CSS Flexbox

.parent{display:flex;align-items:stretch;}.box3{align-self:flex-end;/*overrideindividualchild*/}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sit amet diam feugiat

ullamcorper nunc vel, sodales mi. Aliquam tempor enim at arcu finibus, et consectetur mi blandit. Vestibulum semper commodo tempor. Ut venenatis euismod

Proin ex lectus, pellentesque eget est sit amet, euismod maximus arcu

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Cras eu elementum dui. In feugiat feugiat turpis.

Page 51: The Power of CSS Flexbox

Changing item order

@FrshBakedPixels #edui_flexbox

Page 52: The Power of CSS Flexbox

1 2 3 4 5

.child{order:0}/*default*/

Page 53: The Power of CSS Flexbox

1 2 345

.box3{order:1}

.box5{order:-1}

Page 54: The Power of CSS Flexbox

1 2 345

.box3{order:1}

.box5{order:-1}

order:-10001

Page 55: The Power of CSS Flexbox

“Flexing” items

@FrshBakedPixels #edui_flexbox

Page 56: The Power of CSS Flexbox

.child{flex:11200px;}

Page 57: The Power of CSS Flexbox

.child{flex:11200px;}

flex-basisflex-grow

flex-shrink

flex is applied to child elements

Page 58: The Power of CSS Flexbox

flex-basis baseline width auto

flex-grow proportion of extra width an item will get if extra space available

0

flex-shrink proportion of width it will give up if needed

1

Page 59: The Power of CSS Flexbox

.child{flex:01auto}/*default*/

Page 60: The Power of CSS Flexbox

.child{flex:10auto}

flex-grow flex-shrink

Page 61: The Power of CSS Flexbox

.child{flex:1010%}

.child{flex:1020%}

.child{flex:1025%}

Page 62: The Power of CSS Flexbox

.child{flex:1025%}

.child{flex:1125%}

Page 63: The Power of CSS Flexbox

.child{flex:10auto}

.box1{flex:100auto}

.child{flex:10auto}

.box1{flex:100auto}

.box2{flex:100auto}

Page 64: The Power of CSS Flexbox

Real world examples

@FrshBakedPixels #edui_flexbox

Page 65: The Power of CSS Flexbox

Example 1: Vertical align

@FrshBakedPixels #edui_flexbox

Page 66: The Power of CSS Flexbox

Look Ma, I'm vertically aligned!

Page 67: The Power of CSS Flexbox

Look Ma, I'm vertically aligned!

.parent{display:flex;align-items:center;justify-content:center;}

Page 68: The Power of CSS Flexbox

Look Ma, I'm vertically aligned! So am I!

.parent{display:flex;align-items:center;justify-content:center;}

Page 69: The Power of CSS Flexbox

Look Ma, I'm vertically aligned!

So am I!

.parent{display:flex;align-items:center;justify-content:center;flex-direction:column;}

Page 70: The Power of CSS Flexbox

Example 2: Equal height columns

@FrshBakedPixels #edui_flexbox

Page 71: The Power of CSS Flexbox
Page 72: The Power of CSS Flexbox

Columns with grid layout

Page 73: The Power of CSS Flexbox

Columns with flexbox

Page 74: The Power of CSS Flexbox

.row{display:flex;flex-wrap:wrap;}.card-link{flex:10250px;display:flex;flex-direction:column;}.button{margin-top:auto;}

Page 75: The Power of CSS Flexbox

Example 3: Grid vs. Flexbox

@FrshBakedPixels #edui_flexbox

Page 76: The Power of CSS Flexbox
Page 77: The Power of CSS Flexbox
Page 78: The Power of CSS Flexbox
Page 79: The Power of CSS Flexbox

col-4 col-4 col-4

Page 80: The Power of CSS Flexbox

col-3 col-3 col-3 col-3

Page 81: The Power of CSS Flexbox

? ? ? ??

Page 82: The Power of CSS Flexbox

Web app navigation

Page 83: The Power of CSS Flexbox

.parent{display:flex;}.child{flex:1010%;}

Page 84: The Power of CSS Flexbox

.parent{display:flex;}.child{flex:1010%;}

the CSS code stays the same for 3, 4 or 5 items

Page 85: The Power of CSS Flexbox

Example 4: Reordering content

@FrshBakedPixels #edui_flexbox

Page 86: The Power of CSS Flexbox
Page 87: The Power of CSS Flexbox
Page 88: The Power of CSS Flexbox
Page 89: The Power of CSS Flexbox
Page 90: The Power of CSS Flexbox
Page 91: The Power of CSS Flexbox

@media(max-width:1000px)and(orientation:landscape){.Breadcrumbs{order:1;}nav{order:2;}footer{order:3;}}

Page 92: The Power of CSS Flexbox

Page 93: The Power of CSS Flexbox

Page 94: The Power of CSS Flexbox

Example 5: Alignment Shifting Wrapping

@FrshBakedPixels #edui_flexbox

Page 95: The Power of CSS Flexbox
Page 96: The Power of CSS Flexbox

Source: https://css-tricks.com/useful-flexbox-technique-alignment-shifting-wrapping/ ≫

Page 97: The Power of CSS Flexbox

.flex-title{display:flex;align-items:flex-end;flex-wrap:wrap;}

.flex-title.title-main{flex-grow:1;}

Page 98: The Power of CSS Flexbox
Page 99: The Power of CSS Flexbox

Browser support

@FrshBakedPixels #edui_flexbox

Page 100: The Power of CSS Flexbox

Source: http://caniuse.com/#search=flexbox

Page 101: The Power of CSS Flexbox

.parent{display:flex;align-items:center;}.child{flex:01auto;}

.parent{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-box-align:center;-webkit-align-items:center;-ms-flex-align:center;align-items:center;}.child{-webkit-box-flex:0;-webkit-flex:01auto;-ms-flex:01auto;flex:01auto;}

Browser prefixes

Page 102: The Power of CSS Flexbox

Supporting older browsers

Use fallbacks .parent{display:table;display:flex;}.child{display:table-cell;width:20%;flex:1020%;}

Use Modernizr .no-flexbox.parent{display:table;}.flexbox.parent{display:flex;}

Page 103: The Power of CSS Flexbox

Resources• Flexbox cheat sheet

https://css-tricks.com/snippets/css/a-guide-to-flexbox

• Using Flexbox: Mixing Old and New for the Best Browser Support https://css-tricks.com/using-flexbox/

• Video: Enhancing Responsiveness with Flexboxhttps://vimeo.com/124796320

• Autoprefixerhttps://github.com/postcss/autoprefixer

• Modernizrhttp://modernizr.com/