03 CSS-Responsive-Design
03 CSS-Responsive-Design
1
Outline
1. Flexbox
2. Grid
3. Media Queries
2
Responsive Web Design (RWD)
• RWD is an approach to serve different layouts for
different screen sizes
o Optimize the viewing experience on range of devices:
mobile, desktop, tablet, TV…
o Can be accomplished using CSS grid/flexbox & media
queries
o Mobile-first layouts work well on all screen widths: start with single
column layout for smaller screens
3
3
4
Flexbox
• The Flexbox provide an efficient way to lay out, align
and distribute space among items in a container
o Defines one-dimensional layout
o A flex container stretches items to fill available free space
or shrinks them to prevent overflow
.flex-container {
display: flex; Container Items
gap: 1rem;
justify-content: center;
}
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
https://www.w3schools.com/css/css3_flexbox.asp
5
Flex Container Properties
• flex-direction: either row (default) or column
• flex-wrap: nowrap (default) all flex items will be
on 1 line. Assign wrap to allow flex items to
wrap onto multiple lines
• justify-content: aligns and arranges flex-items
along the main axis
• align-items: aligns items within a flex line, along
the cross-axis
• align-content: aligns and manages spacing
between multiple lines when items wrap
6
row (default):
horizontal
alignment
7
• nowrap (default): all flex items will be on one line
• wrap: flex items will wrap onto multiple lines
8
justify-content
9
align-items
10
align-content
11
flex items - order & flex-basis
order: changes the
display order of the flex
item
flex-grow: determines
how the flex item is
allowed to grow
flex-shrink: allows
an item to shrink if
necessary
13
flex items - align-self
• align-self: overrides default alignment (or the one
specified by align-items) for a specific item along the
Cross Axis
14
15
CSS Grid
• CSS Grid is a two-dimensional layout system to
design the page layout
• Can specify columns/rows
template
• Grid elements can be auto-placed
or explicitly placed using grid lines
or grid areas
• Easy control of space distribution
and alignment of items
16
Grid container & Grid items
• Grid container is .page {
defined by setting the display: grid;
}
display property of
the container <div class="page">
element to grid
<header class="head">
</header>
<main class="main-content">
<aside class="sidebar">
that is a direct </aside>
grid container
</div>
17
Grid Template Columns
1 2 3 4
grid-template-columns:
2fr 1fr 1fr;
18
Grid rows
1 2 3 4
grid-template-rows: 1
px, %, fr)
- Optional, only define it
when really needed
4
19
grid-template-rows & grid-template-columns
https://devopedia.org/css-grid-layout 20
grid-gap
.page {
display: grid;
grid-gap: .5rem;
}
Defines space (i.e., gutter) between
grid tracks (as shown in blue)
grid-auto-flow
Defines how to automatically
place grid items that aren't
explicitly placed
(row if the default)
21
Placing Items using Grid Lines
Start at column line 2 End at column line 4
.head {
1 2 3 4
grid-column: 2/4; 1
w line 1
Start at ro
grid-row: 1/2;
End a
} t row
line
2
2
grid-column: 2/4; 3
Or grid-column: 2/ span 2;
header {
grid-column: 1 / span 3;
}
.left-side {
grid-column: 1 / 2;
}
main {
grid-column: 2 / 3;
}
.right-side {
grid-column: 3 / 4;
}
footer {
grid-column: 1 / span 3;
}
24
Placing Items
using Grid areas
grid-template-areas
is used to define named grid areas
26
• justify-items
defines alignment along
the row axis
• align-items
defines alignment along
the column axis
27
• justify-content
justifies all grid content
on row axis (if container has
extra space)
• align-content
justifies all grid content
on column axis (if container
has extra space)
28
• justify-self
• aligns an item inside a
single cell along the row
axis
• align-self
• aligns an item inside a
single cell along the
column axis
29
Grid vs Flexbox
• Grid allows defining a two-dimensional layout with
columns and rows, unlike flexbox which is a one-
dimensional layout (either in a column or a row).
• In practice you combine these layout models. Often you can
use a Flexbox container inside a Grid container
o Grid is often used for the overall page layout (i.e., Macro layouts
describing the larger, page-wide organization) while the flexbox is
used for small-scale one-dimensional layouts (e.g., menu or card
layout)
30
Media Queries
31
Responsive page layout using Media Queries
Use media queries to define
layouts for different screen sizes
Source: https://kinsta.com/blog/responsive-web-design/
33
Common Layout Patterns
https://web.dev/patterns/layout/
Watch explanation in this video
34
Menu using a flexbox
• A website menu could be created using a ul
element with display: flex
nav ul {
<nav> width: 90%;
<ul> display: flex;
<li><a href="#">Home</a></li> column-gap: 1rem;
<li><a href="#">About</a></li> row-gap: 0.4rem;
<li><a href="#">Contact us</a></li> flex-wrap: wrap;
</ul> }
</nav> nav ul li {
list-style: none;
}
35
Line-up card
justify-content: space-between
• Flexbox column card with justify-content:
space-between
o places the first and last child elements (e.g., title
and image) at the edges of the flex container
o the remaining space evenly distributed between
the elements
• e.g., the descriptive text in between gets placed with equal
spacing to each edge
36
Aspect ratio Image Card
aspect-ratio: <width> / <height>
.card img {
aspect-ratio: 16 / 9;
}
37
Clamping card
clamp(<min>, <actual>, <max>)
39
Pancake stack – Header-Main-Footer
grid-template-rows: auto 1fr auto
40
Sidebar & Content
grid-template-columns: minmax(<min>, <max>) 1fr
42
RAM (Repeat, Auto-fit, Minmax)
grid-template-columns: repeat(auto-fit, minmax(<base>, 1fr))
• A responsive layout with auto-created grid columns and
automatically-placed children
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
Browser!
• Use RAM (Repeat-Auto-fit-Minmax)
to create dynamic grid areas
• I want you to auto-create the grid
columns you decide how many you
can fit using the auto-placement
algorithm
• I want the columns to be minimum See posted example
280px and a maximum of sharing the
available space equality among the
columns 43
Super centered
place-items: center
• Use grid’s place-items: center to center
an element within its parent
o place-items: center is a shorthand that sets
both align-items and justify-items to center
44
Summary
• Use Grid any ,me you work with two-dimensional
layouts to divide the page into several sec,ons
having different size and posi,on
• Use Flexbox for one-dimensional layout that offers
space alloca,on between items + the ability to
alter its items’ width/height to best fill the
available space
• Use Grid layout and Media Queries (when
needed) for responsive design
• .. mastering CSS needs hands-on pracDce 🧗🏋…
45
Resources
• Responsive Design Patterns
o https://web.dev/patterns/layout/
o https://web.dev/learn/design/
• Responsive Web Design Code Camp
o https://www.freecodecamp.org/learn/responsive-web-design/
• Flexbox
o https://css-tricks.com/snippets/css/a-guide-to-flexbox/
o https://marina-ferreira.github.io/tutorials/css/flexbox/
• CSS Grid
o https://1linelayouts.glitch.me/
o https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout
o https://gridbyexample.com/learn/
o https://css-tricks.com/snippets/css/complete-guide-grid/
46