Web Development /CSS — Styling Modern Websites / Lesson
Web Development

Responsive Web Design and Media Queries

Learn how to build websites that adapt smoothly to different screen sizes using responsive layouts, flexible units, mobile-first design, media queries, responsive images, and modern CSS techniques.

🎯

Learning Objectives

["Understand the concept and importance of responsive web design.","Understand the viewport and how browsers render websites on different devices.","Apply mobile-first design principles when building modern websites.","Use flexible CSS units such as percentages, rem, em, vw, vh, and fr.","Use media queries to create layouts for different screen sizes.","Create responsive navigation bars, cards, grids, sidebars, and content sections.","Use responsive typography with modern CSS functions such as clamp().","Make images and other media responsive.","Choose breakpoints based on content rather than specific device names.","Test responsive websites using browser developer tools.","Identify common responsive design mistakes and avoid them.","Apply responsive design principles to a practical web project."]

📖

Lesson Content

Introduction to Responsive Web Design

Modern websites are accessed from many different types of devices. A visitor may use a smartphone, tablet, laptop, desktop computer, television, or another device with a different screen size.

If a website is designed only for one screen size, its content may become difficult to read or use on another device. Responsive Web Design solves this problem by allowing a website layout to adapt to the available screen space.

Responsive design is not simply about making a website smaller. It is about creating a flexible interface that remains usable, readable, accessible, and visually organized across different screen sizes.

What Is Responsive Web Design?

Responsive Web Design is an approach to website development where the layout and components of a website automatically adapt to different viewport sizes and device conditions.

A responsive website can change:

  • The number of columns in a layout.
  • The size of headings and text.
  • The spacing between elements.
  • The position of navigation elements.
  • The width of images and videos.
  • The arrangement of cards.
  • The visibility or placement of sidebars.
  • The amount of content displayed at different sizes.

The goal is to make the website comfortable to use regardless of the available screen width.

The Viewport

The viewport is the visible area of a web page inside the browser window.

On a desktop computer, the viewport may be very wide. On a smartphone, it is much narrower.

HTML pages commonly include the following viewport declaration:

<meta name="viewport" content="width=device-width, initial-scale=1.0">

This tells mobile browsers to use the device’s actual viewport width when rendering the page.

Why the Viewport Meta Tag Matters

Without an appropriate viewport configuration, mobile browsers may render a page as though it were designed for a much wider desktop screen and then scale it down.

For responsive websites, this declaration should normally be included inside the <head> element.

Fixed Layouts vs Flexible Layouts

A fixed layout often relies heavily on fixed pixel dimensions.

.container {
    width: 1200px;
}

This can create horizontal scrolling when the viewport is smaller than 1200 pixels.

A flexible layout can instead use relative units:

.container {
    width: 90%;
    max-width: 1200px;
    margin-inline: auto;
}

This allows the container to shrink on smaller screens while preventing it from becoming unnecessarily wide on large screens.

Useful Responsive Units

Responsive layouts commonly use a mixture of fixed and relative units.

Percentage (%)

Percentages are relative to a relevant containing dimension.

.content {
    width: 80%;
}

rem

The rem unit is relative to the root element’s font size. It is useful for scalable typography and spacing.

h1 {
    font-size: 2rem;
}

em

The em unit is relative to the font size of the current element or its inherited context, depending on the property.

vw and vh

vw represents a percentage of the viewport width, while vh represents a percentage of the viewport height.

.hero {
    min-height: 70vh;
}

fr

The fr unit is especially useful with CSS Grid. It represents a fraction of the available grid space.

.grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
}

Fluid Layouts

A fluid layout expands and contracts according to the available space.

For example:

.wrapper {
    width: min(90%, 1200px);
    margin-inline: auto;
}

This creates a layout that uses 90 percent of the available width while preventing the content from becoming wider than 1200 pixels.

Mobile-First Design

