CSS Media Queries

 CSS media queries are a key component of responsive web design, allowing you to apply different styles based on various characteristics of the device or viewport, such as screen width, height, orientation, and resolution. They enable you to create layouts that adapt to different devices, ensuring a consistent and user-friendly experience across desktops, tablets, and smartphones.

Here's a basic overview of CSS media queries:

Syntax:

Media queries start with the @media rule followed by one or more media features enclosed in parentheses. Inside the curly braces, you define the styles to apply when the specified conditions are met. Here's the general syntax:

@media media-type and (media-feature) { /* Styles to apply */ }

Example:

/* Styles for screens with a maximum width of 768 pixels */
@media screen and (max-width: 768px) {
    body {
        font-size: 14px;
    }
}

Common Media Features:

  • Width and Height: max-width, min-width, max-height, min-height.
  • Orientation: orientation: portrait, orientation: landscape.
  • Device Aspect Ratio: aspect-ratio.
  • Device Pixel Ratio: min-resolution, max-resolution.
  • Viewport Width and Height: max-width, min-width, max-height, min-height.
  • Hover Capability: hover: hover, hover: none.
  • Color Scheme: prefers-color-scheme: dark, prefers-color-scheme: light.
  • Light Level: light-level.

Combining Media Features:

You can combine multiple media features using logical operators like and, not, and only.

/* Styles for devices with a width between 768px and 1024px in landscape orientation */ @media only screen and (min-width: 768px) and (max-width: 1024px) and (orientation: landscape) { /* Styles to apply */ }

Using Media Queries in Practice:

  1. Mobile-First Approach: Start by designing your website for mobile devices and then use media queries to add styles for larger screens.

  2. Breakpoints: Identify key breakpoints where your design needs to adapt. Common breakpoints are often based on popular device sizes, such as 768px for tablets and 1024px for desktops.

  3. Testing: Test your website across different devices and screen sizes to ensure that it looks and functions as intended.

By leveraging CSS media queries effectively, you can create responsive designs that provide an optimal viewing experience for users on a wide range of devices and screen sizes.

Let's say we have a webpage with a header, main content, and a sidebar. We want the layout to adjust based on the screen size.

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Responsive Layout</title> <link rel="stylesheet" href="styles.css"> </head> <body> <header> <h1>Responsive Layout Example</h1> </header> <div class="container"> <main> <article> <h2>Main Content</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed gravida massa vel sapien convallis, ac tristique ligula efficitur.</p> </article> </main> <aside> <h2>Sidebar</h2> <ul> <li>Link 1</li> <li>Link 2</li> <li>Link 3</li> </ul> </aside> </div> <footer> <p>&copy; 2024 Responsive Layout Example</p> </footer> </body> </html>


Now, let's define the CSS styles in styles.css to make this layout responsive:
/* Default styles */ header, main, aside, footer { padding: 20px; } .container { display: flex; flex-wrap: wrap; } main { flex: 2; /* Take up 2/3 of the available space */ } aside { flex: 1; /* Take up 1/3 of the available space */ } /* Media query for tablets and larger */ @media screen and (min-width: 768px) { aside { order: -1; /* Move the sidebar to the top */ } } /* Media query for desktops and larger */ @media screen and (min-width: 1024px) { header, footer { text-align: center; } .container { flex-direction: row; } aside { width: 25%; /* Set a fixed width for the sidebar */ } main { width: 75%; /* Set a fixed width for the main content */ } }

In this example:

  • The layout uses flexbox to arrange the main content and sidebar side by side.
  • On tablets and larger screens (min-width: 768px), the sidebar is moved above the main content by changing its order.
  • On desktops and larger screens (min-width: 1024px), the header and footer are centered, and the sidebar and main content have fixed widths.

By using media queries, we've created a layout that adjusts dynamically based on the screen size, providing an optimal viewing experience for users on various devices.


Post a Comment

0 Comments