What Are Media Queries?

What Are Media Queries
Source: Seobility

Date First Published: 14th February 2024

Topic: Web Design & Development

Subtopic: Web Design

Article Type: Computer Terms & Definitions

Difficulty: Medium

Difficulty Level: 7/10

Learn about what media queries are in this article.

Media queries are a CSS functionality that allows different style rules to be defined based on the width and height of the viewport, orientation of the viewport, and screen resolution. This allows webpage content to be adapted to different screen sizes and resolutions and is a key part of responsive web design and mobile optimisation. Media queries have been a standard for responsive web design recommended by the W3C since June 2012.

CSS Media Features

Below are some commonly used media features.

Value Description
Orientation Orientation of the viewport. Landscape or portrait
Max-height Maximum height of the viewport
Min-height Minimum height of the viewport
Height Height of the viewport (including scrollbar)
Max-width Maximum width of the viewport
Min-width Minimum width of the viewport
Width Width of the viewport (including scrollbar)

How Do Media Queries Work?

Media queries act as filters for different screen sizes and are a common way to provide an adapted style sheet for desktops, laptops, tablets, and mobile phones. Like all rules within CSS, the ones that appear further down the list override the ones above them. All media information is retrieved using media queries and the display is automatically customised based on the properties of the device it is running on without requiring any user interaction.

Media queries consist of an optional media type and media features. They are introduced with "@media" and assign a CSS block to a page only if the following conditions are met. This means that appropriate resolutions and screen sizes do not have to be specified for each individual device.

Usually, the desktop styles are defined first, the styles for tablet users are defined next, and the smartphone styles are defined last. Sometimes, styles may be defined in the opposite order, which is considered mobile-first development.

Example Of A Media Query

Below is an example of a simple media query. It changes the background colour to yellow if the viewport is 600 pixels wide or wider. If the viewport is less than 600 pixels, the background colour will be purple. This could happen if the window is shrunk on a desktop computer or when using a mobile device, such as a smartphone to view the webpage.

<!DOCTYPE html> <html> <head> <style> body { background-color: purple; } @media screen and (min-width: 600px) { body { background-color: yellow; } } </style> </head> <body> <h1>Resize the browser window to see the effect.</h1> </body> </html>


Feedback

  • Is there anything that you disagree with on this page?
  • Are there any spelling, grammatical, or punctuation errors on this page?
  • Are there any broken links or design errors on this page?

If so, it is important that you tell me as soon as possible on this page.


Comments