This just says that any time there is a screen size bigger than 500px, certain styles will be applied, and when it is below that, it will apply different properties.
Just read your post on using CSS for alternate styles for websites based on screen resolution. Great post.
I’m wondering if I can pay you to make me a simple template for HTML emails (that look like text) that looks good in mobile devices and desktop. Here’s the specs…
If the screen size is not mobile device then I need the text area to wrap at 350px.
If the screen size is mobile device then I want the text to wrap 100% across the screen (for either landscape or non-landscape) with a max width of 350px (in case of something like landscape mode iPad).
I need the text itself to be style font size +1 (so it looks slightly larger then the users normal email messages).
What I’m looking to avoid is using a 350px table to wrap the text and then having the iPhone shrink the text down smaller then normal… Requiring the user to take an extra step zooming in.
Also looking to avoid using end of line breaks at 350px and having those breaks end up mid screen on a mobile device (looking horrible).
Should be pretty simple for someone into this stuff. Let me know if you can do it and how much.
Just FYI, this is impossible if you plan to support all email clients. Gmail strips out the head of your message so media queries wont work. The best you can do is build a fluid layout and use text-size-adjust.
I’m just at the base of my learning curve on using alternate stylesheets for different devices. Following your example elsewhere I’ve sort of got it working. I’m testing it on my HTC Desire Android and for it to work on this phone I’m having to set as follows:
screen and (min-device-width: 1008px) – HTC Desire ignores
screen and (min-device-width: 1009px) – HTC Desire uses this stylesheet
The Desire is 480×800 so there’s a part of this I’m not understanding!
Nexus S reports 533 Pixels in landscape mode. so none of the above snippets will work if the phone is in landscape mode when you navigate to your site. my iPod on the other hand always reports 320px in portrait, as well as in landscape mode. Same for iPhone 4. It always reports 320px.
In my testing, the “min-width: 321px” seems to override most of the others. Is that necessary/helpful? I notice that you (Chris) don’t use it in your code for this site. Looks like you’re only using:
In regards to the media queries working on android browser. I Use Dreamweaver CS5.5. When I tested there was no change .
When I tested in landscape view it takes the default web browser screen width it is not device screen width.
I think I’ve figured it out. When I use “(max-device-width: 800px)” it works. I guess @media is seeing that my Nexus is 480×800 and taking the higher number as the width.
When I use this to request a stylesheet for retina displays:
<link rel=’stylesheet’ type=’text/css’ href=’/css/mobile-retina.css’ media=’only screen and (-webkit-min-device-pixel-ratio: 2)’/>
I can’t get the W3 Validator to validate this. The error is: Bad value only screen and (-webkit-min-device-pixel-ratio: 2) for attribute media on element link: Expected a letter at start of a media feature part but saw – instead.
Is there a way of using the value “-webkit-min-device-pixel-ratio” mentioned in this post in a way so that it’s ‘valid’?
Mike:
I would add Your mobile-retina.css in Your main CSS file?
Example:
/* Start main CSS file */
. . .
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
/* here come styles from mobile-retina.css */
}
/* End main CSS file */
i am writing right now is a page that will change the background image for each screen device because the desktop version is going to have hi-res photos on it. So, I don’t want to make mobile people download these super large images. I want my CSS files to be seperate so i am writing my media queries like this for example:
So a quick question. IF i visit a page that uses media queries does the browser download all of the style sheets or just the one that has been targeted? In my case, the styles.css should always load and then the other media queried stylesheets will load depending if they are true or not.
Is it possible to target the iphone4+ with a separate media query than other smart phones with the same min/width size
I ask because my responsive site (http://www.cortac.com) is shaping up nicely on an iPhone4+. However, in older (2 year old) Android’s, etc., the footer doesn’t *stick*, and I was hoping to simply have the footer display: none;
However, by doing so, that also kills my iphone4+ styling where I *do* want the footer to display. The problem is that the iphone4+ and the normal *smartphone* min/max dimensions are targeting the same dimensions of 320 and 480 respectively and I am unable to get separate devices with same width/height ratios to be targeted differently without overriding each other. Any online articles that talk about this would be greatly appreciated!
Gorgeous site, Jeremy! And I really like how you’ve broken it up with media queries. I stumbled on this post searching for the same problem you had – getting everything to look the same cross-phone-browsers. I think this thread may help me. Thanks for posting and keep up the awesome work.
Does the trick that “german” wrote to “Mike” let you target the iPhone4? iPhone4 and iPhone4S are the one with the retina display?
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
/* here come styles from mobile-retina.css */
}
That should work right? Just put that in your main css file where you hide the footer but then add a style in there to display it…
Also, i checked out the website too, looks great. One question I have is when i collapse the browser i notice that you always use the same set of images. Are you planning to keep it that way or will you optimize it for mobile devices by making the background not as big.
I’ve made my site so that it loads a different background image depending on what kind of device it is. I don’t want to download a 150kb-200kb background on my phone when i don’t have too.
Robbie, thank you for your response. I do have the styles for the retina display only, in addition to regular smartphones, and it does not seem to work on distinguishing between the two. If I eliminate the footer for normal smartphones, it overrides the retina styles regardless. Seems to be all or nothing so far.
Yes, thank you for your observation. I indeed plan on loading in smaller background images for the mobile version. I just have not added in those media queries yet, as this is a responsive experiment for me in progress.
My issue is the same as Marks. I am currently using Bootstrap and it seems like the layout changes should be made when the page is vertical in the ipad, but the layout doesn’t change unless I alter the media queries from 768px to 769px. I’m sure this isn’t the way it’s supposed to be done…
You sir are a genius. Works like a charm! iphone4+ now show a different css and I can now make my desktop browser 800px minimum width and not the minimum 1136px (iphone4+)
I used about media queries in a recent project, when I enter into landscape mode from portrait mode on iPad it appears scaled i.e. its already zoomed. I want it to show on original page zoom on both portrait and landscape mode.
Yes there are solutions for this. The simplest is to use Chrome instead of Safari as this is a mobile Safari bug. You can also look at my post on Forrst for various workarounds http://forrst.com/posts/Fixes_for_Mobile_Safari_Zoom_Orientation_bug-2S0, I currently use the accelerometer based fix.
You are not the only one with safari issues. I am having the same issues with safari picking up @media css as well in ANY resolution. the problem is i did a @media body {overflow:hidden} and safari will not scroll now lol
just a stab in the dark to help you out, but you are saying the minimum width needs to be 321px, which your safari is at least 321px wide, so of course its going trigger on there.
Try using max-width instead.
Look at the code example above, you see for smartphones he uses (max-width:320px;).
I’m pretty new to this media queries thing so I may be missing something but I try to implement lessframework on a page and something weird is happening on my iPhone4:
- when is in portrait it is resizing the page just fine but when I rotate to landscape the page goes out of boundaries
- if I refresh the page while in landscape the page is looking just fine
I am a GUI designer. When designing for a mobile site iPhone retina display I traditionally use a resolution of 640×960. When designing for mobile or iPad in PS should I compensate for the hi res display or just design at the 320px (72 DPI)/768px (72 DPI)?
I really love the idea of a boilerplate snippet for queries, but the portrait and landscape queries for smartphones (more accurately, for iPhones) appear broken in my tests.
I’m also curious why we wouldn’t want to leverage device orientation for this task… also seems slightly more platform agnostic. Detecting portrait mode would then look something like…
@media only screen
and (max-device-width: 320px)
and (orientation: portrait) {
/*-- Styles --*/
}
I am having problems understanding your approach to this. I have been working with a framework lately and working on a responsive grid system. Maybe I am understanding this differently but I set my responsive parts in a separate stylesheet to another stylesheet I call ‘global’ or ‘base’ where in this goes the main grid system. In this part of your script
/* Desktops and laptops ———– */
@media only screen
and (min-width : 1224px) {
/* Styles */
}
/* Large screens ———– */
@media only screen
and (min-width : 1824px) {
/* Styles */
}
you have 1824px which yes is understandable, very large screens, larger then my imac monitor so therefore the grid system can expand. But then you have 1224px, ok this is larger then the resolution of my macbook but 1200px at least is my imac size and to me the standard size of any screen round a bout.
Now what Im getting at is this example:
RESPONSIVE STYLESHEET
/* Desktops and laptops ———– */
@media only screen
and (min-width : 1224px) {
/* Styles */
#container {width:1170px}
}
GLOBAL STYLESHEET
#container {width:980px}
Now I feel in my (round a bout) 1200px size monitor, these two sizes are gonna class. Why not delete (min-width : 1224px) and set it at a much larger pixel rate.
I hope this makes sense. Now here’s how i’ve done it which to me makes more sense. Now between 768px and 1824px I can then create the grid system as normal to whatever size i feel and when it goes below 768px or above 1824px, it changes. But their is no mixing.
/* Large Desktop and Laptop Screens (devices and browsers) */
@media only screen and (min-width: 1824px) {}
/* Tablet Devices, Desktop and Laptop Screens */
@media only screen and (max-width: 768px) {}
/* iPads (portrait and landscape) */
@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {}
/* Smartphones (portrait and landscape) */
@media only screen and (min-device-width: 320px) and (max-device-width: 480px) {}
/* iPhone 4 */
@media only screen and (-webkit-min-device-pixel-ratio: 1.5), only screen and (min-device-pixel-ratio: 1.5) {}
@media print {}
The only other idea I have come up with is that your not creating the css anywhere else and the 1224px is what it is but what if a grid is 960 or 980px which is typical.
Also you have a and (min-device-width : 768px) and (max-device-width : 1024px). I know it’s a device-width, will that not effect it on a desktop or laptop computer?
Sorry about the blunt questions, but im just trying to understand it and I ask a lot of questions. :)
I am just learning, but this might be what you are after?
/**********
iPad 3
**********/
@media
only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape)
and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}
@media
only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait)
and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}
/**********
iPhone 4+
**********/
@media
only screen
and (min-device-width : 320px)
and (max-device-width : 480px)
and (orientation : landscape)
and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}
@media
only screen
and (min-device-width : 320px)
and (max-device-width : 480px)
and (orientation : portrait)
and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}
Hello…
how can I make a special styles for the Iphone4 retina in landscape position?
I can apply the portrait.. but when turn my iphone.. this lost the styles
We have recently launched a new website that uses responsive design.
However we are hearing from some users that the site is not responsive on some mobile devices: HTC’s, Window’s phones, etc.
When I test in an emulator the devices highlighted the site looks fine, but still for some devices it seems not to work.
we are planning a tablet version of our existing website with entirely different content. Now, just want to know the css media query for the same catering all the tablets i.e. android, ipads, noble nook,kindle etc.
When I switch from portrait to landscape on my iphone the site doesn’t align properly. It overflows. I have a 480px style set so what else am I doing wrong? Can anybody help me?
You should use percent based values inside of the stylesheet, and set a max and min width for the link to the stylesheets. Everything should be fluid except of the breakpoints, but the viewport above 900/960px can have a fixed value.
Maybe i know what’s your issue, on iPhone when you switch from portrait to landscape there is a bug screwing user zoom, therefore it overflows (if i understand your problem correctly). If that’s the case, you have to zoom out to “manually fix it” isnt it?
There’s a nifty script here http://github.com/scottjehl/iOS-Orientationchange-Fix, that can fix it for the user automatically, and it works pretty well!
hei there i’ve been trying to do something to say sort of “for anything with device-pixel-ratio>1 use this” .
I’ve tried something with the not operator : @media not screen and (max-device-pixel-ratio: 1) { stuff }
Is it worth in your opinion?
Btw i was wondering if vendor prefixes are still needed for this or just the vanilla
meh the whole thing is wrong X)
i meant @media screen and (not max-device-pixel-ratio:1) {stuff }
still it seems not to be working. I guess the general min-device:1.5 is far better.
All this pixel-fiddling does not make sense. With iPad 4 it is clear: you have to ask for the physical size and not for the pixel-resolution. even on a 640-pixel wide iPad a two column design of a web page does not make sense.
I now realized my responsive web page. See http://mm.ymir.de . It not only adapts to small devices, but also to small browser windows. So just narrow the width of your browser and look how the page changes. Also the pictures are responsive: When loading the page they are loaded according to your window size.
Have you tested which size (min-width/max-width) can target the new MacBook’s Retina display?
I’m not sure if this new high definition laptop screen resolution is conflicting with the above media query for “large screens” (min-width : 1824px).
Hey peops, just to let you know that if you’re using Less with the Less.app on Mac, it doesn’t accept the original ‘device-pixel-ratio’ of 1.5 or indeed the opera equivalent of 3/2.
All device-pixel-ratios need to be 2 only.
Just thought it might help someone out there!
Peas and korn.
Plus, Chris – what is your view on the HTML5 form validation strictness of the URL input type. I really have to include my http:// – really? What is up with that…
I used media query it works in browser when i re-sized my browser, but it not work with mobile device, ipad, android , android mobile screen size width 320px, but it takes @media only screen and (min-width:768px) and (max-width:1024px) this query , what i do?
Hey guys, when it comes to responsive there are many ways to accomplish your goals. Probably one of the most easy and common is the way Chris describes here.
One that I have learned is to use increase max widths to target general smartphone devices such as the Samsung Galaxy 3 and others.
@media only screen
and (min-device-width: 320px)
and (max-device-width : 640px) {
/* Your Styles Here*/
}
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Your Styles Here */
}
Umm, what Marcy? Nobody validates HTML anymore? Looks like you may need to work in another field.
Google loves valid HTML, and so does it’s crawlers. If you don’t have valid HTML, you don’t have a site worthy of indexing. This is one thing that makes HTML 5 so great. It is semantic. Just because it works does not mean it’s working for you or your clients.
Look, I work in the industry and I’m speaking from my personal experience. I know web standards and I write code by hand every day. If I run into a problem I can’t spot easily, validation is one potential tool but no one at my agency validates as part of our regular workflow anymore. That’s just the way I see it.
@f1ss1on: You’re parsing words there. Marcy isn’t saying “dude just write shitty broken HTML, it’s cool! She’s saying you shouldn’t worry too much about validators because they don’t take into account the complexity of our rapidly evolving ecosystem. Validators often puke on code which is perfectly fine.
f1ss1on: But what will your code do on a Samsung Galaxy 4 and an iPhone 5? Why do you ask for pixel size instead of physical sizes? It will only save your employment changing the same code again and again to add each new device.
<script type="text/javascript">
// set size for image processing on server
function setSize() {
document.cookie='resolution='+ window.innerWidth +'; path=/';
}
</script>
<meta name="viewport"
content="width=device-width,
nitial-scale=1"
/>
<link rel="stylesheet"
type="text/css"
href="css/small.css"
media="screen and (max-width: 14cm)"
/>
<link rel="stylesheet"
type="text/css"
href="css/medium.css"
media="screen and (min-width: 14cm)
and (max-width: 20cm)"
/>
<link rel="stylesheet"
type="text/css"
href="css/large.css"
media="screen and (min-width: 20cm)"
/>
<body onLoad="setSize();" onResize="setSize();">
Marcy also said that the page fold is a myth. Which is another case in which she is wrong. It has nothing to do with whether people know to scroll or not- it’s the notion of captivating your audience quickly. Putting the right content above the fold entices them to scroll in the first place.
What I don’t understand is your need to make insulting personal comments like this. Debating ideas is awesome – insulting somebody and questioning their right to practice a discipline is immature and unprofessional.
How about making this more about media queries & sharing knowledge and less about cutting people down? Everyone will benefit from more code and less negativity.
It is my opinion that misinformation passed around by people in my industry who claim to be experts is one of its biggest downsides. Especially when they present their opinions as fact, like Marcy did. Junior developers are led astray every day by comments from people like this.
Your responses are over dramatic. I did not personally attack Marcy; she tried to use her agency’s practices to qualify a misguided statement. I responded to it.
Hi all, becareful with Media Queries! They are not pixel accurate.
Some times they are off only 20 pixels but other time they are off up to 150 pixels… However, even few pixels is very bad when trying to target a certain mobile phone screen.
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Your Styles Here */
}
is not working when i test the site im working using ipadpeek. but works when i removed the “device-” is there any difference between them? thanks, this is my first time developing responsive website so im a little confused. please guide me.
Vhinmanansala, I am just reading Ethan Marcottes Responsive Web Design and he mentions that min-width and min-device -width are two different things. I guess the essence is that a screen is not considered a device.
when i try to use this code.. it doesn’t work correctly
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) {
}
but.. when i change the code to..
@media only screen and (min-width : 768px) and (max-width : 1024px){
}
it can be work correctly.. i’m so confuse bout what is happen with that.. please explain to me..
If anyone here needs a way to quickly generate valid custom media queries or need to look up other device specific presets, I wrote a free media query tool called Media Query Builder which you can find here -> Media Query Builder
I just found something strange that might save people a lot of wasted time with media queries on Android devices:
I’m developing a mobile site and for some reason, a 540×960 phone running Android Jellybean would only pick up the 360-459px wide stylesheet. When I commented out this stylesheet from the header, the phone totally ignored the others, even the one that specifically targets it’s resolution.
<link rel="stylesheet" type="text/css" media="only screen and (min-device-width:540px) and (max-device-width:800px)" href="mob_vhigh.css"/>
<link rel="stylesheet" type="text/css" media="only screen and (min-device-width:480px) and (max-device-width:639px)" href="mob_high.css"/>
<link rel="stylesheet" type="text/css" media="only screen and (min-device-width:360px) and (max-device-width:479px)" href="mob_mid.css"/>
<link rel="stylesheet" type="text/css" media="only screen and (min-device-width:0px) and (max-device-width:359px)" href="mob_small.css" />
After a ton of research, it turns out that the default browser zoom setting does not actually zoom in the browser per se, but rather ignores it’s physical resolution, and pretends to have a smaller one. After some experiments:
‘Close’ zoom loads ‘small.css’,
‘Medium’ zoom loads ‘mid.css’ and
‘Far’ loads the proper stylesheet for the screen’s actual resolution.
It would appear there is no way to force the user’s browser to load the proper size stylesheet if their settings are different. Just thought I’d share this, it’s wasted a lot of my time, and finally I can move on :) Hope it helps
Hi again, here’s a few more things I’ve since learnt about the subtleties of Android devices:
There are several attributes that can be applied the the tag that alter the way they handle zoom. One of these is: content="target-densitydpi=device-dpi"
What this does is prevents the device from zooming on the page when it loads. This solved an irritating zoomed-on-load issue I suddenly had with the iPhone 4s. Users can still scale (if you’ve allowed it), it merely targets the device’s actual DPI rating, instead of relying on the often unpredictable automatic zoom. My meta tag now looks like this:
This allows for the layout to adjust completely based on any display. The only hitch is the OS. For some reason, I can’t for the life of me get it to load on a HTC 7 Trophy windows phone without being zoomed in. Any thoughts? This is the only OS that seems to cause problems now. Even Blackberries work fine.
In another tweak, I’ve updated my media queries in an attempt to ignore most tablets while still catering for ludicrously high-DPI phones, such as the HTC Droid DNA (1920×1080, wow). To do this, I’ve simply added another media query with a new condition below the main one:
‹link rel="stylesheet" type="text/css" media="only screen and (min-device-width:540px) and (max-device-width:960px)" href="mob_vhigh.css"/›
‹link rel="stylesheet" type="text/css" media="only screen and (min-device-width:961px) and (min-webkit-device-pixel-ratio:2) and (min-device-pixel-ratio:2)" href="mob_vhigh.css"/›
This way, the phones load like normal, but any display larger than 960px must have a very high DPI display to access the styles, so hopefully this will be phones only. It’s not perfect, as I haven’t had a chance to try it on the Retina iPad (DPI anyone?), but it is designed to quickly provide a clear divide.
Rather than try and guess how high-res the next generation of phones will be, and have the site inevitably break on some new device in the future, I thought it made more sense to simply allow no limit and instead exclude the devices I didn’t want by using media queries. Having your mobile layout designed for small screens appear on a iPad makes you feel like a spoon-fed kid, as the buttons are now big enough to smash with your fists instead of point with a finger.
I found that some of the media queries here a bit haphazard. For example, the smartphone landscape query is min-width:321px my desktop browser when larger then 321px wide will run the stylesheet intended for smartphones. Seen how iOS+Safari doesn’t consider itself a handheld device, using @media handheld and (... does not work, so I found that some were clashing with others were they shouldn’t have been. After many frustrating hours I went looking and found this question on Stack Overflow, Media query ipad vs iphone4
The media queries are being used to specifically target a device width, pixel density(css ratio) and the orientation. Leaving very little for one device to use another stylesheet not intended for it. Although this maybe slightly more to write out, you get the consistency across all devices and I have never had any headaches using this method since.
I am pretty new with media queries. I have a website I am creating a mobile version for. When I check the site in iphone 4s portrait, 320px wouldn’t cover 100% the width. I need to push it to 375px to make it cover the width of portrait view. Any suggestions?
I am using: inside code
and html { -webkit-text-size-adjust: none; } inside css
the media querie I am using is:
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi)
I have tried the same media queries for wordpress test site loaded on localhost. I used online responsive design tool http://ipadpeek.com/ . This tool fails to catch landscape and portrait mode queries. But while I tested localhosted wordpress site directly by changing screen resolution of my pc with windows 7 in portrait and landscape its works.
I know article is from a while back, but has anyone had an issue with “max-device-width” and having rendering issue.
My impression was that the “max-device-width” only rendered on phones, tablets, and mobile devices, not on desktop browsers.
This happens when the browser is set and the initial load of the css file with the @Media query loads.
So if I have
@media screen and (max-width:500px), screen and (max-device-width: 1280px){/* My mobile styles here */}
the desktop browser or screen resolution of the desktop is set at 1280px, it will actually render the 1280px styles mobile styles and not the desktop styles.
Did browser makers roll “max-device-width” into their browser builds? Any pointers or insite would be great.
I have built my first truely responsive site, with a lot of help from CSS Tricks. I built it in SCSS with lots of media queries using breakpoints as outlined by Chris C. My question is- I have only user min-width and max-width (No min-device-width or max-device-width). Is this incorrect? As far as I can tell my site is working fine across all devices, though testing UserAgents is a pain. Most of my testing comes from resizing the browser in Chrome, IE and FF, and then using Chrome Dev Tools to test on Galaxy Nexus and also on my wife’s iPhone. Here is an example of my breakpoints.
@mixin breakpoint($point) {
@if $point == 1600 {
@media (max-width: 1600px) and (min-width: 1280px) { @content; }}
@else if $point == 1280 {
@media (max-width: 1280px) and (min-width: 1024px) { @content; }}
@else if $point == 1024 {
@media (max-width: 1024px) and (min-width: 800px) { @content; }}
@else if $point == 800 {
@media (max-width: 800px) and (min-width: 640px) { @content; }}
@else if $point == 640 {
@media (max-width: 640px) and (min-width: 320px) { @content; }}
@else if $point == 480 {
@media (max-width: 480px) and (min-width: 320px) { @content; }}
@else if $point == 320 {
@media (max-width: 320px) and (min-width: 0px) { @content; }}
I am entirely new in this topic. But this seems very interesting. Could you drop an example what the @content does mean and/or where and what shall be stated in it?
Thanks a lot.
Martin- Sure! With the above code, I can use SCSS and mixins to easily make my site mobile responsive. Rather than creating a huge block of CSS at the bottom of my stylesheets and putting all my mobile CSS there, I add in my mobile CSS as I design my normal CSS. I then use a gem which moves everything to the end for production. Moving forward with the above breakpoints, my CSS might like like this:
My above illustrations was simple… but when you use this for an entire site it becomes super powerful. When I need to go back and edit the screen width of one div only on mobile devices at 640px, I simply go to that class in my CSS and make changes- I don’t need to scroll through a page of mobile CSS to make changes.
As Chris Coyer said the above way(using mixins and SCSS) is the way your brain wants to connect the dots, so why organize things differently. Since I have started to do this, I can just code mobile as I go. I can put together a sites CSS and responsive all at once, rather than two separate items.
Hi Chris. Great article. I often refer back to it. I was just thinking that a lot has evolved and come about with mobiles and tablets since you wrote this article back in October 2010. Smaller tablets have been released and some mobile resolutions have gotten bigger. Has this blurred the lines somewhat with your advised media queries for different devices? Or would you say it’s largely unchanged?
I’ve been doing some testing on this and using a sony xperia s it seems to think that device width changes with orientation (720px then 1280px).
This would lend one to use something like this to target Android:
@media only screen
and (min-device-width : 720px)
and (max-device-width : 1280px)
The problem with this is that it overlaps with the iPad and using min-device-pixel-ratio is not enough to separate them when you get to the iPad 3 as they both have that as 2
Does anyone know how to make android behave and yet not get my mobile site to appear on the ipad.
Because browser vendors don’t support the “NOT” selector from W3C I had to rely on JavaScript / jQuery to isolate the CSS that I didn’t want to show on iOS and Android device.
<script type="text/javascript">
//detect if browser supports touch
var is_touch_device = 'ontouchstart' in document.documentElement;
if(is_touch_device){
$('body').addClass('touchDevice');
}else{
$('body').addClass('notTouch');
};
</script>
This detailed browser compatibility table of the viewport (and some related items) from Peter-Paul Koch will help anyone interested in this subject in separating the facts from the utopias.
I have a website Noobpost I’m trying to figure out how many media queries max-width sizes I should use, right now I have four, but it doesn’t seem to work well with landscape views. It also appears I need to position everything in a percentage to make it more fluid.
You could always just keep the content centered (or left aligned) on the page at a normal width, say 960 or 1024px which would allow it to be readable. To fill up the extra space on the sides you could use a gradient, clever shadows, or some images to “fill” up the blank space. My site maxes out at 960px so it looks the same on 1600 wide monitors, and 1900 wide monitors and beyond. A word of advice- when designing for large screen, testing or actually designing for a large screen is really helpful- it is hard to visualize a large screen when working on a 13″ laptop.
Hi, I have some question, in ipad protrait mode…
you have specified the min-device-width:768px and max-device-width:1024px and orientation portrait and the same values you given for landscape too. Actually, when viewing with portrait in ipad the max-device-width will be 768px right? How the max-device-width be 1024px.
Is there a correct meta tag to go with this? I’m trying to target the iPhone4 and below screen widths (and not the iPad2 and above screen widths). But this isnt even working for iPhone4′s..
This is an informative article however i would approach responsive design much the same as we currently do for fully fluid layouts. My personal approach is semi fluid in nature with responsive design only to enhance the content.
If for example we apply a fixed width on the container (div as the most semantic selector) Then we can simply target that by simply applying a max width, minimum width and auto for backwards compatibility. Of course will work for semi modern browsers this is the best option in my opinion. We can scale this up or down based upon a percentage and change the width constrictions and there we have a complete fluid and responsive design.
Of course other elements needs to be specifically styled, rearranged or sometimes hidden (i would not recommend) by using media queries.
Hi,
I am working on a website to make it responsive.
1) Mobile phones including iphone series using a css file (includes portrait and landscape layouts with @media),
2) Tablets & ipads using a css file (includes portraits and landscape layouts with @media),
3) and desktop version css (which for large, medium and small break points usig @media spearated.)
Is there a way to accomplish the complete workaround some thing like this
link href="desktop.css" rel="stylesheet" media='screen and (min-width:100px) and (max-width:1280px)' css file for desktop version
link rel="stylesheet" media="only screen and (device-width: 768px) and (device-height: 1024px) and (-webkit-min-device-pixel-ratio: 1)" href="ipad.css" for ipads & tablets
link href="iphone.css" rel="stylesheet" media='only screen and (max-device-width : 650px)' for mobiles, iphones only
I know I might be wrong here, as I am very new to this.. Please help me.
Pretty useful! I’ll have to try it though.
What are you using to switch between mobile width and full width on your current site?
I don’t quite see it in your source.
He isn’t using a different width just for mobile. He is using things such as
This just says that any time there is a screen size bigger than
500px, certain styles will be applied, and when it is below that, it will apply different properties.Just read your post on using CSS for alternate styles for websites based on screen resolution. Great post.
I’m wondering if I can pay you to make me a simple template for HTML emails (that look like text) that looks good in mobile devices and desktop. Here’s the specs…
If the screen size is not mobile device then I need the text area to wrap at 350px.
If the screen size is mobile device then I want the text to wrap 100% across the screen (for either landscape or non-landscape) with a max width of 350px (in case of something like landscape mode iPad).
I need the text itself to be style font size +1 (so it looks slightly larger then the users normal email messages).
What I’m looking to avoid is using a 350px table to wrap the text and then having the iPhone shrink the text down smaller then normal… Requiring the user to take an extra step zooming in.
Also looking to avoid using end of line breaks at 350px and having those breaks end up mid screen on a mobile device (looking horrible).
Should be pretty simple for someone into this stuff. Let me know if you can do it and how much.
Just FYI, this is impossible if you plan to support all email clients. Gmail strips out the head of your message so media queries wont work. The best you can do is build a fluid layout and use text-size-adjust.
I’m just at the base of my learning curve on using alternate stylesheets for different devices. Following your example elsewhere I’ve sort of got it working. I’m testing it on my HTC Desire Android and for it to work on this phone I’m having to set as follows:
screen and (min-device-width: 1008px) – HTC Desire ignores
screen and (min-device-width: 1009px) – HTC Desire uses this stylesheet
The Desire is 480×800 so there’s a part of this I’m not understanding!
have you used device width, initial scale=1 in your meta? mobile devices sometimes rescale their viewings…
Here can you find more Media Query Snippets. http://nmsdvid.com/snippets/
This is awesome!!! thanks.
This media queries snippet are just great, well organized and very clear.. Thanks a lot
Nexus S reports 533 Pixels in landscape mode. so none of the above snippets will work if the phone is in landscape mode when you navigate to your site. my iPod on the other hand always reports 320px in portrait, as well as in landscape mode. Same for iPhone 4. It always reports 320px.
Is this set of media queries suitable for the “mobile-and-up” approach?
In my testing, the “min-width: 321px” seems to override most of the others. Is that necessary/helpful? I notice that you (Chris) don’t use it in your code for this site. Looks like you’re only using:
@media (max-width: 1350px) {}
@media (max-width: 1200px) {}
@media (max-width: 1024px) {}
@media (max-width: 860px) {}
Thanks!
You are correct, but I’m not sure what the solution is here. Chris?
If you use
min-width:321px, thenmax-width:500px, the min width property overrides it because it applies all styles above321px.Nice Article .thank u
Hey Guys !!!!!
I Have a Problem
In regards to the media queries working on android browser. I Use Dreamweaver CS5.5. When I tested there was no change .
When I tested in landscape view it takes the default web browser screen width it is not device screen width.
Same problem. Android browser adopts default stylesheet instead of mobile stylesheet. Testing on Nexus S.
I think I’ve figured it out. When I use “(max-device-width: 800px)” it works. I guess @media is seeing that my Nexus is 480×800 and taking the higher number as the width.
When I use this to request a stylesheet for retina displays:
<link rel=’stylesheet’ type=’text/css’ href=’/css/mobile-retina.css’ media=’only screen and (-webkit-min-device-pixel-ratio: 2)’/>
I can’t get the W3 Validator to validate this. The error is: Bad value only screen and (-webkit-min-device-pixel-ratio: 2) for attribute media on element link: Expected a letter at start of a media feature part but saw – instead.
Is there a way of using the value “-webkit-min-device-pixel-ratio” mentioned in this post in a way so that it’s ‘valid’?
Mike:
I would add Your mobile-retina.css in Your main CSS file?
Example:
/* Start main CSS file */
. . .
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
/* here come styles from mobile-retina.css */
}
/* End main CSS file */
Nobody really validates HTML anymore since there are so many evolving standards. If your site works then why do you care?
(orientation : portrait) doesn’t validate for CSS3…. Are you ignoring that? Or do you have a workaround?
Thank you….
i am writing right now is a page that will change the background image for each screen device because the desktop version is going to have hi-res photos on it. So, I don’t want to make mobile people download these super large images. I want my CSS files to be seperate so i am writing my media queries like this for example:
So a quick question. IF i visit a page that uses media queries does the browser download all of the style sheets or just the one that has been targeted? In my case, the styles.css should always load and then the other media queried stylesheets will load depending if they are true or not.
Is it possible to target the iphone4+ with a separate media query than other smart phones with the same min/width size
I ask because my responsive site (http://www.cortac.com) is shaping up nicely on an iPhone4+. However, in older (2 year old) Android’s, etc., the footer doesn’t *stick*, and I was hoping to simply have the footer display: none;
However, by doing so, that also kills my iphone4+ styling where I *do* want the footer to display. The problem is that the iphone4+ and the normal *smartphone* min/max dimensions are targeting the same dimensions of 320 and 480 respectively and I am unable to get separate devices with same width/height ratios to be targeted differently without overriding each other. Any online articles that talk about this would be greatly appreciated!
Did you try checking for the existence of a retina display (-webkit-min-device-pixel-ratio)?
LOL!
“Jeremy Mansfield: Jesus Lover…”
Gorgeous site, Jeremy! And I really like how you’ve broken it up with media queries. I stumbled on this post searching for the same problem you had – getting everything to look the same cross-phone-browsers. I think this thread may help me. Thanks for posting and keep up the awesome work.
Does the trick that “german” wrote to “Mike” let you target the iPhone4? iPhone4 and iPhone4S are the one with the retina display?
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
/* here come styles from mobile-retina.css */
}
That should work right? Just put that in your main css file where you hide the footer but then add a style in there to display it…
Also, i checked out the website too, looks great. One question I have is when i collapse the browser i notice that you always use the same set of images. Are you planning to keep it that way or will you optimize it for mobile devices by making the background not as big.
I’ve made my site so that it loads a different background image depending on what kind of device it is. I don’t want to download a 150kb-200kb background on my phone when i don’t have too.
Robbie, thank you for your response. I do have the styles for the retina display only, in addition to regular smartphones, and it does not seem to work on distinguishing between the two. If I eliminate the footer for normal smartphones, it overrides the retina styles regardless. Seems to be all or nothing so far.
Yes, thank you for your observation. I indeed plan on loading in smaller background images for the mobile version. I just have not added in those media queries yet, as this is a responsive experiment for me in progress.
My issue is the same as Marks. I am currently using Bootstrap and it seems like the layout changes should be made when the page is vertical in the ipad, but the layout doesn’t change unless I alter the media queries from 768px to 769px. I’m sure this isn’t the way it’s supposed to be done…
This is in the my header.php
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
There are several reasons to use the viewport-width instead of the device-width.
Read more under:
http://www.quirksmode.org/blog/archives/2010/09/combining_meta.html
You sir are a genius. Works like a charm! iphone4+ now show a different css and I can now make my desktop browser 800px minimum width and not the minimum 1136px (iphone4+)
Hi now i want to put |Home| page in my web. can you express to me.
@Run Raksmey
Sure via fed express, $1.78 per pixel.
Hello,
I used about media queries in a recent project, when I enter into landscape mode from portrait mode on iPad it appears scaled i.e. its already zoomed. I want it to show on original page zoom on both portrait and landscape mode.
Any solution for the same?
Yes there are solutions for this. The simplest is to use Chrome instead of Safari as this is a mobile Safari bug. You can also look at my post on Forrst for various workarounds http://forrst.com/posts/Fixes_for_Mobile_Safari_Zoom_Orientation_bug-2S0, I currently use the accelerometer based fix.
You can overcome this with some Javascript.
Add this to your
<head>element.<meta name="viewport" content="width=device-width, initial-scale=1.0">Then:
Hi!
Am I really the only one with issues using media queries and Desktop Safari?!
@media only screen and (min-device-width : 321px)
targets safari browser on desktop aswell, regardless of size of browserwindow.
You are not the only one with safari issues. I am having the same issues with safari picking up @media css as well in ANY resolution. the problem is i did a @media body {overflow:hidden} and safari will not scroll now lol
just a stab in the dark to help you out, but you are saying the minimum width needs to be 321px, which your safari is at least 321px wide, so of course its going trigger on there.
Try using max-width instead.
Look at the code example above, you see for smartphones he uses (max-width:320px;).
Yeah I know, but I’m refering to this in the examples:
/* Smartphones (landscape) ———– */
@media only screen
and (min-width : 321px) {
/* Styles */
}
Changing it to min-device-width doesn’t help either.
What would be the heights associated with the widths? I’m weary about above-the-fold problems.
You would have to research each device you want to target to find out the heights or find the phone with the lowest resolution to use as a baseline.
Honestly, you should just structure your page so all important vital things are at the top and less vital things below that.
Amd make it so your 2ndary things are partly visible so they know tp scroll, but I think it is mostly implied.
Look at other mobile sites and measure.
The page fold is a myth…people know how to scroll. Especially on mobile web.
Hello everyone,
I’m pretty new to this media queries thing so I may be missing something but I try to implement lessframework on a page and something weird is happening on my iPhone4:
- when is in portrait it is resizing the page just fine but when I rotate to landscape the page goes out of boundaries
- if I refresh the page while in landscape the page is looking just fine
I also noticed exactly the same thing on their website http://www.lessframework.com.
Does anyone know how to fix this?
Thanks,
Vic
I had the same problem on the Ipad. This is a ios bug.
Small javascript fixed the problem.
See: http://stackoverflow.com/questions/5434656/ipad-layout-scales-up-when-rotating-from-portrait-to-landcape
Hi Chris,
I am a GUI designer. When designing for a mobile site iPhone retina display I traditionally use a resolution of 640×960. When designing for mobile or iPad in PS should I compensate for the hi res display or just design at the 320px (72 DPI)/768px (72 DPI)?
Thanks for the help.
Jared
Hey there!
Thanks for the useful info. Im a newbie & hungry for good advice & coming across great content & design inspires me even more.
I love your input boxes with the images in them! Can you tell me how or where i could find out how to use them?
Thanks, marc
I really love the idea of a boilerplate snippet for queries, but the portrait and landscape queries for smartphones (more accurately, for iPhones) appear broken in my tests.
I’m also curious why we wouldn’t want to leverage device orientation for this task… also seems slightly more platform agnostic. Detecting portrait mode would then look something like…
I am having problems understanding your approach to this. I have been working with a framework lately and working on a responsive grid system. Maybe I am understanding this differently but I set my responsive parts in a separate stylesheet to another stylesheet I call ‘global’ or ‘base’ where in this goes the main grid system. In this part of your script
/* Desktops and laptops ———– */
@media only screen
and (min-width : 1224px) {
/* Styles */
}
/* Large screens ———– */
@media only screen
and (min-width : 1824px) {
/* Styles */
}
you have 1824px which yes is understandable, very large screens, larger then my imac monitor so therefore the grid system can expand. But then you have 1224px, ok this is larger then the resolution of my macbook but 1200px at least is my imac size and to me the standard size of any screen round a bout.
Now what Im getting at is this example:
RESPONSIVE STYLESHEET
/* Desktops and laptops ———– */
@media only screen
and (min-width : 1224px) {
/* Styles */
#container {width:1170px}
}
GLOBAL STYLESHEET
#container {width:980px}
Now I feel in my (round a bout) 1200px size monitor, these two sizes are gonna class. Why not delete (min-width : 1224px) and set it at a much larger pixel rate.
I hope this makes sense. Now here’s how i’ve done it which to me makes more sense. Now between 768px and 1824px I can then create the grid system as normal to whatever size i feel and when it goes below 768px or above 1824px, it changes. But their is no mixing.
/* Large Desktop and Laptop Screens (devices and browsers) */
@media only screen and (min-width: 1824px) {}
/* Tablet Devices, Desktop and Laptop Screens */
@media only screen and (max-width: 768px) {}
/* iPads (portrait and landscape) */
@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {}
/* Smartphones (portrait and landscape) */
@media only screen and (min-device-width: 320px) and (max-device-width: 480px) {}
/* iPhone 4 */
@media only screen and (-webkit-min-device-pixel-ratio: 1.5), only screen and (min-device-pixel-ratio: 1.5) {}
@media print {}
The only other idea I have come up with is that your not creating the css anywhere else and the 1224px is what it is but what if a grid is 960 or 980px which is typical.
Also you have a and (min-device-width : 768px) and (max-device-width : 1024px). I know it’s a device-width, will that not effect it on a desktop or laptop computer?
Sorry about the blunt questions, but im just trying to understand it and I ask a lot of questions. :)
Is there a way to distinguish between an iPhone 4 and an iPad 3…? Just using the pixel-ratio doesn’t seem to work…
Old iPads are distinguished, but not the new ones…
Thansk,
JH
I am just learning, but this might be what you are after?
/**********
iPad 3
**********/
@media
only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape)
and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}
@media
only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait)
and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}
/**********
iPhone 4+
**********/
@media
only screen
and (min-device-width : 320px)
and (max-device-width : 480px)
and (orientation : landscape)
and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}
@media
only screen
and (min-device-width : 320px)
and (max-device-width : 480px)
and (orientation : portrait)
and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}
Hello…
how can I make a special styles for the Iphone4 retina in landscape position?
I can apply the portrait.. but when turn my iphone.. this lost the styles
this is right?
Gazu, here is how I style for landscape with media queries:
@media screen and (max-width:480px) and (orientation: landscape){}
Also to fix the safari bug I put this in the head:
<meta name=”viewport” content=”width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no” />
You saved my day, thanks!
Thank you very much Vic
We have recently launched a new website that uses responsive design.
However we are hearing from some users that the site is not responsive on some mobile devices: HTC’s, Window’s phones, etc.
When I test in an emulator the devices highlighted the site looks fine, but still for some devices it seems not to work.
Any ideas?
http://www.zappybaby.be/nl
http://forum.zappybaby.be
i can’t use media query in bigcommerce template, did not work, shal i need any java script for it? pls help
no thanks , now i used media queries well
Hi,
we are planning a tablet version of our existing website with entirely different content. Now, just want to know the css media query for the same catering all the tablets i.e. android, ipads, noble nook,kindle etc.
I am new to this field actually.
When I switch from portrait to landscape on my iphone the site doesn’t align properly. It overflows. I have a 480px style set so what else am I doing wrong? Can anybody help me?
You should use percent based values inside of the stylesheet, and set a max and min width for the link to the stylesheets. Everything should be fluid except of the breakpoints, but the viewport above 900/960px can have a fixed value.
Maybe i know what’s your issue, on iPhone when you switch from portrait to landscape there is a bug screwing user zoom, therefore it overflows (if i understand your problem correctly). If that’s the case, you have to zoom out to “manually fix it” isnt it?
There’s a nifty script here http://github.com/scottjehl/iOS-Orientationchange-Fix, that can fix it for the user automatically, and it works pretty well!
hei there i’ve been trying to do something to say sort of “for anything with device-pixel-ratio>1 use this” .
I’ve tried something with the not operator :
@media not screen and (max-device-pixel-ratio: 1) { stuff }Is it worth in your opinion?
Btw i was wondering if vendor prefixes are still needed for this or just the vanilla
meh the whole thing is wrong X)
i meant
@media screen and (not max-device-pixel-ratio:1) {stuff }still it seems not to be working. I guess the general min-device:1.5 is far better.
All this pixel-fiddling does not make sense. With iPad 4 it is clear: you have to ask for the physical size and not for the pixel-resolution. even on a 640-pixel wide iPad a two column design of a web page does not make sense.
I now realized my responsive web page. See http://mm.ymir.de . It not only adapts to small devices, but also to small browser windows. So just narrow the width of your browser and look how the page changes. Also the pictures are responsive: When loading the page they are loaded according to your window size.
http://responsive.is/www.bostonglobe.com
Have you tested which size (min-width/max-width) can target the new MacBook’s Retina display?
I’m not sure if this new high definition laptop screen resolution is conflicting with the above media query for “large screens”
(min-width : 1824px).Hey peops, just to let you know that if you’re using Less with the Less.app on Mac, it doesn’t accept the original ‘device-pixel-ratio’ of 1.5 or indeed the opera equivalent of 3/2.
All device-pixel-ratios need to be 2 only.
Just thought it might help someone out there!
Peas and korn.
Plus, Chris – what is your view on the HTML5 form validation strictness of the URL input type. I really have to include my http:// – really? What is up with that…
Marcy is obviously new to web dev.
I love you!
Hey Chris, great info. Any ideas on how to check using PHP for a particular screen size, in this case for an iPhone, or max-screen-size: 320px?
I used media query it works in browser when i re-sized my browser, but it not work with mobile device, ipad, android , android mobile screen size width 320px, but it takes @media only screen and (min-width:768px) and (max-width:1024px) this query , what i do?
Don’t forget the metatag in the html page:
And for selection of the design think about using the width and height in inch or cm instead of pixels. See my website
meta name=”viewport” content=”width=device-width, initial-scale=1″
pls tell me cross browser compatible media query for retina
Use ithis website for ur refrense http://css3please.com/
Hey guys, when it comes to responsive there are many ways to accomplish your goals. Probably one of the most easy and common is the way Chris describes here.
One that I have learned is to use increase max widths to target general smartphone devices such as the Samsung Galaxy 3 and others.
Umm, what Marcy? Nobody validates HTML anymore? Looks like you may need to work in another field.
Google loves valid HTML, and so does it’s crawlers. If you don’t have valid HTML, you don’t have a site worthy of indexing. This is one thing that makes HTML 5 so great. It is semantic. Just because it works does not mean it’s working for you or your clients.
Please refer to this if you have questions.
Google SEO Test – Google Prefers W3C Valid HTML & CSS?
Look, I work in the industry and I’m speaking from my personal experience. I know web standards and I write code by hand every day. If I run into a problem I can’t spot easily, validation is one potential tool but no one at my agency validates as part of our regular workflow anymore. That’s just the way I see it.
@f1ss1on: You’re parsing words there. Marcy isn’t saying “dude just write shitty broken HTML, it’s cool! She’s saying you shouldn’t worry too much about validators because they don’t take into account the complexity of our rapidly evolving ecosystem. Validators often puke on code which is perfectly fine.
f1ss1on: But what will your code do on a Samsung Galaxy 4 and an iPhone 5? Why do you ask for pixel size instead of physical sizes? It will only save your employment changing the same code again and again to add each new device.
Marcy also said that the page fold is a myth. Which is another case in which she is wrong. It has nothing to do with whether people know to scroll or not- it’s the notion of captivating your audience quickly. Putting the right content above the fold entices them to scroll in the first place.
Sounds like Marcy needs to find a new agency.
What I don’t understand is your need to make insulting personal comments like this. Debating ideas is awesome – insulting somebody and questioning their right to practice a discipline is immature and unprofessional.
How about making this more about media queries & sharing knowledge and less about cutting people down? Everyone will benefit from more code and less negativity.
It is my opinion that misinformation passed around by people in my industry who claim to be experts is one of its biggest downsides. Especially when they present their opinions as fact, like Marcy did. Junior developers are led astray every day by comments from people like this.
Your responses are over dramatic. I did not personally attack Marcy; she tried to use her agency’s practices to qualify a misguided statement. I responded to it.
Again, you’re stating your opinion as fact, and the opinion of others as misinformation.
If you think I’m being dramatic by pointing that out, I have nothing more to add.
Marcy, I apologize for insulting you. I’ll word my comments differently in the future.
Hi all, becareful with Media Queries! They are not pixel accurate.
Some times they are off only 20 pixels but other time they are off up to 150 pixels… However, even few pixels is very bad when trying to target a certain mobile phone screen.
Hi, im wondering why
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Your Styles Here */
}
is not working when i test the site im working using ipadpeek. but works when i removed the “device-” is there any difference between them? thanks, this is my first time developing responsive website so im a little confused. please guide me.
Today this article very help me :) Thanks!
Vhinmanansala, I am just reading Ethan Marcottes Responsive Web Design and he mentions that min-width and min-device -width are two different things. I guess the essence is that a screen is not considered a device.
Hey guys.. i need your help..
when i try to use this code..
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) {
}
but.. when i change the code to..
@media only screen and (min-width : 768px) and (max-width : 1024px){
}
it can be work correctly.. i’m so confuse bout what is happen with that.. please explain to me..
btw,this is my blog http://idwds.blogspot.com who i work for progress..please check
Hey guys.. i need your help..
when i try to use this code.. it doesn’t work correctly
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) {
}
but.. when i change the code to..
@media only screen and (min-width : 768px) and (max-width : 1024px){
}
it can be work correctly.. i’m so confuse bout what is happen with that.. please explain to me..
btw,this is my blog http://idwds.blogspot.com who i work for progress..please check
@media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* Styles */
}
I used this option to hide some elements for one of my site.
It works fine for iPhone but doesnot work on other android phones
I have been using it like this to make it web & mobile resonsive.
@media only screen and (min-width: 0px) and (max-width: 480px) {
.wrapper {margin: 0 auto; max-width: 98%}
container {margin: 2% auto; width: 96%}
}
@media only screen and (min-width: 481px) and (max-width: 719px) {
.wrapper {margin: 0 auto; max-width: 98%}
container {margin: 2% auto; width: 99%}
}
I have been using it like this to make it web & mobile resonsive.
@media only screen and (min-width: 0px) and (max-width: 480px) {
.wrapper {margin: 0 auto; max-width: 98%}
container {margin: 2% auto; width: 96%}
}
@media only screen and (min-width: 481px) and (max-width: 719px) {
.other_sized_properties
}
What if I want the same Media Query to target the iPad AND the desktop (max-width: 768), without duplicating the code? How to do that?
You yourself seems to be doing other way round. from larger screen to smaller one. not mobile first.
I always use this post as a reference, but what about the new 7-inch devices?
I also use this as a reference… but as spike said how about the new 7″ Nexus and the iPad Mini. What is the best with for this?
If anyone here needs a way to quickly generate valid custom media queries or need to look up other device specific presets, I wrote a free media query tool called Media Query Builder which you can find here -> Media Query Builder
You seem to be missing the ability to target both ipad and iphones:
This is information is helpful, but is there any way to write minimum media queries and work on all size devices.
Great post, very handy.
I just found something strange that might save people a lot of wasted time with media queries on Android devices:
I’m developing a mobile site and for some reason, a 540×960 phone running Android Jellybean would only pick up the 360-459px wide stylesheet. When I commented out this stylesheet from the header, the phone totally ignored the others, even the one that specifically targets it’s resolution.
<link rel="stylesheet" type="text/css" media="only screen and (min-device-width:540px) and (max-device-width:800px)" href="mob_vhigh.css"/>
<link rel="stylesheet" type="text/css" media="only screen and (min-device-width:480px) and (max-device-width:639px)" href="mob_high.css"/>
<link rel="stylesheet" type="text/css" media="only screen and (min-device-width:360px) and (max-device-width:479px)" href="mob_mid.css"/>
<link rel="stylesheet" type="text/css" media="only screen and (min-device-width:0px) and (max-device-width:359px)" href="mob_small.css" />
After a ton of research, it turns out that the default browser zoom setting does not actually zoom in the browser per se, but rather ignores it’s physical resolution, and pretends to have a smaller one. After some experiments:
‘Close’ zoom loads ‘small.css’,
‘Medium’ zoom loads ‘mid.css’ and
‘Far’ loads the proper stylesheet for the screen’s actual resolution.
It would appear there is no way to force the user’s browser to load the proper size stylesheet if their settings are different. Just thought I’d share this, it’s wasted a lot of my time, and finally I can move on :) Hope it helps
Hi again, here’s a few more things I’ve since learnt about the subtleties of Android devices:
There are several attributes that can be applied the the tag that alter the way they handle zoom. One of these is:
content="target-densitydpi=device-dpi"What this does is prevents the device from zooming on the page when it loads. This solved an irritating zoomed-on-load issue I suddenly had with the iPhone 4s. Users can still scale (if you’ve allowed it), it merely targets the device’s actual DPI rating, instead of relying on the often unpredictable automatic zoom. My meta tag now looks like this:
‹meta name="Viewport" content="target-densitydpi=device-dpi, width=device-width"/›This allows for the layout to adjust completely based on any display. The only hitch is the OS. For some reason, I can’t for the life of me get it to load on a HTC 7 Trophy windows phone without being zoomed in. Any thoughts? This is the only OS that seems to cause problems now. Even Blackberries work fine.
In another tweak, I’ve updated my media queries in an attempt to ignore most tablets while still catering for ludicrously high-DPI phones, such as the HTC Droid DNA (1920×1080, wow). To do this, I’ve simply added another media query with a new condition below the main one:
‹link rel="stylesheet" type="text/css" media="only screen and (min-device-width:540px) and (max-device-width:960px)" href="mob_vhigh.css"/›
‹link rel="stylesheet" type="text/css" media="only screen and (min-device-width:961px) and (min-webkit-device-pixel-ratio:2) and (min-device-pixel-ratio:2)" href="mob_vhigh.css"/›
This way, the phones load like normal, but any display larger than 960px must have a very high DPI display to access the styles, so hopefully this will be phones only. It’s not perfect, as I haven’t had a chance to try it on the Retina iPad (DPI anyone?), but it is designed to quickly provide a clear divide.
Rather than try and guess how high-res the next generation of phones will be, and have the site inevitably break on some new device in the future, I thought it made more sense to simply allow no limit and instead exclude the devices I didn’t want by using media queries. Having your mobile layout designed for small screens appear on a iPad makes you feel like a spoon-fed kid, as the buttons are now big enough to smash with your fists instead of point with a finger.
I found that some of the media queries here a bit haphazard. For example, the smartphone landscape query is
min-width:321pxmy desktop browser when larger then 321px wide will run the stylesheet intended for smartphones. Seen how iOS+Safari doesn’t consider itself a handheld device, using@media handheld and (...does not work, so I found that some were clashing with others were they shouldn’t have been. After many frustrating hours I went looking and found this question on Stack Overflow, Media query ipad vs iphone4The media queries are being used to specifically target a device width, pixel density(css ratio) and the orientation. Leaving very little for one device to use another stylesheet not intended for it. Although this maybe slightly more to write out, you get the consistency across all devices and I have never had any headaches using this method since.
I am pretty new with media queries. I have a website I am creating a mobile version for. When I check the site in iphone 4s portrait, 320px wouldn’t cover 100% the width. I need to push it to 375px to make it cover the width of portrait view. Any suggestions?
I am using: inside code
and html { -webkit-text-size-adjust: none; } inside css
the media querie I am using is:
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi)
Great Blog!! Thanks in advance
I have tried the same media queries for wordpress test site loaded on localhost. I used online responsive design tool http://ipadpeek.com/ . This tool fails to catch landscape and portrait mode queries. But while I tested localhosted wordpress site directly by changing screen resolution of my pc with windows 7 in portrait and landscape its works.
I know article is from a while back, but has anyone had an issue with “max-device-width” and having rendering issue.
My impression was that the “max-device-width” only rendered on phones, tablets, and mobile devices, not on desktop browsers.
This happens when the browser is set and the initial load of the css file with the @Media query loads.
So if I have
@media screen and (max-width:500px), screen and (max-device-width: 1280px){/* My mobile styles here */}
the desktop browser or screen resolution of the desktop is set at 1280px, it will actually render the 1280px styles mobile styles and not the desktop styles.
Did browser makers roll “max-device-width” into their browser builds? Any pointers or insite would be great.
Thanks.
Hi!
You have used proportion for iPhone 4, but what proportion will work for iPhone 5?
I have built my first truely responsive site, with a lot of help from CSS Tricks. I built it in SCSS with lots of media queries using breakpoints as outlined by Chris C. My question is- I have only user min-width and max-width (No min-device-width or max-device-width). Is this incorrect? As far as I can tell my site is working fine across all devices, though testing UserAgents is a pain. Most of my testing comes from resizing the browser in Chrome, IE and FF, and then using Chrome Dev Tools to test on Galaxy Nexus and also on my wife’s iPhone. Here is an example of my breakpoints.
@mixin breakpoint($point) {
@if $point == 1600 {
@else if $point == 1280 {
@else if $point == 1024 {
@else if $point == 800 {
@else if $point == 640 {
@else if $point == 480 {
@else if $point == 320 {
}
I am entirely new in this topic. But this seems very interesting. Could you drop an example what the @content does mean and/or where and what shall be stated in it?
Thanks a lot.
Martin- Sure! With the above code, I can use SCSS and mixins to easily make my site mobile responsive. Rather than creating a huge block of CSS at the bottom of my stylesheets and putting all my mobile CSS there, I add in my mobile CSS as I design my normal CSS. I then use a gem which moves everything to the end for production. Moving forward with the above breakpoints, my CSS might like like this:
.region-menu .navigation ul.inline, ul.links.inline {
@include breakpoint(640) { display: block; float: left; padding-left: 0; width: 160px; }}
As you can see, when SCSS compiles my style sheets, it will yield the following CSS:
@media (max-width: 640px) and (min-width: 320px) {
.region-menu .navigation ul.inline, ul.links.inline { display: block; float: left; padding-left: 0; width: 160px; }}
My above illustrations was simple… but when you use this for an entire site it becomes super powerful. When I need to go back and edit the screen width of one div only on mobile devices at 640px, I simply go to that class in my CSS and make changes- I don’t need to scroll through a page of mobile CSS to make changes.
As Chris Coyer said the above way(using mixins and SCSS) is the way your brain wants to connect the dots, so why organize things differently. Since I have started to do this, I can just code mobile as I go. I can put together a sites CSS and responsive all at once, rather than two separate items.
Hi Chris. Great article. I often refer back to it. I was just thinking that a lot has evolved and come about with mobiles and tablets since you wrote this article back in October 2010. Smaller tablets have been released and some mobile resolutions have gotten bigger. Has this blurred the lines somewhat with your advised media queries for different devices? Or would you say it’s largely unchanged?
I’ve been doing some testing on this and using a sony xperia s it seems to think that device width changes with orientation (720px then 1280px).
This would lend one to use something like this to target Android:
The problem with this is that it overlaps with the iPad and using
min-device-pixel-ratiois not enough to separate them when you get to the iPad 3 as they both have that as 2Does anyone know how to make android behave and yet not get my mobile site to appear on the ipad.
Reading this article http://stackoverflow.com/questions/12061127/css-media-queries-on-nexus-7-display-resolution-not-working-in-code lends one to think that actually Android is going to be a swine!
Thanks
Chris
IF NOT TOUCH = standard desktop
Because browser vendors don’t support the “NOT” selector from W3C I had to rely on JavaScript / jQuery to isolate the CSS that I didn’t want to show on iOS and Android device.
This article is awesome. I am gona use it.
This detailed browser compatibility table of the viewport (and some related items) from Peter-Paul Koch will help anyone interested in this subject in separating the facts from the utopias.
Thanks for sharing this great article…
but how can we use pixel-ratio mediaquery?
@media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}
I have a website Noobpost I’m trying to figure out how many media queries max-width sizes I should use, right now I have four, but it doesn’t seem to work well with landscape views. It also appears I need to position everything in a percentage to make it more fluid.
Any advice on what to do for large screens?
/* Large screens ———– /
@media only screen and (min-width : 1824px) {
/ Styles */
}
I’m particularly concerned about line length becoming unreadable.
You could always just keep the content centered (or left aligned) on the page at a normal width, say 960 or 1024px which would allow it to be readable. To fill up the extra space on the sides you could use a gradient, clever shadows, or some images to “fill” up the blank space. My site maxes out at 960px so it looks the same on 1600 wide monitors, and 1900 wide monitors and beyond. A word of advice- when designing for large screen, testing or actually designing for a large screen is really helpful- it is hard to visualize a large screen when working on a 13″ laptop.
Hi, I have some question, in ipad protrait mode…
you have specified the min-device-width:768px and max-device-width:1024px and orientation portrait and the same values you given for landscape too. Actually, when viewing with portrait in ipad the max-device-width will be 768px right? How the max-device-width be 1024px.
Is there a correct meta tag to go with this? I’m trying to target the iPhone4 and below screen widths (and not the iPad2 and above screen widths). But this isnt even working for iPhone4′s..
This is an informative article however i would approach responsive design much the same as we currently do for fully fluid layouts. My personal approach is semi fluid in nature with responsive design only to enhance the content.
If for example we apply a fixed width on the container (div as the most semantic selector) Then we can simply target that by simply applying a max width, minimum width and auto for backwards compatibility. Of course will work for semi modern browsers this is the best option in my opinion. We can scale this up or down based upon a percentage and change the width constrictions and there we have a complete fluid and responsive design.
Of course other elements needs to be specifically styled, rearranged or sometimes hidden (i would not recommend) by using media queries.
Hi,
I am working on a website to make it responsive.
1) Mobile phones including iphone series using a css file (includes portrait and landscape layouts with @media),
2) Tablets & ipads using a css file (includes portraits and landscape layouts with @media),
3) and desktop version css (which for large, medium and small break points usig @media spearated.)
Is there a way to accomplish the complete workaround some thing like this
I know I might be wrong here, as I am very new to this.. Please help me.
Thanks in advance
Chris