Mobile-first design means designing the base version of the interface for smaller screens and then progressively enhancing it for larger screens.

For example:

.course-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
}

@media (min-width: 768px) {
    .course-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 1100px) {
    .course-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}

The base layout contains one column. As more space becomes available, the layout expands into multiple columns.

Media Queries

Media queries allow CSS to apply different styles when certain conditions are true.

A common example is a minimum viewport width:

@media (min-width: 768px) {
    .sidebar {
        display: block;
    }
}

The styles inside the media query apply when the viewport is at least 768 pixels wide.

Maximum-Width Media Queries

You can also apply styles when the viewport is below a particular width.

@media (max-width: 767px) {
    .navigation {
        display: none;
    }
}

However, when using a mobile-first strategy, min-width media queries are often easier to maintain because the base styles represent the smallest layout.

Choosing Breakpoints

A breakpoint is the point at which the layout changes because the available space requires a different arrangement.

Do not select breakpoints only because a particular phone or tablet has a certain screen size. Instead, resize your layout and identify where the content begins to look crowded or difficult to use.

Common breakpoints may include values such as:

  • 480px for small screens.
  • 768px for medium layouts.
  • 1024px for larger tablets and smaller desktops.
  • 1200px and above for wide desktop layouts.

These are examples, not strict rules. Your content should determine the actual breakpoints.

Responsive Typography

Text should remain readable on both small and large screens.

Modern CSS provides the clamp() function for creating fluid values with minimum and maximum limits.

h1 {
    font-size: clamp(2rem, 5vw, 4rem);
}

This means the heading can scale with the viewport while remaining within a defined minimum and maximum size.

Responsive Images

Images should not overflow their containers.

img {
    max-width: 100%;
    height: auto;
    display: block;
}

The max-width: 100% rule allows an image to shrink when its container becomes smaller.

For more advanced responsive images, HTML provides features such as srcset and the <picture> element.

Responsive Cards

A common pattern in modern websites is a card grid.

On a mobile screen, cards may appear in one column. On a tablet, they may appear in two columns, and on a desktop, they may appear in three or four columns.

.cards {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
}

@media (min-width: 700px) {
    .cards {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 1050px) {
    .cards {
        grid-template-columns: repeat(3, 1fr);
    }
}

Responsive Navigation

Navigation is one of the most important responsive components.

A desktop navigation bar may contain several links in one row, while a mobile layout may use a menu button or vertically stacked navigation links.

CSS can control layout changes, while JavaScript can be used when the mobile menu needs interactive open and close behavior.

Responsive Sidebars

A dashboard or learning platform may use a sidebar on desktop and move that sidebar below the main content on smaller screens.

.layout {
    display: grid;
    grid-template-columns: 1fr;
}

@media (min-width: 900px) {
    .layout {
        grid-template-columns: 260px 1fr;
    }
}

This approach is useful for LMS dashboards, admin panels, blogs, and business websites.

Responsive Containers

Modern websites often use a maximum content width so that long lines of text do not become difficult to read on very wide screens.

.container {
    width: min(92%, 1200px);
    margin-inline: auto;
}

This simple pattern is highly useful when creating professional layouts.

CSS Grid and Responsive Design

CSS Grid makes responsive layouts easier because columns can be created dynamically.

For example:

.course-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 1.5rem;
}

The auto-fit and minmax() combination allows the browser to automatically determine how many columns can fit while keeping each card above the minimum width.

CSS Flexbox and Responsive Design

Flexbox is also useful for responsive interfaces.

.actions {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
}

The flex-wrap property allows items to move onto another line when there is not enough horizontal space.

Responsive Spacing

Spacing can also be fluid.

.section {
    padding-block: clamp(2rem, 6vw, 5rem);
}

This allows the section spacing to adapt to the viewport while remaining within sensible limits.

Responsive Tables

Large tables can become difficult to display on mobile screens.

One approach is to allow horizontal scrolling around the table:

