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
107
votes
4 answers

Retina displays, high-res background images

This might sound like a silly question. If I use this CSS snippet for regular displays (Where box-bg.png is 200px by 200px); .box{ background:url('images/box-bg.png') no-repeat top left; width:200px; height:200px } and if I use a media…
Dean Elliott
  • 1,503
  • 3
  • 17
  • 16
98
votes
4 answers

Why does the order of media queries matter in CSS?

Of late, I've been designing sites that are more responsive and I've been using CSS media queries frequently. One pattern I noticed is that the order in which the media queries are defined actually matters. I didn't test it in every single browser,…
dsignr
  • 2,295
  • 2
  • 35
  • 45
98
votes
7 answers

Max-Width vs. Min-Width

Most of the tutorials I'm reading on using Media Queries are demonstrating the use of min-width, but I'm rarely seeing people using max-width. Is this some sort of design trend, or pattern, why people are using min-width over max-width? For example,…
James Jeffery
  • 12,093
  • 19
  • 74
  • 108
91
votes
8 answers

Fancy Media Queries with some LESS Magic

It would be nice to wrap css-styles for different resolutions within some css-classes using less. I'd like to do something like: footer { width: 100%; } .tablet { footer { width: 768px; } } .desktop { footer { width: 940px; …
Andre Zimpel
  • 2,323
  • 4
  • 27
  • 42
88
votes
12 answers

CSS Media Query - Soft-keyboard breaks css orientation rules - alternative solution?

I am working with multiple tablet devices - both Android and iOS. Currently I have following resolution variations for all the tablets. 1280 x 800 1280 x 768 1024 x 768 (iPad Obviously) - iPad does not have this issue Simplest way to apply device…
Hossain Khan
  • 6,332
  • 7
  • 41
  • 55
85
votes
3 answers

Should I use max-device-width or max-width?

With CSS media queries you can use max-device-width to target a device width (such as an iPhone or Android device) and/or a max-width that targets a page width. If you use max-device-width, when you change the size of the browser window on your…
Jared
  • 2,978
  • 4
  • 26
  • 45
85
votes
8 answers

How to remove responsive features in Twitter Bootstrap 3?

Since Bootstrap 3 there's no longer seperate files for responsive and standard stylesheets. So how can I easily remove the responsive features?
84
votes
13 answers

How can I detect Internet Explorer (IE) and Microsoft Edge using JavaScript?

I've looked around a lot, and I understand that there's a lot of ways to detect internet explorer. My problem is this: I have an area on my HTML document, that when clicked, calls a JavaScript function that is incompatible with internet explorer of…
user5171952
84
votes
11 answers

Detect if a browser in a mobile device (iOS/Android phone/tablet) is used

Is there a way to detect if a handheld browser is used (iOS/Android phone/tablet)? I tried this with the goal to make an element half as wide in a browser on a handheld device but it doesn't make a difference. width: 600px; @media handheld { width:…
ivvi
  • 5,120
  • 5
  • 36
  • 53
83
votes
15 answers

How to override css prefers-color-scheme setting

I am implementing a dark mode, as macOS, Windows and iOS have all introduced dark modes. There is a native option for Safari, Chrome, and Firefox, using the following CSS media rule: @media (prefers-color-scheme: dark) { body { color:#fff; …
JimmyBanks
  • 4,178
  • 8
  • 45
  • 72
83
votes
8 answers

Media Query Styles Not Overriding Original Styles

I'm attempting to use some media queries for a website I'm building. The problem I'm having however, is while the media query styles are actually being applied, they're being overridden. I can't for the life of me tell why because I'm using the same…
Ben Black
  • 3,751
  • 2
  • 25
  • 43
82
votes
9 answers

CSS media query to target iPad and iPad only?

Hi I am working with multiple tablet devices, iPad, Galaxy Tab, Acer Iconia, LG 3D Pad and so on. iPad - 1024 x 768 LG Pad - 1280 x 768 Galaxy Tab - 1280 x 800 I want to target iPad only using CSS3 media query. Since, device width of LG and iPad…
Hossain Khan
  • 6,332
  • 7
  • 41
  • 55
81
votes
5 answers

iPhone X / 8 / 8 Plus CSS media queries

What are the CSS media queries corresponding to Apple's new devices ? I need to set the body's background-color to change the X's safe area background color.
nathan
  • 9,329
  • 4
  • 37
  • 51
79
votes
11 answers

Media Queries firing at wrong width

I am building a responsive page and the media queries are firing at the wrong width size. I am using Chrome. @media screen and (max-width: 1200px) { .logo-pic { display: none; } } For example, this rule works it just fires at wrong size. This…
swollavg
  • 911
  • 1
  • 6
  • 11
73
votes
7 answers

iPhone 6 and 6 Plus Media Queries

Does anyone know specific screen sizes to target media queries for iPhone 6 and 6 Plus? Also, the icon sizes and splash screens?
MattT
  • 857
  • 1
  • 7
  • 10