Skip to content

Responsive Design with Media Queries | Lecture 26

Responsive Design with Media Queries

Let’s begin with Lecture 26 of the HTML5 & CSS3 series. Responsive Design with Media Queries โ€“ Make Your Site Look Great on All Devices. This lecture is for absolute beginners; even kids can follow along and understand.

๐ŸŽฏ Objective:

By the end of this lecture, you will:

  • Understand what responsive design is and why it’s important
  • Know how to use media queries to adjust layout based on screen size
  • Be able to create layouts that adapt to mobile phones, tablets, and desktops
  • Build a responsive version of your card layout or navigation bar
  • Make your website truly mobile-friendly

๐Ÿ“ฑ What Is Responsive Design?

Responsive design means creating websites that look good and work well on all devices, from large desktop monitors to small mobile phones.

Think of it like this:

If your website was a car, then responsive design is its suspension system โ€” it adapts smoothly to any road (screen size)!

๐Ÿ” Why Use Media Queries?

Media queries allow you to apply different CSS styles depending on:

  • Device width and height
  • Orientation (portrait or landscape)
  • Resolution (high-DPI screens like Retina displays)

Theyโ€™re the key to making your site look great no matter how someone is viewing it.

๐Ÿง  Basic Syntax of a Media Query
@media only screen and (condition) {
    /* CSS rules go here */
}

Common Conditions:

ConditionDescription
max-widthStyles apply when screen is smaller than this
min-widthStyles apply when screen is larger than this
orientation: portraitVertical phone view
orientation: landscapeHorizontal phone/tablet view
๐Ÿ“ Common Breakpoints for Responsive Design
DeviceWidth Range
Mobile PhonesUp to 767px
Tablets768px โ€“ 1023px
Desktops1024px and up

These are not strict rules โ€” they can vary based on your content.

๐Ÿ’ป Try This: Make Your Card Layout Responsive

Letโ€™s update your Flexbox card layout from Lecture 21 so it looks great on all screen sizes using media queries.

Step 1: Open flex-card-layout.html

If you havenโ€™t already created it, review Lecture 21 (Advanced Flexbox | Lecture 21ย ) to build it first.

Step 2: Add These Media Queries

Update your <style> section like this:

<style>
    body {
        margin: 0;
        font-family: Arial, sans-serif;
        padding: 40px;
        background-color: #f9f9f9;
    }

    h1 {
        text-align: center;
        color: #333;
    }

    .card-container {
        display: flex;
        flex-wrap: wrap;
        gap: 20px;
        justify-content: center;
    }

    .card {
        flex: 1 1 calc(33% - 20px);
        background-color: white;
        border: 1px solid #ddd;
        border-radius: 8px;
        box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        padding: 20px;
        box-sizing: border-box;
    }

    .card h3 {
        margin-top: 0;
    }

    /* Tablet View */
    @media (max-width: 768px) {
        .card {
            flex: 1 1 calc(50% - 20px); /* 2 cards per row */
        }
    }

    /* Mobile View */
    @media (max-width: 480px) {
        .card {
            flex: 1 1 100%; /* 1 card per row */
        }
    }
</style>

This makes your card layout:

  • Show 3 cards per row on desktop
  • Switch to 2 cards per row on tablets
  • Show 1 card per row on phones
Step 3: Run with Live Server

Right-click the code โ†’ Show All Commands โ†’ Launch Live Server

Resize your browser window and watch the layout change automatically!
๐ŸŽ‰ You’ve just built a fully responsive card layout!

๐Ÿ’ก Bonus: Make Your Flex Navbar Stack on Small Screens

Go back to flex-navbar.html And add this inside your <style> block:

/* Mobile View */
@media (max-width: 768px) {
    .navbar {
        flex-direction: column;
        align-items: flex-start;
    }

    .nav-links {
        flex-direction: column;
        width: 100%;
    }

    .nav-links li {
        width: 100%;
    }
}

Now your navbar stacks vertically on smaller screens โ€” perfect for mobile users!

Responsive Design with Media Queries
A boy typing into a computer screen showing the same webpage viewed on a phone tablet and desktop

๐Ÿงช Try It Yourself!

  1. Add a new breakpoint at max-width: 992px to make the layout switch to 2 columns earlier.
  2. Add a transition effect to your cards so they slide in smoothly when resizing.
  3. Style the mobile menu differently โ€” maybe with more padding or rounded corners.
  4. Test your layout on real devices using your phone or browser dev tools.

๐Ÿš€ Next Lecture Preview:

In Lecture 27, weโ€™ll dive into CSS Variables (Custom Properties) โ€” learn how to define and reuse colors, fonts, and other values across your entire website with ease and flexibility!

Stay Updated

If you found this information useful, donโ€™t forget to bookmark this page and Share.

HTML5 and CSS3 Compleate Series

Najeeb Alam

Najeeb Alam

Technical writer specializes in developer, Blogging and Online Journalism. I have been working in this field for the last 20 years.