.table-wrapper {
    overflow-x: auto;
}

table {
    min-width: 700px;
}

This prevents the entire page from becoming horizontally scrollable while still allowing the table to be viewed.

Responsive Videos

Videos and embedded content should also adapt to their containers.

.video-wrapper {
    width: 100%;
    aspect-ratio: 16 / 9;
}

.video-wrapper iframe {
    width: 100%;
    height: 100%;
    border: 0;
}

The aspect-ratio property helps maintain the desired shape while the width changes.

Container Queries

Modern CSS also supports container queries. Unlike viewport media queries, container queries allow a component to respond to the size of its own container.

.course-area {
    container-type: inline-size;
}

@container (min-width: 600px) {
    .course-card {
        display: grid;
        grid-template-columns: 160px 1fr;
    }
}

This can be useful for reusable components that may appear inside containers of different sizes.

Accessibility and Responsive Design

Responsive design should not focus only on appearance. A responsive website must remain usable and accessible.

  • Keep text readable.
  • Provide enough space around interactive controls.
  • Do not rely only on hover interactions.
  • Maintain logical reading order.
  • Ensure keyboard users can access interactive elements.
  • Avoid unnecessary horizontal scrolling.
  • Do not hide important information simply because the screen is small.

Testing Responsive Websites

Modern browsers provide developer tools that allow developers to simulate different screen sizes.

When testing, check:

  • Small smartphone widths.
  • Large smartphone widths.
  • Tablet layouts.
  • Laptop layouts.
  • Large desktop screens.
  • Landscape orientation.
  • Long and short content.
  • Different text sizes.

Common Responsive Design Mistakes

1. Using Fixed Widths Everywhere

Large fixed widths can create horizontal scrolling on small screens.

2. Using Too Many Breakpoints

Creating a media query for every device can make CSS difficult to maintain. Use breakpoints when the content requires them.

3. Ignoring Images

Large images can overflow containers or increase page loading times.

4. Making Text Too Small

Responsive design should preserve readability rather than simply fitting more information onto the screen.

5. Designing Only for Desktop

A desktop-first layout may become difficult to simplify for smaller screens. A mobile-first strategy often produces cleaner responsive CSS.

6. Testing Only One Device

A layout can work correctly at one width and fail at another. Always test across a range of viewport sizes.

Practical Project: Responsive Dwallnet Academy Layout

For this lesson, create a responsive training platform layout containing:

  • A navigation bar.
  • A hero section.
  • A course grid.
  • A sidebar or learning information area.
  • Responsive buttons.
  • Responsive images.
  • Responsive typography.
  • Mobile, tablet, and desktop layouts.

The objective is to make the interface comfortable to use on a small smartphone while still taking advantage of larger screens.

Complete Responsive Example

The following example demonstrates several responsive techniques together.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta
        name="viewport"
        content="width=device-width, initial-scale=1.0"
    >

    <title>Dwallnet Academy</title>

    <style>
        * {
            box-sizing: border-box;
        }

        body {
            margin: 0;
            font-family: Arial, sans-serif;
            line-height: 1.6;
            background: #f5f7fb;
            color: #172033;
        }

        img {
            max-width: 100%;
            height: auto;
            display: block;
        }

        .container {
            width: min(92%, 1200px);
            margin-inline: auto;
        }

        .site-header {
            background: #0b3b82;
            color: #ffffff;
        }

        .nav {
            min-height: 70px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 1rem;
        }

        .logo {
            font-size: 1.4rem;
            font-weight: 800;
        }

        .nav-links {
            display: none;
            gap: 1rem;
        }

        .nav-links a {
            color: #ffffff;
            text-decoration: none;
        }

        .hero {
            padding-block: clamp(3rem, 8vw, 7rem);
        }

        .hero h1 {
            font-size: clamp(2rem, 6vw, 4.5rem);
            line-height: 1.05;
            margin-bottom: 1rem;
        }

        .hero p {
            max-width: 650px;
        }

        .button {
            display: inline-block;
            margin-top: 1rem;
            padding: 0.8rem 1.2rem;
            border-radius: 8px;
            background: #ffffff;
            color: #0b3b82;
            text-decoration: none;
            font-weight: 700;
        }

        .layout {
            display: grid;
            grid-template-columns: 1fr;
            gap: 2rem;
            padding-block: 3rem;
        }

        .course-grid {
            display: grid;
            grid-template-columns: repeat(
                auto-fit,
                minmax(230px, 1fr)
            );
            gap: 1.5rem;
        }

        .card {
            background: #ffffff;
            padding: 1.5rem;
            border-radius: 14px;
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.06);
        }

        .sidebar {
            background: #ffffff;
            padding: 1.5rem;
            border-radius: 14px;
        }

        @media (min-width: 768px) {
            .nav-links {
                display: flex;
            }

            .layout {
                grid-template-columns: 250px 1fr;
                align-items: start;
            }
        }

        @media (min-width: 1100px) {
            .layout {
                grid-template-columns: 280px 1fr;
            }
        }
    </style>
