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
688
votes
22 answers

Media Queries: How to target desktop, tablet, and mobile?

I have been doing some research on media queries and I still don't quite understand how to target devices of certain sizes. I want to be able to target desktop, tablet and mobile. I know that there will be some discrepancies but it would be nice to…
betamax
  • 13,431
  • 9
  • 38
  • 55
600
votes
6 answers

What is the difference between "screen" and "only screen" in media queries?

What is the difference between screen and only screen in media queries? Why…
Jitendra Vyas
  • 148,487
  • 229
  • 573
  • 852
590
votes
4 answers

CSS media queries: max-width OR max-height

When writing a CSS media query, is there any way you can specify multiple conditions with "OR" logic? I'm attempting to do something like this: /* This doesn't work */ @media screen and (max-width: 995px OR max-height: 700px) { ... }
Fraser
  • 14,036
  • 22
  • 73
  • 118
480
votes
12 answers

Can media queries resize based on a div element instead of the screen?

I would like to use media queries to resize elements based on the size of a div element they are in. I cannot use the screen size as the div is just used like a widget within the webpage, and its size can vary.
yazz.com
  • 57,320
  • 66
  • 234
  • 385
407
votes
14 answers

Is it possible to put CSS @media rules inline?

I need to dynamically load banner images into a HTML5 app and would like a couple of different versions to suit the screen widths. I can't correctly determine the phone's screen width, so the only way I can think of doing this is to add background…
Dancer
  • 17,035
  • 38
  • 129
  • 206
390
votes
17 answers

Twitter Bootstrap 3: how to use media queries?

I'm using Bootstrap 3 to build a responsive layout where I want to adjust a few font sizes according to the screen size. How can I use media queries to make this kind of logic?
Rui
  • 5,900
  • 10
  • 38
  • 56
324
votes
10 answers

CSS native variables not working in media queries

I am trying to use CSS variables in media query and it does not work. :root { --mobile-breakpoint: 642px; } @media (max-width: var(--mobile-breakpoint)) { }
user7122183
301
votes
6 answers

@Media min-width & max-width

I have this @media setup: HTML: CSS: @media screen and (min-width: 769px) { /* STYLES HERE */ } @media screen and (min-device-width: 481px) and…
rallybilen
  • 3,077
  • 3
  • 15
  • 7
287
votes
7 answers

How to detect if the OS is in dark mode in browsers?

Similar to "How to detect if OS X is in dark mode?" only for browsers. Has anyone found if there is a way to detect if the user's system is in the new OS X Dark Mode in Safari/Chrome/Firefox? We would like to change our site's design to be dark-mode…
funkju
  • 3,463
  • 2
  • 15
  • 19
258
votes
3 answers

@media media query and ASP.NET MVC razor syntax clash

I've got a large site that runs in ASP.NET MVC using the Razor view engine. I have a base stylesheet which contains all of the generic styling for the whole site. On occasion, however, I have page specific styles which in the of the page -…
Sniffer
  • 6,242
  • 10
  • 45
  • 53
253
votes
8 answers

What is the difference between max-device-width and max-width for mobile web?

I need to develop some html pages for iphone/android phones, but what is the difference between max-device-width and max-width? I need to use different css for different screen size. @media all and (max-device-width: 400px) @media all and…
virsir
  • 15,159
  • 25
  • 75
  • 109
249
votes
23 answers

Why are my CSS3 media queries not working on mobile devices?

In the styles.css, I am using media queries, both of which use a variation of: /*--[ Normal CSS styles ]----------------------------------*/ @media only screen and (max-width: 767px) { /*--[ Mobile styles go…
redconservatory
  • 21,438
  • 40
  • 120
  • 189
248
votes
9 answers

What does @media screen and (max-width: 1024px) mean in CSS?

I found this piece of code in a CSS file I inherited, but I can't make any sense out of it: @media screen and (max-width: 1024px){ img.bg { left: 50%; margin-left: -512px; } } Specifically, what is happening on the first line?
Yarin
  • 173,523
  • 149
  • 402
  • 512
236
votes
23 answers

CSS @media print issues with background-color;

I'm attempting to make a printable stylesheet for our app but I'm having issues with background-color in @media print. @media print { #header{display:none;} #adwrapper{display:none;} td { border-bottom: solid; …
Weston Watson
  • 5,344
  • 6
  • 24
  • 25
207
votes
18 answers

$(window).width() not the same as media query

I am using Twitter Bootstrap on a project. As well as the default bootstrap styles I have also added some of my own //My styles @media (max-width: 767px) { //CSS here } I am also using jQuery to change the order of certain elements on the page…
Pattle
  • 5,983
  • 8
  • 33
  • 56
1
2 3
99 100