Useful CSS Media Query Features
Tags: webdev, css
Published: 2019-02-12 20:15:38

---
title: Useful CSS Media Query Features
published: true
description: List of useful CSS media query features
cover_image: https://thepracticaldev.s3.amazonaws.com/i/oznre6eb9nubs2evjor9.png
tags: webdev, css
---

A typical media query consists of a **media type** (screen, print, speech, etc) and one or more expressions, involving **media features**, which resolve to either true or false. 
The result of the query is true if the media type specified in the media query matches the type of device the document is being displayed on and all expressions in the media query are true. The corresponding styles are applied, when the media query result is true.

*Note:* If no media type is specified the default is `all` type, i.e the corresponding styles will be applicable for all devices.

## Media features

Media features describe specific characteristics of the user agent and the device the document is being displayed on. These are optional.

Here are a few useful media features that I've come across-


### 1. Aspect Ratio

Aspect ratio is the width to height ratio of the viewport. 

```scss
/* Exact aspect ratio */
@media (aspect-ratio: 2/1) {
    ...
}

/* Minimum aspect ratio */
@media (min-aspect-ratio: 16/9) {
 ...
}

/* Maximum aspect ratio */
@media (max-aspect-ratio: 8/5) {
    ...
}
```

Here's a practical example using aspect ratio -

```scss
img {
    display: block;
    margin: auto;
    width: 100vw;
    height: calc((9/16)*100vw);
}

@media (min-aspect-ratio: 16/9) {
    img {
        height: 100vh;
        width: calc((16/9)*100vh);
    }
}
```

[Check out the working demo](https://jsfiddle.net/ananyaneogi/x70wy25b/4/). Increase/decrease the height of the window to see it in effect.

--------------

### 2. Orientation

There are two orientation values-

 - `portrait`: The viewport is in a portrait orientation, i.e., the height is greater than or equal to the width.

 - `landscape`: The viewport is in a landscape orientation, i.e., the width is greater than the height.

```scss

@media (orientation: landscape) {
    /* landscape styles */
}

@media (orientation: portrait) {
    /* portrait styles */
}
```

This media feature helps in responsive design. We can use it with other media features such as width and height.

```scss
@media screen and (min-height: 640px) and (orientation: portrait) { 
    ...
}

```

---------------

### 3. `display-mode`

The display-mode is used to test the display mode of an application. There are 4 types according to the [spec.](https://w3c.github.io/manifest/#the-display-mode-media-feature) It is a part of Web App Manifest specification.
The feature query will apply irrespective of whether a web app manifest is present or not.

Check out this article for a practical usage of this- [Making Fullscreen Experiences](https://developers.google.com/web/fundamentals/native-hardware/fullscreen/)


-------------------

### 4. `hover`, `any-hover` and `any-pointer`

`hover`: The hover CSS media feature can be used to test whether the user's primary input mechanism can hover over elements.

I've found it useful to use this feature to avoid cases in which touch devices emulate *hover* states on long tap of *hoverable* elements.

```scss
@media (hover: hover) {
  a:hover {
    background: blue;
  }
}
```

`any-hover`: This checks whether any available input can hover over elements.

`any-pointer`: This checks whether the user has any pointing device (mouse, stylus), if so then how accurate it is.
Accuracy is measured with - `fine`, `coarse`, `none`


A really great article on this topic -
[Touch Devices Should Not Be Judged By Their Size](https://css-tricks.com/touch-devices-not-judged-size/)

-------------------

### 5. Width and Height

These two are very commonly used media features and are an essential part of responsive web.

```scss
/* Exact width */
@media (width: 360px) {
  body {
    ...
  }
}

/* Maximum width */
@media (max-width: 768px) { 
  body {
   ...
}

/* Minimum width */
@media (min-width: 992px) { 
  body {
    ...
  }
}

/* Exact height */
@media (height: 360px) {
  body {
    ...
  }
}

/* Minimum height */
@media (min-height: 640px) {
  body {
    ...
  }
}

/* Maximum height */
@media (max-height: 768px) {
  body {
    ...
  }
}

/* All the values can also be in rem, em, vw, vh */

```

----------------


### 6. `prefers-color-scheme`

This has been introduced in media queries level 5. It will be useful when it is fully implemented!
This is used to detect if the user has requested their system to use a light or dark color theme. Previously I wrote about [enabling dark mode on websites using surrounding light](https://dev.to/ananyaneogi/enabling-dark-mode-on-websites-based-on-surrounding-light--3jel), this can be an add on to that if the ambient light API is not available.

Check out other features introduced in [media queries level 5.](https://drafts.csswg.org/mediaqueries-5/)

*Edit:* As pointed out [here](https://dev.to/equinusocio/comment/8lkb), according to [caniuse](https://caniuse.com/#search=prefers-color-scheme), it is available in the technology preview version of safari.

-------------------

These are few of the useful media features that I've found. There are others as well such as `resolution`, `update`, etc. Full list can be found on [MDN.](https://developer.mozilla.org/en-US/docs/Web/CSS/@media)


If you've come across any other lesser known media features and it's use cases, let me know!