Questions tagged [media-queries]

Media queries enable the conditional application of CSS styles based on media types, such as screen and print, and the conditions of media features, such as viewport and device height and width. They are part of the W3C CSS3 specification.

CSS Media Queries allow you to limit the scope of CSS rules based on, for example, screen size, device orientation, or display density. Introduced in the CSS3 specification, the use of media queries has become the de facto standard behind building websites that utilize .

Every media query usually consists of a media type and can optionally contain one or more expressions which resolve to either true or false. If the media type matches the device the page is being viewed on and all expressions resolve to true, the corresponding style sheet or style rules are applied to the document.

<!-- Media Query on a link element -->
<link rel="stylesheet" href="style.css" media="(max-width: 750px)" />

<!-- Media Query within a stylesheet -->
<style>
@media (max-width: 750px) {
  footer {
    display: none;
  }
}
</style>

NOTE: Stylesheets that have media queries attached to their <link> elements will still download even if their media queries would return false.

Media Query Types

There are a handful of different media types, but the most commonly used are all, screen, and print. Including a media type is not always required based on whether or not your expression, or your own conditions, require it.

Media Query Expressions

Media query expressions can be created using the logical operators and, not, and only. Additionally, comma separated expressions constitutes the "or" operator.

  • The and operator is used to combine multiple media features into a single query. Each feature must return true for the entire query to be evaluate as true.

  • The min-width means greater than or equal to. Similarly max-width means less than or equal to.

     @media (min-width: 650px) and (orientation: landscape) { ... }
    
  • The not operator must be used with an explicit media type and is used to reverse the logic of an entire media query.

     @media not all and (max-width: 650px) { ... }
    
  • The only operator must be used with an explicit media type and tells older browsers not to process the query.

     @media only print { ... }
    
  • Comma separated expressions evaluates each expression with an "or" operator between them.

     @media (max-width: 600px), (min-width: 800px) { ... }
    

Additional Information on CSS Media Queries

7959 questions
203
votes
7 answers

How to detect the device orientation using CSS media queries?

In JavaScript the orientation mode can be detected using: if (window.innerHeight > window.innerWidth) { portrait = true; } else { portrait = false; } However, is there a way to detect the orientation using CSS only? Eg. something…
Francesco
  • 24,839
  • 29
  • 105
  • 152
197
votes
4 answers

CSS media query to target only iOS devices

Is there a @media query to target only devices running iOS? For example: @media (min-device-width:320px) and (max-device-width:768px) { #nav { yada: yada; } } Would this also alter the behavior of the page on Android devices with…
Kevin G
  • 2,325
  • 3
  • 16
  • 30
195
votes
4 answers

Media Queries - In between two widths

I'm trying to use CSS3 media queries to make a class that only appears when the width is greater than 400px and less than 900px. I know this is probably extremely simple and I am missing something obvious, but I can't figure it out. What I have…
russellsayshi
  • 2,449
  • 5
  • 17
  • 21
184
votes
13 answers

Common CSS Media Queries Break Points

I am working on a Responsive Web Site with CSS Media Queries. Is the following a good organization for devices? Phone, Ipad (Landscape & Portrait), Desktop and Laptop, Large Screen What are the common media queries break-point values? I am planning…
Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
181
votes
11 answers

Bootstrap 3 breakpoints and media queries

On the Bootstrap 3 media queries documentation it says: We use the following media queries in our Less files to create the key breakpoints in our grid system. Extra small devices (phones, less than 768px): No media query since this is the default…
178
votes
11 answers

IE8 support for CSS Media Query

Does IE8 not support the following CSS media query: @import url("desktop.css") screen and (min-width: 768px); If not, what is the alternate way of writing? The same works fine in Firefox. Any issues with the code below? @import url("desktop.css")…
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
167
votes
8 answers

Using Sass Variables with CSS3 Media Queries

I'm trying to combine the use of a Sass variable with @media queries as follows: $base_width:1160px; @media screen and (max-width: 1170px) {$base_width: 960px;} @media screen and (min-width: 1171px) {$base_width: 1160px;} $base_width is then…
Jeremy S.
  • 4,599
  • 3
  • 18
  • 25
162
votes
14 answers

Media query to detect if device is touchscreen

What is the safest way, using media queries, to make something happen when not on a touchscreen device? If there is no way, do you suggest using a JavaScript solution such as !window.Touch or Modernizr?
JJJollyjim
  • 5,837
  • 19
  • 56
  • 78
159
votes
11 answers

Get the device width in javascript

Is there a way to get the users device width, as opposed to viewport width, using javascript? CSS media queries offer this, as I can say @media screen and (max-width:640px) { /* ... */ } and @media screen and (max-device-width:960px) { /*…
Nicholas Evans
  • 2,194
  • 2
  • 16
  • 18
132
votes
11 answers

iPhone 5 CSS media query

The iPhone 5 has a longer screen and it's not catching my website's mobile view. What are the new responsive design queries for the iPhone 5 and can I combine with existing iPhone queries? My current media query is this: @media only screen and…
Maverick
  • 3,039
  • 6
  • 26
  • 35
123
votes
7 answers

Chrome Device Mode Emulation Media Queries Not Working

For some reason device emulation mode is not reading my media queries. It works on other sites including my own sites that I made with bootstrap, but it's not working on media queries I am using from scratch (clicking the media queries button turns…
Sam Scott
  • 1,256
  • 2
  • 9
  • 7
120
votes
6 answers

Extending selectors from within media queries with Sass

I have an item class and a compact "modifier" class: .item { ... } .item.compact { /* styles to make .item smaller */ } This is fine. However, I'd like to add a @media query that forces the .item class to be compact when the screen is small…
soundly_typed
  • 39,257
  • 5
  • 28
  • 36
119
votes
5 answers

How can I emulate prefers-color-scheme media query in Chrome?

Chrome 76 has added support for prefers-color-scheme media query (a.k.a. "dark mode"). But how can I test my webpage in both color schemes easily, without toggling the system dark mode on and off? Here is the same question for Firefox, but I can't…
xmcp
  • 3,347
  • 2
  • 23
  • 36
113
votes
13 answers

Bootstrap - Removing padding or margin when screen size is smaller

EDITED: Maybe I should ask which selector sets up the side padding when the screen is reduced to below 480px width? I have been browsing bootstrap-responsiveness.css for a while to locate this but nothing seems to affect this. Original I…
Seong Lee
  • 10,314
  • 25
  • 68
  • 106
112
votes
4 answers

What are the rules for CSS media query overlap?

How do we space out media queries accurately to avoid overlap? For example, if we consider the code: @media (max-width: 20em) { /* for narrow viewport */ } @media (min-width: 20em) and (max-width: 45em) { /* slightly wider viewport…
Baumr
  • 6,124
  • 14
  • 37
  • 63