CSS Flexbox Layout
CSS Flexbox Layout
Web Development
Joan Boone
jpboone@email.unc.edu
Slide 1
Part 1: Flexbox Overview
Part 2: Multi-column Layout
Part 3: Card Layout
Part 4: Media Object Pattern
Part 5: Centering Content Pattern
Part 6: Header and Footer Patterns
Part 7: Sticky Header/Footer Patterns
Slide 2
Flexbox Resources
Introduction
• MDN Web Docs: Flexbox
• w3schools: CSS Flexbox
• CSS-Tricks: A Complete Guide to Flexbox
Specification
• CSS Flexible Box Layout Module
• Browser support
Slide 3
What is Flexbox Really For?
The key phrase in the Flexbox specification is
“distributing space and aligning content”
• Flexbox is all about taking a bunch of things of varying sizes and fitting
them into a container which itself has a varying size.
• Flexbox is squishy.
• Flexbox tries to create the best possible layout for items, giving bigger
items more space and smaller items less space, thus preserving the
readability of content.
• If you find Flexbox hard and weird, it is often because you are trying to use
Flexbox as a grid system.
If so, you are fighting against the very thing that makes it Flexbox, i.e., its
inherent flexibility.
• Patterns that suit flex layout well are those where we are not so interested
in having a pixel-perfect size for each.
We just want them to display well alongside each other.
CSS Flexbox is a model where you can lay out elements in a row or
column. The elements “flex” their sizes, either by growing to fill unused
space or shrinking to avoid overflowing the parent container
• Flex Container is the element on which display:flex is applied
• Flex Items are the children, or direct descendants, of the Flex Container
flex-wrap
align-self
Slide 10
Basic Flexbox Layout
Flex container Flex items
.wrapper {
display: flex; CSS
flex-direction: column;
gap: .5em;
}
CSS Flexbox defines a one-dimensional layout that is either row or column oriented.
A flexbox is defined by a flex container – this is the HTML element with a
display:flex property. The flex items are the children, or direct descendants, of
the flex container.
flex-direction defines the orientation. By default, it is row-oriented.
gap defines the size of the gap between rows and columns.
basic-flex.html Slide 11
2-Column Flexbox Layout
using a nested flexbox HTML
<div class="wrapper">
<header>Header</header>
<div class="content-wrapper">
<aside>
<p>Sed ut ...</p><p>Nemo ....</p>
</aside>
<section>
<p>Lorem ...</p><p>Duis aute ...</p>
</section>
</div>
<footer>Footer</footer>
</div>
CSS
.wrapper { .content-wrapper {
display: flex; display: flex;
flex-direction: column; flex-direction: row;
gap: .5em; gap: .5em;
} }
basic-2-column-flex.html Slide 12
Responsive
2-Column Flexbox
.wrapper {
display: flex;
flex-direction: column; @media (min-width: 64em) {
gap: .5em;
.content-wrapper {
}
flex-direction: row;
.content-wrapper { }
display: flex;
flex-direction: column; aside { flex: 1; }
gap: .5em; section { flex: 2; }
} }
responsive-2-column-flex.html Slide 13
Part 1: Flexbox Overview
Part 2: Multi-column Layout
Part 3: Card Layout
Part 4: Media Object Pattern
Part 5: Centering Content Pattern
Part 6: Header and Footer Patterns
Part 7: Sticky Header/Footer Patterns
Slide 14
Card Layout with Flexbox (without media query)
Cards have a
minimum width (15em)
that “grows” as needed
to fill available space
* { box-sizing: border-box; }
section {
... Include element's
display: flex; border and padding
flex-wrap: wrap; when determining size
}
article {
...
width: 15em; /* 240px */
flex-grow: 1;
Mobile-first /* 2 lines above can be replaced with */
/* flex: 1 1 15em; */
}
article {
...
width: 100%;
Set the width so that }
no more than 2 @media (min-width: 50.0em) {
Mobile-first cards will fit in a row article {
width: 35%;
flex-grow: 1;
}
}
flex-cards-media-query.html
Slide 16
Nested Flexboxes
with and without a media query
“Responsive by default”
strawberry-media-query.html
strawberry-no-media-query.html
Slide 17
“Responsive by Default” Flexbox
Source: Using Media Queries for Responsive Design in 2018
.flex {
display: flex;
flex-wrap: wrap;
}
.flex > * {
flex: 1 1 250px;
margin: 10px;
}
responsive-by-default-flex.html Slide 18
“Responsive by Default”
Flexbox vs. Grid for Card Layout
Flexbox fills all of the available
space with the cards, although
this can result in different
sized cards.
.wrapper > * {
flex: 1 1 250px;
margin: 2%;
}
card-flex.html Slide 19
Part 1: Flexbox Overview
Part 2: Multi-column Layout
Part 3: Card Layout
Part 4: Media Object Pattern
Part 5: Centering Content Pattern
Part 6: Header and Footer Patterns
Part 7: Sticky Header/Footer Patterns
Slide 20
Media Object Pattern
The Media Object pattern is found everywhere – it refers to a two-column
box with an image on one side and descriptive text on the other.
References
• Solved by Flexbox: Media Object
• Some variations: Flexbox Media Object Examples
• CSS-Tricks: How to create a media object with Grid
Slide 21
Part 1: Flexbox Overview
Part 2: Multi-column Layout
Part 3: Card Layout
Part 4: Media Object Pattern
Part 5: Centering Content Pattern
Part 6: Header and Footer Patterns
Part 7: Sticky Header/Footer Patterns
Slide 22
Centering Content Pattern
“wrapper” flex container
for overall page layout
“content-wrapper” flex container
for sidebar and main content
HTML
aside element flex container for
logo and label
flex-centering-content.html Slide 23
Centering Content Pattern
<aside>
<i class="fas fa-sun fa-4x"></i>
<p>My logo goes here</p>
</aside> HTML
aside {
display: flex; CSS
flex-direction: column;
HTML align-items: center;
justify-content: center; }
flex-centering-content.html
Slide 24
Part 1: Flexbox Overview
Part 2: Multi-column Layout
Part 3: Card Layout
Part 4: Media Object Pattern
Part 5: Centering Content Pattern
Part 6: Header and Footer Patterns
Part 7: Sticky Header/Footer Patterns
Slide 25
Header (or navigation) Pattern
<footer class="footer-container">
<p>How to find me...</p>
<a href=""><i class="fas fa-envelope-square fa-2x"></i></a>
<a href=""><i class="fab fa-twitter-square fa-2x"></i></a>
<a href=""><i class="fab fa-instagram-square fa-2x"></i></a>
<a href=""><i class="fab fa-facebook-square fa-2x"></i></a>
<a href=""><i class="fab fa-linkedin fa-2x"></i></a>
</footer>
.footer-container {
background-color: slateblue;
display: flex; These are references to
flex-wrap: wrap; Font Awesome icons
justify-content: flex-start;
}
header-footer.html
Slide 27
Part 1: Flexbox Overview
Part 2: Multi-column Layout
Part 3: Card Layout
Part 4: Media Object Pattern
Part 5: Centering Content Pattern
Part 6: Header and Footer Patterns
Part 7: Sticky Header/Footer Patterns
Slide 28
Sticky Footer Problem
How to force the footer to stick to the bottom of the page when the
page has sparse content?
Slide 29
Sticky Footer Solution
How to force the footer to stick to the bottom of the page when the
page has sparse content?
• Add a wrapper <div> around
the page content (header,
aside, section, footer)
• Make the wrapper a column-
oriented flex container
• Set its min-height:
100vh; This corresponds to
100% of the browser viewport
height
• For the .content-flex-
container (aside, section),
set flex:1 -- forces main
content to use all vertical
space.
Source: Solved by Flexbox: Sticky Footer sticky-footer-flex-2column.html Slide 30
Sticky Header + Scrolled Content
Header sticks to the top of the window and content scrolls behind it