A Guide to Responsive Design

Learn responsive design principles, media queries, and practical techniques for building pages that work across devices.

🎯 Introduction

People browse the web on phones, tablets, and desktop computers with many different screen sizes. Responsive design helps pages adapt to these screens while providing a good user experience. Flexible layouts and media queries keep pages usable and visually consistent across devices.

This article introduces responsive design, its core CSS media query techniques, and common implementation patterns and settings to help you build pages for multiple devices.


📱 What is Responsive Design?

Responsive design is an approach that adjusts a page's layout, font sizes, images, and other content to suit the device's screen size, resolution, and orientation. A single page serves multiple devices rather than requiring separate versions.

Its main benefits include:

BenefitDescription
Support for multiple devicesPhones, tablets, and desktops use the same URL with a consistent experience
Better user experienceContent stays readable without horizontal scrolling
Easier maintenanceOne codebase reduces development and maintenance costs

🧩 What are CSS Media Queries?

CSS media queries are a core responsive design technique. They apply different style rules based on characteristics such as viewport width, height, resolution, or orientation, allowing the layout and appearance to adjust dynamically.

Think of a media query as a condition in CSS: its styles apply only when that condition matches.


🧾 Basic Media Query Syntax

A media query has the following basic structure:

@media media-type and (condition) {
    /* Style rules */
}
  • @media introduces the media query.
  • The media type is often screen for screen-based devices.
  • Conditions commonly test width, height, or resolution.
  • Use and to combine conditions.

For example, apply styles when the viewport is at most 600px wide:

@media screen and (max-width: 600px) {
    body {
        background-color: lightblue;
    }
}

The page background becomes light blue when the viewport width is 600px or less.


🎯 Common Media Query Conditions

Common conditions include:

ConditionDescriptionExample
max-widthMaximum viewport width(max-width: 768px)
min-widthMinimum viewport width(min-width: 1024px)
orientationViewport orientation: portrait or landscape(orientation: portrait)
resolutionDisplay resolution(min-resolution: 2dppx)

Combine conditions for more precise targeting:

@media screen and (min-width: 600px) and (max-width: 1024px) {
    /* Target viewport widths from 600px to 1024px */
}

🧱 Common Responsive Design Techniques

Alongside media queries, responsive design uses these techniques and approaches:

TechniqueDescription
FlexboxUses flexible box layout to arrange content flexibly
Percentage widthsReplaces fixed widths with percentages for fluid sizing
Flexible imagesUses max-width: 100% to scale images with their container
Viewport configurationUses <meta name="viewport"> to control mobile viewport scaling
Showing and hiding contentSelectively displays or hides elements based on available space

📐 Typical Phone, Tablet, and Desktop Settings

Responsive styles often target three broad device categories:

Device categoryWidth range (px)Common adjustments
Phones320 - 600Wrapping content, slightly larger text, larger buttons
Tablets601 - 1024Switching multiple columns to one, collapsing menus
Desktops1025 and aboveFixed-width content areas, multiple columns

Example media queries:

/* Phone styles */
@media screen and (max-width: 600px) {
    body {
        font-size: 16px;
    }
}

/* Tablet styles */
@media screen and (min-width: 601px) and (max-width: 1024px) {
    body {
        font-size: 14px;
    }
}

/* Desktop styles */
@media screen and (min-width: 1025px) {
    body {
        font-size: 12px;
    }
}

❓ Knowledge Check

Q1: How does responsive design differ from adaptive design?

Responsive design adjusts layouts dynamically with CSS media queries. Adaptive design typically provides several fixed-width layouts for different devices. Responsive design is more flexible and easier to maintain.

Q2: Which media query conditions are most common?

max-width and min-width are commonly used to change layouts based on viewport width.

Q3: How do you make images resize across devices?

Use max-width: 100%; height: auto; so images do not exceed their container's width and their height adjusts proportionally.


🧪 Exercises

Exercise 1: Build a Simple Responsive Page

Create a page with a yellow background below 600px and a white background at widths of 600px or more.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Responsive Background Color</title>
        <style>
            body {
                background-color: white;
                transition: background-color 0.3s ease;
            }
            @media screen and (max-width: 599px) {
                body {
                    background-color: yellow;
                }
            }
        </style>
    </head>
    <body>
        <h1>Resize the browser to see the background change</h1>
    </body>
</html>

Copy the code into your editor and preview the result!


Exercise 2: Build a Responsive Flexbox Menu

Create a simple navigation menu using Flexbox. Stack the links vertically on phones and arrange them horizontally on tablets and larger screens.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Responsive Navigation Menu</title>
        <style>
            nav {
                display: flex;
                background-color: #333;
                padding: 10px;
            }
            nav a {
                color: white;
                text-decoration: none;
                padding: 10px 15px;
                flex-grow: 1;
                text-align: center;
            }
            /* Stack links on phones */
            @media screen and (max-width: 600px) {
                nav {
                    flex-direction: column;
                }
                nav a {
                    border-bottom: 1px solid #555;
                }
                nav a:last-child {
                    border-bottom: none;
                }
            }
            /* Arrange links horizontally on tablets and larger screens */
            @media screen and (min-width: 601px) {
                nav {
                    flex-direction: row;
                }
                nav a {
                    border-bottom: none;
                }
            }
        </style>
    </head>
    <body>
        <nav>
            <a href="#">Home</a>
            <a href="#">Products</a>
            <a href="#">About Us</a>
            <a href="#">Contact Us</a>
        </nav>
    </body>
</html>

Copy the code into your editor and preview the result!


🎉 You have learned responsive design principles, media query syntax, and practical techniques! You are ready to start building modern pages that work across devices.