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
71
votes
7 answers

Achieving min-width with viewport meta tag

I would like my webpage's viewport width to equal device-width as long as device-width > 450px, or 450px otherwise (my layout dynamically scales, but doesn't look good below 450px wide). The following two meta tags work well on tablets, where the…
Tom G
  • 2,025
  • 3
  • 21
  • 32
68
votes
14 answers

Is it possible to emulate orientation in a browser?

As the title is it possible to emulate orientation in google chrome or firefox? Meaning somehow change the browser to support media query (orientation = (landscape or portrait)) I have a emulator for mobile, but I would like to have the developer…
TryingToImprove
  • 7,047
  • 4
  • 30
  • 39
66
votes
3 answers

Switching to Em-Based Media Queries

Now that the WebKit page-zoom bug has been fixed, what are the main reasons for using em-based media queries rather than pixel-based media queries? Bounty I'm adding a bounty to my question because so many prominent CSS frameworks and web developers…
cantera
  • 24,479
  • 25
  • 95
  • 138
65
votes
1 answer

Nesting @media rules in CSS

Support seems to be different across browsers.. Check the link Firefox: Black with white text. Opera, Chrome, IE9: Blue with black text. Which is correct and how would I make it consistent? The code @media screen and (min-width: 480px) { …
James South
  • 10,147
  • 4
  • 59
  • 115
61
votes
7 answers

Media query for devices supporting hover

I'd like to provide separate behaviour for browsers supporting hover (e.g. desktop browsers) and ones which don't (e.g. touchscreen devices). Specifically I want to declare a hover state on browsers that support it, but not for browsers that don't,…
moogal
  • 1,599
  • 2
  • 11
  • 12
60
votes
5 answers

calc() not working within media queries

@media screen and (max-width: calc(2000px-1px)) { .col { width: 200px; } } The value after subtraction should be 1999px, however it does not seem to be working. If I manually change it to 1999px it works fine, so I know it's not a problem with…
AndSmith
  • 741
  • 1
  • 6
  • 12
59
votes
3 answers

Firefox: How to test prefers-color-scheme?

In Firefox 67, one can use media queries to detect user preference on light or dark theme. In my version of Firefox (under Ubuntu), it seems that my preference is light theme. That is, the following CSS gives a blue background: @media…
Per Alexandersson
  • 2,443
  • 1
  • 22
  • 28
59
votes
3 answers

CSS media queries for screen sizes

I am currently trying to design a layout which will be compatible for multiple screen sizes. The screen sizes I am designing for are listed below: Screen Sizes: 640x480 800x600 1024x768 1280x1024 (and larger) The thing that I'm having trouble…
JaPerk14
  • 1,664
  • 3
  • 24
  • 32
57
votes
12 answers

CSS: max-width for @media query not working

(It works on other browsers but not chrome) I want to apply a style only when the browser size is less than 1400px with max-width not working @media only screen and (max-width:1400px) { .heading-left { left: -0.5%; } } with min-width its…
Divya Barsode
  • 778
  • 1
  • 5
  • 15
56
votes
6 answers

Twitter Bootstrap Responsive layout does not work in IE8 or lower

I developed a site using twitter bootstrap and it seems that the responsive layout part is broken in all IE browsers from IE8 and below. Is this just not supported for these browsers?
medium
  • 4,136
  • 16
  • 55
  • 66
55
votes
4 answers

@media query not working in mobile. Works fine in Chrome

I am trying to get this working but somehow its not working in mobile. when i use chrome tool to overrides the screen size, it works fine. i am not sure what i am doing wrong. please help @media screen and (min-device-width : 320px) and…
patel.milanb
  • 5,822
  • 15
  • 56
  • 92
54
votes
13 answers

Fixed background image with ios7

I have a project that I am using the fixed background image. It works great on everything except ios7. On the ipad the background image is zoomed in and blurry. Here is the CSS code I am using - .header { display: table; height: 100%; width:…
user2560895
  • 1,481
  • 3
  • 13
  • 11
54
votes
3 answers

How to code CSS media queries targeting ALL mobile devices and tablets?

@media only screen and (max-device-width : 640px) { /* Styles */ } @media only screen and (max-device-width: 768px) { /* Styles */ } This is what I have so far. The PSD mockup for the mobile site I'm working on, is 640px wide. The other one, the…
user1666487
  • 573
  • 1
  • 6
  • 6
53
votes
8 answers

CSS media queries and jQuery window .width() do not match

For a responsive template, I have a media query in my CSS: @media screen and (max-width: 960px) { body{ /* something */ background: red; } } And, I made a jQuery function on resize to log the width: $(window).resize(…
benoît
  • 1,473
  • 3
  • 13
  • 31
51
votes
5 answers

Common breakpoints for media queries on a responsive site

So I am working on my first responsive website which makes extensive use of media queries. I was wondering if there are some common page-widths I should optimize for. I will probably have a maximum width (not going full fluid) I am thinking I'll…
Zach Lysobey
  • 14,959
  • 20
  • 95
  • 149