What Media queries work in CSS?
Published: 2022-09-24 10:06:30

---
title: What Media queries work in CSS?
published: true
date: 2022-09-15 04:30:00 UTC
tags: 
canonical_url: https://jainil.dev/what-media-queries-work-in-css/
---



Making sure your content looks good on all screen sizes is known as responsive design. Layouts, fonts, and images on the website should all automatically adjust to the user’s device.

A matched set of conditions regarding the user’s device, browser, or system settings can change the appearance (and even behaviour) of a website or app.


<ul>
<li><a href="#the-top-12-most-stunning-linux-distributions">The Top 12 Most Stunning Linux Distributions</a></li>
<li><a href="#1-zorin-os">1. Zorin OS</a></li>
<li><a href="#2-deepin">2. Deepin</a></li>
<li><a href="#3-cutefishos">3. CutefishOS</a></li>
<li><a href="#4-garuda-linux">4. Garuda Linux</a></li>
<li><a href="#5-feren-os">5. Feren OS</a></li>
<li><a href="#6-popos">6. Pop!_OS</a></li>
<li><a href="#7-manjaro-linux">7. Manjaro Linux</a></li>
<li><a href="#8-kde-neon">8. KDE Neon</a></li>
<li><a href="#9-elementary-os">9. elementary OS</a></li>
<li><a href="#10-nitrux-os">10. Nitrux OS</a></li>
<li><a href="#11-linux-mint">11. Linux Mint</a></li>
<li><a href="#12-solus-budgie">12. Solus Budgie</a></li>
<li><a href="#a-conclusion">A Conclusion</a></li>
</ul>
</div></nav>

![How do media queries work in CSS?](https://www.csssolid.com/images/css-media-query.png)

You can use the CSS Media Query to only apply CSS when the browser and device environment comply with a set of rules. To create different layouts depending on the size of the viewport, media queries are an essential component of responsive web design.


## Understanding the syntax

![How do media queries work in CSS?](https://www.litmus.com/wp-content/uploads/2020/04/definingmobile-mediaquery.jpg)

![How do media queries work in CSS?](https://www.fixrunner.com/wp-content/uploads/2021/02/image1.png)

```
@media media-type and (media-feature-rule) {
  /* CSS rules go here */
}

```

- @media identifies the type of media being used to access a website, the features it supports, and the operators that can be used to combine both simple and complex conditions.
- Tells the browser what kind of media this code is for using Media Type (e.g. print, or screen).
- Operators are supported by media queries, just like they are in many programming languages, allowing us to match media types based on specific criteria.

and

```
/* Matches screen between 320px AND 768px */
@media screen (min-width: 320px) and (max-width: 768px) {
  .element {
    /* Styles! */
  }
}

```

_or (or comma-separated)_

```
/* 
  Matches screens where either the user prefers dark mode or the screen is at least 1200px wide */
@media screen (prefers-color-scheme: dark), (min-width 1200px) {
  .element {
    /* Styles! */
  }
}

```

_not_

```
@media print and ( not(color) ) {
  body {
    background-color: none;
  }
}

```

A media expression is a rule or test that must be satisfied for the CSS it contains to be applied.

## Website in real-time

![How do media queries work in CSS?](https://www.tutorialstonight.com/assets/css/media-query-css-for-all-devices.webp)

See the Pen [Untitled](https://codepen.io/jainil__1209/pen/XWqNOjx) by Jainil Prajapati ([@jainil\_\_1209](https://codepen.io/jainil__1209)) on [CodePen](https://codepen.io).

<script async="" src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>
## Breakpoints for Devices

CSS breakpoints are the points added to the code, and depending on the screen size of the device, the website content reacts to these points. This aids in providing the user with the ideal layout. Because CSS breakpoints are used in conjunction with media queries, they are also referred to as CSS media query breakpoints.

![How do media queries work in CSS?](https://pbs.twimg.com/media/E8hOdAqWUAIGI-g.jpg:large)

![How do media queries work in CSS?](https://miro.medium.com/max/1200/1*YWyrRcr62YrTVdqdd0Mmjw.jpeg)

[Read – How to use CSS breakpoints to create responsive designs](https://getflywheel.com/layout/css-breakpoints-responsive-design-how-to/#based)

## Min-Max width

We can apply CSS if the viewport is above or below a certain width — or an exact width — using the min-width, max-width, and width media features. Viewport width is the feature we tend to detect most frequently to create responsive designs (and that is supported by a wide range of browsers).

![How do media queries work in CSS?](https://miro.medium.com/max/1248/0*2EClIG6PgSlWrUlx.)

```
@media only screen and (min-width: 600px) 
 {...}

```

The real meaning of this statement is “Do {} if [device width] is greater than or equal to 600px.”

Therefore, if the email is opened on an iPhone 5S with a 320px screen width, neither the media query nor the styles in {…} will be activated.

![How do media queries work in CSS?](https://miro.medium.com/max/1248/0*a3izme62hcRrpjq7.)

```
@media only screen and (max-width: 600px)  
{...}

```

The real meaning of this statement is “Do {…} if [device width] is less than or equal to 600px.”

The media query will therefore be triggered and all of the styles in {…} will be applied if the email is opened on an iPhone 5S with a 320px screen width.

## Essential media features

**width and height**

These two frequently utilised media features are a requirement for responsive web.

```
/* 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 */

```

**Orientation** Two orientation values exist:

When the height is greater than or equal to the width, the viewport is said to be in portrait orientation.

viewport orientation: The viewport is oriented in a landscape mode, meaning that the width is greater than the height.

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

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

```

The responsive design benefit from this media feature. It can be combined with other media attributes like width and height.

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

```

**display-mode**

applies styles based on an application’s display mode. Fullscreen, standalone, minimal-UI, and browser are the available modes.

```
@media all and (display-mode: fullscreen) {
  body {
    margin: 0;
    border: 5px solid black;
  }
}

```

## Finishing up

Now that you know what media queries are, why they are helpful, and how to use them, you can use them to improve the user experience for visitors using mobile devices. Additionally, CSS media queries are not the only way to make your website responsive. There are other ways as well, like the aforementioned flexbox and grid, to make your design flexible.

Thank you for reading all the way through; I hope you learned at least a few things. Coding on!