</head>

<body>

    <header class="site-header">
        <div class="container nav">
            <div class="logo">Dwallnet Academy</div>

            <nav class="nav-links">
                <a href="#">Home</a>
                <a href="#">Courses</a>
                <a href="#">About</a>
                <a href="#">Contact</a>
            </nav>
        </div>
    </header>

    <section class="hero">
        <div class="container">
            <h1>Learn Technology. Build Your Future.</h1>

            <p>
                Learn practical technology skills through
                structured courses and hands-on projects.
            </p>

            <a href="#" class="button">
                Explore Courses
            </a>
        </div>
    </section>

    <main class="container layout">

        <aside class="sidebar">
            <h2>Learning Dashboard</h2>
            <p>Track your courses and learning progress.</p>
        </aside>

        <section>
            <h2>Popular Courses</h2>

            <div class="course-grid">

                <article class="card">
                    <h3>Web Development</h3>
                    <p>
                        Learn HTML, CSS, JavaScript and
                        modern web development.
                    </p>
                </article>

                <article class="card">
                    <h3>Data Analysis</h3>
                    <p>
                        Learn how to transform raw data
                        into useful information.
                    </p>
                </article>

                <article class="card">
                    <h3>Cyber Security</h3>
                    <p>
                        Understand fundamental concepts
                        of protecting digital systems.
                    </p>
                </article>

                <article class="card">
                    <h3>WordPress Development</h3>
                    <p>
                        Build and manage professional
                        WordPress websites.
                    </p>
                </article>

            </div>
        </section>

    </main>

</body>
</html>

Lesson Summary

Responsive Web Design allows websites to adapt to different screen sizes and devices. It combines flexible layouts, relative units, responsive images, typography, Flexbox, Grid, and media queries to create interfaces that remain usable across different environments.

A strong responsive workflow starts with a flexible base layout, commonly using a mobile-first strategy, and progressively enhances the interface when additional space becomes available.

Responsive design is not about targeting every individual device. It is about creating a layout that responds intelligently to the space available to it.

Practice Exercise

Create a responsive Dwallnet Technologies landing page containing:

  1. A responsive header.
  2. A hero section with a heading, paragraph, and button.
  3. A responsive three-to-four-column course grid.
  4. A sidebar that moves below the content on smaller screens.
  5. Responsive images.
  6. Fluid heading sizes using clamp().
  7. At least two media queries.
  8. A responsive navigation system.
  9. A responsive footer.

Test the page at different viewport widths and correct every horizontal overflow or layout problem you discover.

💻

