M4 1 Flexbox
M4 1 Flexbox
Programming 1 Web
© 2017
Agenda
1. Flex layout algorithm
2. Container properties
3. Element properties: location
4. Element properties: responsive
5. Case study: page design
https://gitlab.com/kdg-ti/programming-1.1/web/examples/m5-flex-examples
2
Flex layout
algorithm
Inner and outer display values
• block, inline and inline-block are outer display
values: the element indicates how it wants to be
positioned in the flow layout of the outer element
(container, parent)
• In CSS3 inner display values have been added: they
modify the layout algorithm in the containing element
– they determine how the direct children of the element
are positioned and sized
– they replace the default flow layout algorithm
4
flex display values
6
Flex container
properties
Container direction: flex-direction
10
Flex-direction 1/2 nesting/flex-direction.css
<div class="wrapper">
<div class="container pillar">
<div class="container line">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
<div class="container line-rev">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
</div>
<div class="container pillar">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
<div class="container pillar-rev">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
12
</div>
Examine using browser development tools!
13
Container wrapping: Flex-wrap
.container { flex-wrap:
nowrap | wrap
| wrap-reverse;
}
14
direction + wrap shorthand: flex-flow
15
Container align on main: justify-content
.container { justify-content:
flex-start | flex-end |
center | space-between
space-around | space-evenly;
}
https://flexboxfroggy.com
do level 1-4
16
Container align on cross: align-items
.container { align-items:
flex-start| flex-end
| center | baseline
| stretch;
}
level 5-13
17
Container spacing (cross) : align-content
.container { align-content:
flex-start | flex-end
| center | space-between
| space-around | stretch;
}
18
Element
properties:
location
Element align on cross: align-self
20
Element (child) order: order
.item {order: <integer>;}
default = 0
21
Element margin
• In contrast with display:block no collapsed margins
(adjacent margins are both applied)
margin: auto;
– centers horizontally (similar to display:block; margin:auto;)
and vertically (impossible with display:block
margin:auto;)
• flex also uses (max/min) width, height and padding
remaining levels
22
Element
properties:
responsive
Element size: flex-grow
.item {flex-grow: <number>;} default = 0 (never grow)
Not negative
Excess space is allocated to elements using a factor
element's flex-grow/sum of all flex-grow's
24
Element size: flex-shrink
Defau =1
.item {flex-shrink: <number>;} Cannot be neg tive
25
Element size: flex-basis
Base size before grow/shrink along main axis
item width (row)
.item {flex-basis:
or height (column)
<size specification>|auto|content;
} size number with unit size of contents
(px,%...)
26
Element size: flex combined property
27
Element size: flex combined property
28
Case study:
page design
page/index.html
Starter page
<div class="container" >
<header>
<h1>Flexbox layout</ h1>
<p>A small flexbox demo</ p>
</header>
<main>
<h2>Main article</ h2>
<p>Lorem ...</ p>
</main>
<div class="promo1"><h3>Aside</h3></div>
<div class="promo2"><h3>Besides</ h3></div>
<footer><p>Legal disclaimer</ p></footer>
</div>
30
Use flex layout to obtain this layout/1
31
Use flex layout to obtain this layout/1
css/asides-on-row.css
.container {
display: flex;
flex-wrap: wrap;
}
header, main, footer {
flex: 100%;
ta e entire row
}
.promo1, .promo2 {
flex: 1;
grow t e u l wei ht
}
== 1 1 auto
32
Use flex layout to obtain this layout/2
33
Use flex layout to obtain this layout/2
page/main aside-on-row.html
css/main-aside-on-row.css
.container { main should be remove
display: flex;
but is overrid en b next rule anyw
flex-wrap: wrap;
}
header, main, footer,
.promo2 {
flex: 100%;
}
main {
flex: 2;
}
.promo1 {
flex: 1;
} grows twice a much as .promo1
both h ve fle -ba e: auto 34
Use flex layout to obtain this layout/3
35
Use flex layout to obtain this layout/3
css/main-asides-on-row.css
.container {
display: flex;
flex-wrap: wrap;
}
header, main, footer, .promo2
{
flex: 100%;
}
main {
flex: 3;
}
.promo1,.promo2 {
flex: 1;
}
36
Use flex layout to obtain this layout/4
37
Use flex layout to obtain this layout/4
css/main-middle.css
.container {
display: flex;
flex-wrap : wrap;
}
header, main, footer,.promo2 {
flex: 100%;
}
main {
flex: 3;
}
header{
39
Summary
1. Flex layout algorithm
2. Container properties
3. Element properties: location
4. Element properties: responsive
5. Case study: page design
40