What Is a Media Query in HTML CSS: Beginner Guide

Ever found yourself wanting to create responsive web designs that adapt to various screen sizes and devices? This is where media queries in HTML and CSS come into play, offering a powerful tool to make your website dynamic and flexible. But what exactly are media queries in HTML and CSS, and how do you use them? Let's dive in!

How to Use CSS Media Queries
How to Use CSS Media Queries

In essence, media queries are a feature of CSS that allow the application of different styles for different media types/devices based on predefined conditions. This enables your website to adapt its layout, colors, fonts, and other visual elements to optimize the user experience on a wide range of screens, from large desktop monitors to small smartphones.

an image of a computer screen with the text css media queries simple explanation
an image of a computer screen with the text css media queries simple explanation

Understanding Media Queries

Media queries use a specific syntax to check the characteristics of the user's media, like the screen's width and height, the resolution, the orientation, and more. If the specified condition is true, the enclosed CSS rules are applied.

Média query
Média query

The basic structure of a media query looks like this:

```css @media { /* CSS rules here */ } ```

Media Types

an image of a computer screen with some type of programming program on it's side
an image of a computer screen with some type of programming program on it's side

Before we delve into the various media features, let's briefly explore some common media types. Although modern web design usually focuses on 'screen', it's essential to be aware of other media types for accessibility and print styling:

  • screen: Computer and mobile screen displays.
  • print: Document fragment printer and PDF documents.
  • speech: Screen readers.

Media Features

HTML Media Elements Explained – Images, Maps, Video & Audio in One Example
HTML Media Elements Explained – Images, Maps, Video & Audio in One Example

Now, let's look at some of the media features that you can use in your media queries:

  • width and height: The viewport's dimensions.
  • device-width and device-height: The device's physical dimensions.
  • orientation: Portrait or landscape orientation.
  • aspect-ratio: Aspect ratio of the viewport (e.g., 4/3, 16/9).

You can use these features individually or combine them with logical operators (and, not, only) to create complex conditions.

CSS Media Queries for All Devices And Browsers | IE7, IE8
CSS Media Queries for All Devices And Browsers | IE7, IE8

Using Media Queries in Your CSS

Now that you understand media queries' basics, it's time to put them into practice. Here's an example of a simple responsive layout that changes the layout direction at certain breakpoints:

a blue poster with the words css media queeny on it's side
a blue poster with the words css media queeny on it's side
CSS Responsive Layout | CSS Tutorial | CSS media query
CSS Responsive Layout | CSS Tutorial | CSS media query
Media query css for tablet | css media query
Media query css for tablet | css media query
How To Use CSS3 Media Queries To Create a Mobile Version of Your Website — Smashing Magazine
How To Use CSS3 Media Queries To Create a Mobile Version of Your Website — Smashing Magazine
Responsive design with media query : screen size?
Responsive design with media query : screen size?
Media query css for mobile | css media query
Media query css for mobile | css media query
Online tool takes the guesswork out of CSS media queries
Online tool takes the guesswork out of CSS media queries
the different types of electronic devices are shown in this graphic style, with text below them
the different types of electronic devices are shown in this graphic style, with text below them
How to use Media Queries in JavaScript with matchMedia — SitePoint
How to use Media Queries in JavaScript with matchMedia — SitePoint
How to Streamline Your CSS by Combining Duplicate Media Queries
How to Streamline Your CSS by Combining Duplicate Media Queries
a poster with the words css media queen and an image of a computer screen
a poster with the words css media queen and an image of a computer screen
the different types of web pages are shown
the different types of web pages are shown
a blue background with the words css media query and an image of a computer
a blue background with the words css media query and an image of a computer
Media Queries ¿Qué son?
Media Queries ¿Qué son?
the screenshoter is showing how to use an app
the screenshoter is showing how to use an app
CSS Notes for Beginners (Easy + Quick Guide) 🎨
CSS Notes for Beginners (Easy + Quick Guide) 🎨
an image of a black background with colorful text
an image of a black background with colorful text
Add CSS In HTML
Add CSS In HTML
What is CSS ?
What is CSS ?
css tips and tricks
css tips and tricks

```css body { display: flex; flex-direction: row; } @media (max-width: 600px) { body { flex-direction: column-reverse; } } ```

In this example, the website displays its content in a row (horizontal) on screens wider than 600px. However, when the viewport's width is 600px or less, the content will stack vertically.

Using Min-resolution and Max-resolution

individus who care about the presentation of their website on different devices can also use the resolution media feature to apply styles based on pixel density. For instance, you can provide different images for high-DPI displays, ensuring crystal-clear quality on Retina devices:

```css img.high-res { /* for high-DPI displays */ src: url('high-res-image.jpg'); } @media (min-resolution: 192dpi), (min-resolution: 2dppx) { img.high-res { src: url('normal-res-image.jpg'); } } ```

The dpi and dppx units allow you to target specific pixel densities. In this example, a minimum resolution of 192dpi or 2dppx triggers the media query, ensuring that lower-resolution devices display the normal image.

Media queries are indispensable for creating responsive and engaging web designs. As screen sizes and technologies continue to evolve, mastering media queries will help you stay ahead of the curve, delivering tailored experiences to users, regardless of their device. So, start experimenting, stay curious, and happy coding!

Related Articles

3d Graphics Html What Is Global Attributes In Html W3schools Html Javascript Link Html Css Javascript Editor Online Html Css Javascript Interview Questions W3schools W3school Geolocation Api W3schools Html Css Grid W3schools Html Try It Editor W3schools Html Text Formatting W3schools Grid Template Area