Code Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Dwallnet Responsive Academy</title>

    <style>
        * {
            box-sizing: border-box;
        }

        body {
            margin: 0;
            font-family: Arial, sans-serif;
            background: #f4f7fb;
            color: #172033;
        }

        img {
            max-width: 100%;
            height: auto;
            display: block;
        }

        .container {
            width: min(92%, 1200px);
            margin-inline: auto;
        }

        .header {
            background: #0b3b82;
            color: #fff;
        }

        .nav {
            min-height: 70px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 1rem;
        }

        .logo {
            font-weight: 800;
            font-size: 1.3rem;
        }

        .nav-links {
            display: none;
            gap: 1rem;
        }

        .nav-links a {
            color: #fff;
            text-decoration: none;
        }

        .hero {
            padding-block: clamp(3rem, 8vw, 7rem);
        }

        .hero h1 {
            font-size: clamp(2rem, 6vw, 4.5rem);
            line-height: 1.05;
            max-width: 800px;
        }

        .hero p {
            max-width: 650px;
        }

        .layout {
            display: grid;
            grid-template-columns: 1fr;
            gap: 2rem;
            padding-block: 3rem;
        }

        .courses {
            display: grid;
            grid-template-columns:
                repeat(auto-fit, minmax(230px, 1fr));
            gap: 1.5rem;
        }

        .card {
            background: #fff;
            padding: 1.5rem;
            border-radius: 14px;
        }

        .sidebar {
            background: #fff;
            padding: 1.5rem;
            border-radius: 14px;
        }

        @media (min-width: 768px) {
            .nav-links {
                display: flex;
            }

            .layout {
                grid-template-columns: 260px 1fr;
            }
        }

        @media (min-width: 1100px) {
            .layout {
                grid-template-columns: 300px 1fr;
            }
        }
    </style>
</head>

<body>

<header class="header">
    <div class="container nav">
        <div class="logo">Dwallnet Academy</div>

        <nav class="nav-links">
            <a href="#">Home</a>
            <a href="#">Courses</a>
            <a href="#">About</a>
            <a href="#">Contact</a>
        </nav>
    </div>
</header>

<section class="hero">
    <div class="container">
        <h1>
            Build Skills That Prepare You For The Digital Future
        </h1>

        <p>
            Learn practical technology skills through
            structured lessons and real-world projects.
        </p>
    </div>
</section>

<main class="container layout">

    <aside class="sidebar">
        <h2>Learning Dashboard</h2>
        <p>Track your learning progress.</p>
    </aside>

    <section>
        <h2>Featured Courses</h2>

        <div class="courses">

            <article class="card">
                <h3>Web Development</h3>
                <p>
                    Learn how to build modern websites.
                </p>
            </article>

            <article class="card">
                <h3>Data Analysis</h3>
                <p>
                    Learn how to work with and understand data.
                </p>
            </article>

            <article class="card">
                <h3>Cyber Security</h3>
                <p>
                    Learn fundamental digital security concepts.
                </p>
            </article>

            <article class="card">
                <h3>WordPress Development</h3>
                <p>
                    Build and manage professional websites.
                </p>
            </article>

        </div>
    </section>

</main>

</body>
</html>
⭐

Key Takeaways

["Responsive Web Design allows a website to adapt to different screen sizes.","The viewport meta tag is important for mobile-friendly rendering.","Flexible units such as %, rem, em, vw, vh, fr, and clamp() help create adaptable layouts.","Mobile-first design starts with smaller screens and progressively enhances larger layouts.","Media queries allow CSS to respond to viewport conditions.","Breakpoints should be chosen according to content needs rather than specific device names.","Flexbox and CSS Grid are powerful tools for responsive layouts.","Images should normally use max-width: 100% and height: auto.","Responsive typography can use clamp() to create controlled fluid scaling.","Responsive websites should be tested at multiple viewport sizes.","Accessibility and usability should remain priorities at every screen size.","Modern container queries can allow individual components to respond to their own container size."]

Enjoying this lesson?

Sign in to your Dwallnet Academy student account to track your learning progress and mark lessons as complete.

Sign In to Track Progress