understanding the flex layout

15
Understanding the Flex Layout By Barak Drechsler

Upload: barak-drechsler

Post on 21-Feb-2017

56 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: Understanding the flex layout

Understanding the Flex Layout

By Barak Drechsler

Page 2: Understanding the flex layout

Flex Box Model

Page 3: Understanding the flex layout

A module of CSS that defines a CSS box model optimized for user interface design. In the flex layout model, the children of a flex container can be laid out in any direction, and can “flex” their sizes, either growing to fill unused space or shrinking to avoid overflowing the parent.

Both horizontal and vertical alignment of the children can be easily manipulated. Nesting of these boxes (horizontal inside vertical, or vertical inside horizontal) can be used to build layouts in two dimensions.

CSS Flexible Box Layout

Page 4: Understanding the flex layout

1.ability to alter its items' width and/or height to best fit in the available space on any display device.

2.flexbox layout algorithm is direction-agnostic as opposed to the block layout, which is vertically-biased, or the inline layout, which is horizontally-biased

3.Flexbox layout is most appropriate for the components of an application, and small-scale layouts, while the (emerging) Grid layout is intended for larger scale layouts.

Flex Layout agenda

Page 5: Understanding the flex layout

1. Flex Container, the parent element, that defines the flex behaviour, defined by display: flex or flex-inline.

2. Flex Item, any child of a Flex container becomes, a flex item.

3. Axes:

a. The flex-direction property establishes the main axis.

b. The justify-content property defines how flex items are laid out along the main axis on the current line.

c. The align-items property defines the default for how flex items are laid out along the cross axis on the current line.

d. The align-self property defines how a single flex item is aligned on the cross axis, and overrides the default established by align-items.

The Basics (MDN)

Page 6: Understanding the flex layout

4. order , allows to adjust flex items order.

5.Flex-wrap, flex can be defined either single line or multiline.

6. The flex property shorthands the flex-grow, flex-shrink, and flex-basis properties to establish the flexibility of the flex items.

The Basics

Page 7: Understanding the flex layout
Page 8: Understanding the flex layout

1. The flex-basis specifies initial main size of a flex item. (the size to be defined before any other factors apply).

2. The flex-grow - specifies the grow factor of each element, if any space remaining.

3. The flex-shrink - specifies the shrink factor, if there isn’t enough space for all elements to fit.

4. You should use the flex shorthand as best practice.

a. One value - flex-grow flex: 1;

b. Two values grow & shrink : flex 1 1;

c. Three Values grow & shrink & basis flex: 1 1 10%;

Flex-basis, flex-grow & flex-shrink

Page 9: Understanding the flex layout

At first the browser calculate each item size, then it check the amount of remaining space.

If extra space available each item will receive its flex grow portion, if there isn’t enough space each item will shrink by its flex shrink portion.

1. Calculate the Remaining Space: Container main size - ( sum of size of flex items baisias)

2. If Remaining Space is positive, apply growth factor.

3. If it negative apply shrink factor.

4. Growth: basis size + (remaining space * (flex-grow/ total flexs ))

5. Shrink: basis size + ( (shrink factor * basis) / (sum of bases * factors ) ) * remaining space

Flex Calculation

Page 11: Understanding the flex layout

Flex have a wide browser support in newer browsers

Although it not perfect: see Github Known Flex Bugs - most are IE 10-11 related, but not only.

Browser Support

Page 12: Understanding the flex layout

https://philipwalton.github.io/solved-by-flexbox/

Dynamic Height Overflows

Solved By Flex

Page 13: Understanding the flex layout

1. In firefox or old chromes, in order for overflow auto to work each flex item must have min-height 0, or min-width: 0 (depends on scrolling direction)

2. Because of some issues with Safari, do not use height 100% of flex: 1 items, keep nesting flex: 1 if needed.

Browser Compatibility Gotchas

Page 14: Understanding the flex layout

1. We use angular material design, use its grid system.

https://material.angularjs.org/latest/layout/introduction

Our day to day

Page 15: Understanding the flex layout

Questions?