@media only screen and (device-width: 768px) {
/* For general iPad layouts */
}
@media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait) {
/* For portrait layouts only */
}
@media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) {
/* For landscape layouts only */
}
iPad-Specific CSS
Chris Coyier
on
No Biggie. But you’re missing a closing slash on your second comment line.
Thanks!
I’d love to see a screencast on designing for an iPad
i’m sure it’s like the iPhone one you made but still…
This CSS will also be used by *all small screens* – this might be undesirable since often the CSS issues (such as position fixed) may not apply to netbooks for instance where the browser and os may be different to an ipad….
I’m wondering, why would the device-width = 768? Why doesn’t it equal 1024?
1024px is the longest side, 768px is the shortest side, therefore you want to make sure the CSS will work on the iPad’s shortest side too. It is iPad specific.
The min width 481px to max width 1024px is so the CSS works in the set orientation on any device between the pixel widths iPhone and iPad in this case.
Hope that helps.
It depends on the orientation of the device (Ipad), whether it is a vertical or a horizontal one. Therefore, Vertical orientation would have a width of 768 and a height of 1024, as for horizontal the width would be 1024 and height 768.
@media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait) {
/* For portrait layouts only */
}
is going to give you some problems with newer Android devices. Found that in order for Portrait styles for iPad not being getting picked up by my Galaxy S2 device was to use:
@media only screen and (device-width: 768px) {
}
Instead.. I know its not really a solution, but it needs to be noted.
This code doesn’t seem to do anything for me.. what am I doing wrong?
wow :D
@designer: Try doing this: #page @media only screen and …. { .. }
Is there anyway to test the css if you don’t have an iPad? Something like IE tester for Mobile? Tried http://ipadpeek.com/ but it doesn’t apply the styles.
try browserStack http://www.browserstack.com/user/dashboard
@Tim If you’re using a Mac get yourself: Xcode which has IOS Simulator (iPhone and iPad). Here’s a quick movie to help you get it. http://www.youtube.com/watch?v=oobXPWHx3ZI
Then head over to http://moduscreate.com/enable-remote-web-inspector-in-ios-6/ to enable developer mode in Safari.
Combine the two and works like element inspector in most browsers, just for iPhone and iPad :)
Haven’t tested this on Windows Safari – anyone else know?
Thanks for the tip on the snippet code, I did not realize tha it was this easy. :-)
For those of you who are trying to get the above code to work on ipadpeek.com, there are a few things you need to do first.
1. insert the following meta tag at the top of the html page just below the opening tag.
2. When you are ready to view your site, you will have to change the monitor resolution to 1024×768 and refresh the ipadpeek.com web browser page.
Sorry for the double post, but the tag code did not show in the above post.
here is the meta tag I was refering to.
<meta name="viewport" content="width=device-width" />
@media only screen and (device-width: 768px) and (device-height: 1024px) and (-webkit-min-device-pixel-ratio: 1)
{
/* Your iPad specific rules here */
}
@media only screen and (device-width: 768px)
{
#background img {
display: none;
}
/* Your iPad specific rules here */
}
Wow, quick and easy fix! Thanks for sharing this technique, it saved me a huge headache.
Thanks!
What about ipad 3, that has a higher definition than 768px
Tested it on the ipad 3, works fine
(this one worked:
@media only screen and (device-width: 768px) and (device-height: 1024px) and (-webkit-min-device-pixel-ratio: 1)
{
#background img {
display: none;
}
}
)
@SirKay
you so cool! that code u’ve give is work 100%. thanks.
I have a related question. The answer might involve using the media query described in this post (or something else). If your page content is less than 1024px in height, is there a technique to make the footer adjust so that the footer background (color or image-repeat) goes all the way down to the bottom of the page when it is viewed in portrait on the ipad? I assume this is a standard problem but I haven’t seen any solutions for it. I’m thinking the footer is the most obvious div to adjust, but there could be some other technique or div that “saves” the design in this use case. Any suggestions would be greatly appreciated.
Should I use
plus
@media only screen and (device-width: 768px)
{
#background img {
display: none;
}
/* Your iPad specific rules here */
}
or could I do without this code?
for some reason this code meta name=”viewport” content=”width=device-width” didn’t appear in the blank areas above
Wow! been searching for days how to fix this issue, now everything works on ipad without messing up the site in other browsers.
Realy thank you for this post!
Has anyone tested these code tips on the new larger Samsung phones?
Thanks guys, for those want to use it inside JavaScript
if (window.matchMedia(‘only screen and (device-width: 768px)’).matches)
{
// ipad… maybe run some small-screen related dom scripting?
}
if (window.matchMedia(‘only screen and (max-width: 480px)’).matches)
{
// smartphone/iphone… maybe run some small-screen related dom scripting?
}
Wow, I never new this. This is awesome stuff. Thanks for sharing it ;)
Perfect. Saved me a lot of trouble in a project short before going live!
Safari has been discontinued on Windows and the remote inspection stuff is only in version 6+ (which is not available for Windows).
Any Idea how to design a div.scroll to show the scroll bar on the ipad. Currently this CSS trick does work with a two figure scroll , which is fine with me. I really want the scroll bar to be visible on the Ipad so the user knows that their is content to be scrolled through?? Any help would be greatly Awesome!
Strangely this didnt work for me, at all.
Kept digging and found a way that did, hop this helps someone:
@media screen and (max-width:480px) {
/* Making the headings red for smartphone users */
h1 {
color: red;
}
}
@media screen and (min-width:481px) and (max-width:1280px) {
/* Making the headings Black targeting PC Users */
h1 {
color:green;
}
}
@media screen and (min-width:1281px) {
/* Making the headings red for smartphone users */
h1 {
color: black;
}
}
thanks for quick and easy way :)
sorted the issue I was having.
why would the device-width = 768? Why doesn’t it equal 1024?
Sorry guys, but this makes no sense!
There’s a lot of devices that’s make use of 480px or 1024px, so this css affects all, not only Ipad devices.
I totally agree with you.
What do you think about php device detection and adding isTablet / isMobile / isIpad … in body class and apply css style like this :
// I use nested css rules with compass
body.isIpad{<br/>
// Your iPad specific rules here <br/>
@media only screen and (orientation : landscape) {<br/>
// Your landscape iPad specific rules here<br/>
}
@media only screen and (orientation : portait) {<br/>
//Your portait iPad specific rules here<br/>
}
<br/>}
Erasmussen, does this work out of the box? AND with all iPad devices? Im sure u can add more specific detail to your code. Like the difference in addressing an ipad 2 of ipad retina.
If you know how I for one would appreciate a fully working code so I can learn from it, because I am having a hard time figuring the responsiveness out. Thank you.
Ramses,
For device detection I am using this php class : http://mobiledetect.net/ really simple and complete. You can detect mobile, tablet device and the type of device : Blackberry, Samsung, Asus or …iPad.
I let you see the documentation. My html code looks like :
Then in css you can use the previously code I sent and add the css-tricks gived here : https://css-tricks.com/snippets/css/retina-display-media-query/ :
This is maybe not the best way to do… but it works good for me.
When “retina” displays came out, I decided to quit fitzing around with pixel counts and trying device-aspect-ratio instead. I’ve had good luck with 3/4 (or 4/3, depending) for iPads, and…so far…40/71 for my iPhone (¡!)
Hello,
as we are defining this for media scree,
Can we define page layout for font size also.
Like my i am looking my page desine output in ipad, but when i increase or decrease the font size- the layout does not show the flow of content in proper way,
Is there any solution, that we can define for font size;
like-
@ media screen if font size: 10 – 50%
page will look like..
Hello everyone,
Im not much into coding, but I am trying to get rid of many css included in my html when viewed by a mobile user agent, my website run under wordpress and I have installed the wptouch plugin for my mobile visitors, this plugin deactivates the effect of most of the other plugins installed in the theme (I don’t mind it). The problem is that I can see the css of all those plugins still loaded (with no effect on design) which slow down the pageloading on a mobile device. Is there a way to avoid they load if the user agent is a mobile device?
PS. support from wptouch team sucks, unless you pay them for pro version
I think that with iPad pro around we might need something more cunning.
No luck. I haven’t found anything. I’m not sure if CSS-Tricks found any solution for iPad Pro’s landscape orientation….
Hi,
I have a working site and its great on my ipad3 and iphone6.
I also have a ipad 1 but no matter what code i edit or use etc it will always only show the same as the iphone6
If i turn it to portrait the screen doesnt even rotate.
My main goal is to have the ipad1 landscape view on the screen showing the same as the ipad 3 NOT like an iphone.
Any help would be great & welcomed.
Why is min-width being set to 481 if the width of the Ipad is 768 in portrait and 1024 in landscape? Does this cover other tablets or is there something very obvious that I’m missing?
I had a problem with iPad on landscape which causes images to zoom, works relatively ok in portrait. I have used a derivative of @SirKay ‘s CSS snippet (thank you for the starting point) as follows;
@media only screen and (device-width: 768px) and (device-height: 1024px) and (orientation:landscape) and (-webkit-min-device-pixel-ratio: 1)
{
#”page or section identifier” {
background:url(“image url”) no-repeat top center; background-size:cover;
}
}
The issue is that iOS has a bug in landscape that fights with the “fixed” or “absolute” image position statement, the bug is not there in portrait although it does introduce an image “repeat vertically” statement which is to stop additionally white space due to scaling, I am experimenting with allowing additional upsizing in the scaling to see if it defeats this.
The only issue with removing the position statement is that it introduces additional white space above the image during scroll, I am exploring code now to overcome this which at this point is a scrolling issue. The alternative is to use the “display;none” parameter which is sometimes preferable on smaller screens to avoid too much user scrolling, at the expense of aesthetics of course.
As far as the minimum width “768px” questions, iOS always uses this as the device width and never as “1024px” but relies on an “Orientation” statement to asses what it should be doing with CSS.
I found that styling within these media queries also apply to iPhone X
Width vs device-width? Aren’t they the same?