CSS - Responsive Design
Instructors: Jess, Carmen, and Eda
Responsive Design
Before this session, please:
- Read Best Practices for Responsive Design: What Is Responsive Web Design, and What Is Its Relationship to Tools Like CSS Grid and Flexbox? and take notes
- Read Best Practices for Responsive Design: How Do Media Queries Work, and What Are Some Common Media Types and Features? and take notes
- Read Best Practices for Responsive Design: What Are Media Breakpoints, and What Are Common Breakpoints in Modern Design? and take notes
- Read Best Practices for Responsive Design: What Is the Mobile First Approach in Responsive Web Design? and take notes
- Try to complete the steps from Design a Piano before class
- Read Responsive Web Design Review and take notes
- Complete Responsive Web Design Quiz solo to test your knowledge of responsive web design
- Plan and complete Build a Technical Documentation Page over the next days or weeks
During this session, we’ll:
- Review notes
- Review notes
- Review notes
- Review notes
- We’ll review core concepts and steps from Design a Piano to share the joy of making a piano
- Review notes
- We’ll break down possible approaches to Build a Technical Documentation Page to help you approach this certification project confidently
After this session, please:
- Read Working with CSS Variables: What Are CSS Custom Properties, and How Do They Work? and take notes
- Read Working with CSS Variables: What Is the @property Rule, and How Does It Work with Fallbacks? and take notes
- Try to complete as many steps from Build a City Skyline before class. This workshop is big and you may need to complete this over the next few days
- Complete Build an Availability Table solo over the next few days. Make sure you don’t just copy the example, think before using green.
- Read CSS Variables Review and take notes
- Complete CSS Variables Quiz solo to test your knowledge of CSS Variables
Instructor notes
Best Practices for Responsive Design: What Is Responsive Web Design, and What Is Its Relationship to Tools Like CSS Grid and Flexbox?
There are three main components to a responsive design:
- Fluid grids (using relative units instead of fixed units, allowing content to resize depending on screen size)
- Flexible images (making sure that images are sized within their containers and don’t overflow)
- Media queries (applying different styles specifically based on the device’s viewport width)
Some CSS features we can use for creating responsive designs are:
- CSS Flexbox and Grid. They are often used together, and offer more flexibility and control compared to older methods like floats and tables.
calc()lets us use dynamic values, allowing us to create flexible layouts.- the
srcsetattribute for images lets us serve different image files based on device capabilities, making sure that users don’t download large image files on devices with smaller screens or lower resolution.
Best Practices for Responsive Design: How Do Media Queries Work, and What Are Some Common Media Types and Features?
A media query consists of a media type and one or more media features that check for specific conditions to apply the styles.
Commonly used media types are:
all(the default value, suitable for all devices)print(for paged material and documents viewed on a screen in print preview mode)screen(primarily for screens)
(There are some old types such as handheld and tv that are now deprecated!)
Commonly used media features are:
min-width(minimum viewport width)
1@media screen and (min-width: 768px) {
2 /* Styles for screens at least 768px wide */
3}aspect-ratio(the ratio between the width and height of the viewport)
1@media screen and (aspect-ratio: 16/9) {
2 /* Styles for screens with a 16:9 aspect ratio */
3}orientation(whether the device is in landscape or portrait orientation)
1@media screen and (orientation: landscape) {
2 /* Styles for landscape orientation */
3}resolution(resolution of the output device in dots per inch (dpi) or dots per centimeter (dpcm))
1@media screen and (min-resolution: 300dpi) {
2 /* Styles for high-resolution screens */
3}hover(whether the primary input mechanism can hover over elements)
1@media (hover: hover) {
2 /* Styles for devices that support hover */
3}prefers-color-scheme(if the user has requested a light or dark color theme)
1@media (prefers-color-scheme: dark) {
2 /* Styles for dark mode */
3}Different media features can be combined and we can use not keyword to negate, and only keyword to isolate media queries:
1@media screen and (min-width: 768px) and (orientation: landscape) {
2 /* Styles for landscape screens at least 768px wide */
3}To target multiple media queries, we can separate them with commas:
1@media screen and (min-width: 768px), print {
2 /* Styles for screens at least 768px wide OR for print */
3}Within each media query, the normal rules of the CSS cascade still apply.
The “mobile-first” responsive design is creating a base style for mobile devices first, then using media queries to improve layout for larger screens. We’ll see it more in its own lesson below.
Best Practices for Responsive Design: What Are Media Breakpoints, and What Are Common Breakpoints in Modern Design?
Using media queries, we can define media breakpoints to apply different styles based on, usually, the device’s viewport width.
Commonly used values for media breakpoints are:
- Small devices (smartphones): up to 640px.
- Medium devices (tablets): 641px to 1024px.
- Large devices (desktops): 1025px and larger.
Another approach is:
- Extra small devices: up to 576px.
- Small devices: 577px to 768px.
- Medium devices: 769px to 992px.
- Large devices: 993px to 1200px.
- Extra large devices: 1201px and larger.
There’s even another one:
- Extra small device: under 576px.
- Small device: more than or equal to 576px.
- Medium device: more than or equal to 768px.
- Large device: more than or equal to 992px.
- Extra large device: more than or equal to 1200px.
- Extra extra large device: more than or equal to 1400px.
These are not strictly defined values, but they are just commonly used practices.
Best Practices for Responsive Design: What Is the Mobile First Approach in Responsive Web Design?
The mobile-first approach is about designing for mobile devices first as a base, then defining designs for larger screens. It’s different from the traditional approach where we first design for larger screen devices and then consider mobile devices.
Many users around the globe nowadays access the Internet through mobile devices, so this is a good approach to designing and developing websites.
Another concern is performance optimization. Mobile devices have less processing power and may be connected to slower networks.
Mobile-first approach makes us think about deciding the absolutely essential content for our websites. Since Google uses mobile-first indexing, this approach is also good for SEO and potentially improves the website’s visibility.
One example of a mobile-first design may look like this:
1<div class="container">
2 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam.</p>
3 <p>Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta.</p>
4 <p>Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>
5</div> 1/* Base styles for mobile */
2.container {
3 width: 100%;
4 padding: 15px;
5}
6
7/* Styles for tablets */
8@media screen and (min-width: 768px) {
9 .container {
10 width: 750px;
11 margin: 0 auto;
12 }
13}
14
15/* Styles for desktops */
16@media screen and (min-width: 1024px) {
17 .container {
18 width: 960px;
19 }
20}Design a Piano
In this fun exercise, we’ll practice what we’ve learned so far and create a responsive piano design!
Responsive Web Design Review
In this unit, we’ve explored:
- Responsive web design
- Media queries
- Common media breakpoints
- Mobile-first approach
Build a Technical Documentation Page
This exercise goes over some HTML and CSS concepts that we’ve learned so far!