• Discover
  • Collections
  • Board
  • Create
  • Profile
  • Settings
Paths

Lesson 3.2: Responsive Design with Media Queries

Lesson 3.2: Responsive Design with Media Queries

2 min read

Introduction

In today's multi-device world, websites must look good and function well on everything from small smartphones to large desktop monitors. Responsive Web Design (RWD) is an approach to web design that makes web pages render well on a variety of devices and window or screen sizes. The cornerstone of RWD in CSS is the Media Query.

Key Concepts

The Viewport Meta Tag

Before using media queries, ensure your HTML includes the viewport meta tag in the <head>:

html
<meta name="viewport" content="width=device-width, initial-scale=1.0"> This tells the browser to set the viewport width to the device's width and set the initial zoom level. ### Media Queries A media query consists of a media type (e.g., `screen`, `print`) and zero or more media feature expressions (e.g., `min-width`, `max-width`). They allow you to apply styles only when certain conditions are met. **Syntax:** css @media media-type and (media-feature-expression) { /* CSS rules to apply */ } **Common Media Features for RWD:** - **`min-width`**: Applies styles when the viewport is *at least* this width or wider. (Mobile-first approach often uses `min-width`). - **`max-width`**: Applies styles when the viewport is *at most* this width or narrower. - `orientation`: Checks if the viewport is in `portrait` or `landscape` mode. ### Breakpoints **Breakpoints** are the points at which a website's layout or design changes to adapt to different screen sizes. Common breakpoints might be around 576px (small phones), 768px (tablets), 992px (desktops), and 1200px (large desktops). ## Quick Example Let's change a column layout to a stacked layout on smaller screens: ```html <div class="responsive-container"> <div class="col">Column 1</div> <div class="col">Column 2</div> <div class="col">Column 3</div> </div> css .responsive-container { display: flex; /* Default to horizontal columns */ gap: 15px; padding: 20px; } .col { flex: 1; background-color: #e6f7ff; border: 1px solid #91d5ff; padding: 20px; text-align: center; } /* Media query for screens smaller than 768px */ @media screen and (max-width: 767px) { .responsive-container { flex-direction: column; /* Stack columns vertically */ } } ## Summary Responsive design is vital for modern web development, ensuring a consistent user experience across diverse devices. Media queries, along with the `viewport` meta tag, are the primary tools in CSS for implementing responsiveness. By strategically defining breakpoints and adapting styles using `min-width` and `max-width`, you can create flexible and adaptable layouts.
End of lesson
👏Well done!
Previous Lesson
Lesson 3.1: Typography and Colors
Next Lesson
Quiz: Module 3: Styling and Responsiveness

Course Content

0% Complete0/11 Lessons

Lesson 1.1: Introduction to CSS

Lesson 1.2: CSS Selectors and Properties

Lesson 1.3: The CSS Box Model

Quiz

Lesson 3.1: Typography and Colors

Lesson 3.2: Responsive Design with Media Queries

Quiz

Course Content

0% Complete0/11 Lessons

Lesson 1.1: Introduction to CSS

Lesson 1.2: CSS Selectors and Properties

Lesson 1.3: The CSS Box Model

Quiz

Lesson 3.1: Typography and Colors

Lesson 3.2: Responsive Design with Media Queries

Quiz