Course Content
Introduction to HTML (Structure of the Web)
0/1
Introduction to CSS (Styling the Web)
Getting Started with CSS
0/2
Introduction to JavaScript (Interactivity on the Web)
Project Presentation and Review
0/1
Protected: Web Development Bootcamp: Master HTML, CSS, and JavaScript in 3 Weeks
About Lesson

Responsive Design with Media Queries

Responsive design is an approach to web design that ensures your website looks good and functions well on devices of all sizes, from mobile phones to desktop monitors. Media queries are a key part of responsive web design, allowing you to apply different styles based on the characteristics of the device (such as screen width, height, or orientation).


1. Writing Media Queries for Mobile, Tablet, and Desktop Screens

Media queries are used in CSS to apply styles only if certain conditions are met. For example, you can target devices based on their screen width, height, or orientation. Media queries typically work by defining a range of screen widths for mobile, tablet, and desktop devices.

Basic Syntax of a Media Query

The basic syntax for writing a media query in CSS looks like this:

@media (condition) {
  /* Styles for the condition */
}

Where (condition) specifies the device’s properties, such as width or height.

Example Media Queries for Different Devices

1. Mobile Devices (Small screens)

For mobile devices, we target screens with a maximum width of 600px. This usually applies to smartphones in portrait orientation.

@media (max-width: 600px) {
  /* Styles for mobile devices */
  body {
    font-size: 14px;
  }
}
2. Tablets (Medium screens)

For tablets, we target screens with a width between 601px and 1024px. This applies to most tablets in portrait or landscape orientation.

@media (min-width: 601px) and (max-width: 1024px) {
  /* Styles for tablets */
  body {
    font-size: 16px;
  }
}
3. Desktops (Large screens)

For desktops, we target screens with a width of at least 1025px. This usually applies to laptops and desktop monitors.

@media (min-width: 1025px) {
  /* Styles for desktops */
  body {
    font-size: 18px;
  }
}

You can also combine multiple media queries to target devices with specific conditions, such as landscape orientation or high-resolution screens.


2. Best Practices for Responsive Design

a. Mobile-First Design

Mobile-first design means designing for the smallest screens first, and then progressively enhancing the design for larger screens. This approach makes it easier to ensure your site works well on mobile devices before adapting it to larger screens.

Example:

/* Mobile-first styles */
body {
  font-size: 14px;
}

/* Tablet styles */
@media (min-width: 601px) {
  body {
    font-size: 16px;
  }
}

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

b. Use Relative Units (em, rem, %)

Rather than using fixed units like px, it’s best to use relative units such as em, rem, or % for font sizes, margins, and padding. This ensures that the design is more flexible and can scale depending on the device or user preferences (e.g., for users who need larger text).

Example:

body {
  font-size: 1rem; /* 16px */
}

h1 {
  font-size: 2em; /* 2 times the font size of the body */
}

c. Avoid Fixed Widths

Avoid using fixed widths for elements, as they can cause layout issues on smaller screens. Instead, use flexible units like percentages or fr for grids, and allow elements to scale according to the screen size.

Example:

.container {
  width: 100%; /* Full width of the parent container */
}

.item {
  width: 45%; /* Flexible width */
  margin: 2.5%;
}

d. Use Fluid Layouts and Flexbox/Grid

Utilize fluid layouts and layout systems like Flexbox and CSS Grid to create designs that adjust automatically to different screen sizes. These methods allow for more flexible and responsive layouts without having to rely on media queries for every adjustment.

Example using Flexbox:

.container {
  display: flex;
  flex-wrap: wrap;
}

.item {
  flex: 1 1 45%; /* Flex item takes up 45% of the container's width */
  margin: 10px;
}

e. Hide Unnecessary Content on Smaller Screens

For smaller devices, it may be helpful to hide certain content or navigation elements that might clutter the screen or are not needed for a better user experience. You can use media queries to hide these elements.

Example:

@media (max-width: 600px) {
  .sidebar {
    display: none; /* Hide sidebar on mobile */
  }
}

f. Test on Real Devices

It’s important to test your website on real devices in addition to using browser developer tools. Real devices may behave slightly differently, and you may need to tweak your media queries for optimal performance across various screen sizes.


3. Example: Full Responsive Layout

Let’s look at a full example that uses media queries to create a responsive layout for a webpage.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Responsive Web Design</title>
  <style>
    /* Basic mobile-first styles */
    body {
      font-family: Arial, sans-serif;
      font-size: 14px;
      margin: 0;
      padding: 0;
    }

    header {
      background-color: #4CAF50;
      padding: 10px;
      text-align: center;
      color: white;
    }

    .container {
      display: flex;
      flex-direction: column;
      padding: 20px;
    }

    .content, .sidebar {
      padding: 10px;
    }

    .content {
      flex: 1;
    }

    .sidebar {
      display: none; /* Hidden on mobile */
    }

    /* Tablet styles */
    @media (min-width: 601px) and (max-width: 1024px) {
      .sidebar {
        display: block; /* Sidebar shown on tablet */
        width: 30%;
        float: right;
      }
    }

    /* Desktop styles */
    @media (min-width: 1025px) {
      .container {
        flex-direction: row;
      }

      .content {
        width: 70%;
      }

      .sidebar {
        width: 30%;
      }
    }
  </style>
</head>
<body>
  <header>
    <h1>Responsive Web Design</h1>
  </header>

  <div class="container">
    <div class="content">
      <h2>Main Content</h2>
      <p>This is the main content area. Resize the browser to see the layout change!</p>
    </div>
    <div class="sidebar">
      <h3>Sidebar</h3>
      <p>This is the sidebar. It will appear on tablet and desktop views.</p>
    </div>
  </div>
</body>
</html>

In this example:

  • Mobile-first styles are applied by default.
  • Tablet styles show the sidebar by floating it to the right on screens larger than 600px but smaller than 1024px.
  • Desktop styles arrange the content and sidebar side by side using a row layout.

4. Conclusion

Media queries are essential for creating responsive web designs that adapt to various screen sizes. By writing appropriate media queries for different devices (mobile, tablet, and desktop), using best practices like mobile-first design, and utilizing fluid layouts and Flexbox/Grid, you can ensure your website looks great and functions properly on all devices.