CSS Media Queries: A Beginner's Guide to Responsive Design

2024-07-27

Media queries are a powerful feature in CSS that allows you to apply styles conditionally based on various device characteristics, such as screen size, orientation, resolution, and even device type (print, screen reader, etc.). This enables you to create responsive web designs that adapt their layout and presentation to different viewing environments, enhancing user experience across a broad range of devices.

"screen" vs. "only screen"

  • "screen" is the core media type that specifies that the styles within the media query are intended for display on screens, as opposed to other media types like print, speech, or braille. It's the essential part of the query that targets screen-based devices.
  • "only screen" combines the "screen" media type with the "only" keyword. Historically, the "only" keyword served a specific purpose in older browsers that didn't fully understand media queries. It helped ensure that the styles within the query were only applied for screen media and not misinterpreted for other media types.

Modern Browser Behavior

Modern browsers (those with good support for media queries) now disregard the "only" keyword. They interpret @media screen and @media only screen identically because they correctly parse media features within media queries.

When to Use Which

  • In modern development, you can typically omit the "only" keyword for brevity and clarity. @media screen is sufficient to target screen devices.
  • If you're supporting very old browsers (highly unlikely in most cases these days), you might include "only screen" for backwards compatibility, but it's generally not necessary.

Here's a code example demonstrating both approaches:

/* Modern approach (preferred) */
@media screen and (max-width: 768px) {
  /* Styles for screens less than or equal to 768px wide */
}

/* Backwards compatibility (optional) */
@media only screen and (max-width: 768px) {
  /* Styles for screens less than or equal to 768px wide */
}



@media screen and (max-width: 768px) {
  /* Styles for screens less than or equal to 768px wide */
  body {
    font-size: 16px;
  }
  .image {
    width: 100%; /* Make images responsive on small screens */
  }
}

Targeting Tablets in Landscape (Including "only screen" for backwards compatibility, optional):

@media only screen and (min-width: 768px) and (max-width: 992px) and (orientation: landscape) {
  /* Styles for screens between 768px and 992px wide in landscape */
  .content {
    flex-direction: row; /* Adjust layout for tablets in landscape */
  }
}

High-Resolution Screens:

@media screen and (resolution: 2x) {
  /* Styles for high-resolution screens (retina displays) */
  .logo {
    background-image: url("high-res-logo.png"); /* Use higher-resolution logo */
  }
}



  • This technique involves checking for specific features of a browser to determine its capabilities. For example:
if (window.matchMedia('(orientation: portrait)').matches) {
  // Code to run on portrait screens
}
  • Limitation: Feature detection doesn't guarantee complete device compatibility as features might not be universally implemented. It's generally less reliable than media queries.

User Agent Detection (Not Recommended):

  • This approach analyzes the user agent string, a header sent by the browser that identifies itself. However:
    • User agent strings can be spoofed or modified.
    • They can be complex and change frequently, making maintenance difficult.
  • Recommendation: Avoid user agent detection for responsive design as it's unreliable and not future-proof. Media queries are the preferred method.

Server-Side Rendering (For Specific Use Cases):

  • In some scenarios, a server might detect the device type and send a different version of the website optimized for that device. This can be useful for very specific situations, but:
    • It adds complexity to server-side code.
    • It's not ideal for dynamic content that needs to adapt on the client-side (the user's device).

css media-queries



Example Codes for Customizing Numbering in HTML Ordered Lists

In HTML, ordered lists are created using the <ol> tag.Each item within the list is defined using the <li> tag.By default...


Understanding HTML, CSS, and XHTML for 100% Min-Height Layouts

HTML (HyperText Markup Language) is the building block of web pages. It defines the structure and content of a webpage using elements like headings...


Tables for Data, DIVs for Design: The Right Tools for the Job in HTML and CSS

Tables (HTML): These are meant for presenting data in a tabular format, like rows and columns. They have elements like <tr> (table row), <td> (table cell), etc...


Optimize Your Webpages: Tools for Unused Resources

Browser Developer Tools: Most modern browsers like Chrome and Firefox have built-in developer tools. These tools allow you to inspect the website's code and identify potential issues...


Conquering Div Alignment: Your Guide to Horizontal Placement in CSS

Two or more divs side-by-side: This is the most common scenario. You want your divs to display horizontally next to each other...



css media queries

Fixing Width Collapse in Percentage-Width Child Elements with Absolutely Positioned Parents in Internet Explorer 7

In IE7, when you set a child element's width as a percentage (%) within an absolutely positioned parent that doesn't have an explicitly defined width


Unveiling Website Fonts: Techniques for Developers and Designers

The most reliable method is using your browser's developer tools. Here's a general process (specific keys might differ slightly):


Interactive Backgrounds with JavaScript: A Guide to Changing Colors on the Fly

Provides the structure and content of a web page.You create elements like <div>, <p>, etc. , to define different sections of your page


Cross-Browser Rounded Corners Made Easy: Mastering the border-radius Property in CSS

In CSS (Cascading Style Sheets), the border-radius property allows you to add a curved effect to the corners of an element's outer border


Enhancing Textarea Usability: The Art of Auto-sizing

We'll create a container element, typically a <div>, to hold the actual <textarea> element and another hidden <div>. This hidden element will be used to mirror the content of the textarea