Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
Web Dev + WordPress + Security

Target iPhone and iPad with CSS3 Media Queries

When designing a website, it’s always a good idea to test on as many different platforms, devices, and browsers as possible. These days, pimping your websites for the iPhone and iPad is an important step in the design process. Especially on the iPad, sites tend to look about 20% cooler than on desktop browsers, so you definitely want to take the time to fine-tune the details. And when dealing with iDevices, it’s often necessary to deliver some custom CSS to make everything just right.

Want to apply CSS styles to the iPad and iPhone? Here is the plug-n-play, copy-&-paste code that actually works.

As you may have heard, I’ve been super-busy behind the scenes building an Angry-Birds fan site, of all things. The site is looking great so far, but needed some tweaking to appear slick on the iPad and iPhone. After testing a number of different solutions, here is what I found that actually works..

Target iPad & iPhone with separate CSS files

If you want to apply styles to the iPad and iPhone using external stylesheets, put this code in your <head>:

<!--[if !IE]>-->
<link type="text/css" rel="stylesheet" media="only screen and (max-device-width: 480px)" href="http://example.com/iPhone.css" />
<link type="text/css" rel="stylesheet" media="only screen and (min-device-width: 768px) and (max-device-width: 1024px)" href="http://example.com/iPad.css" />
<!--<![endif]-->

For this to work, you need to edit the path to each of the stylesheets. Then add some styles exclusively for the iPhone (first <link>) or the iPad (second <link>).

How does it work? Well, we first exclude IE by wrapping the links in a simple conditional comment. Then we’re using some CSS3 media queries to target each device specifically. Safari on the iPhone responds to a max-device-width of 480px, and Safari on the iPad seems to respond best when both min-device-width and max-device-width are used in the query. Together, these media queries apply styles in either portrait or landscape orientation.

Target iPad & iPhone from within your existing stylesheet

Using external stylesheets to target iStuff works great, but the extra HTTP requests can reduce performance. To avoid this, we can apply iPad/iPhone-specific CSS using our existing stylesheet. You know, the same one that we’re using for Firefox, Opera, and other desktop browsers. After much experimentation and testing, here is what I found that works:

/* iPad [portrait + landscape] */
@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
	.selector-01 { margin: 10px; }
	.selector-02 { margin: 10px; }
	.selector-03 { margin: 10px; }
}

/* iPhone [portrait + landscape] */
@media only screen and (max-device-width: 480px) {
	.selector-01 { margin: 10px; }
	.selector-02 { margin: 10px; }
	.selector-03 { margin: 10px; }
}

Just place that code into your stylesheet and change the declaration blocks to something more useful. Unfortunately, combining these two media queries won’t work:

/* == iPad/iPhone [portrait + landscape] == */
@media only screen and (min-device-width: 768px) and (max-device-width: 1024px), 
@media only screen and (max-device-width: 480px) {
	.selector-01 { margin: 10px; }
	.selector-02 { margin: 10px; }
	.selector-03 { margin: 10px; }
}

As always, comments, questions, and suggestions are welcome! :)

About the Author
Jeff Starr = Creative thinker. Passionate about free and open Web.
GA Pro: Add Google Analytics to WordPress like a pro.

37 responses to “Target iPhone and iPad with CSS3 Media Queries”

  1. Andrew W. 2010/10/27 5:55 am

    These are fantastic articles but I can only use them to get myself into trouble. I’m finding it difficult to locate a CSS guru (no luck on elance) who can remake my site for mobile browsers. Any suggestions on where else I should look?
    Andrew

  2. Jeff Starr 2010/10/27 6:02 pm

    @mtness: I haven’t tested yet on anything 960×640, but the first rule should target those devices as well. If anyone knows more about this, please share with us. Thanks!

    @Avi D: Yes these styles work fine when included directly in your existing stylesheet. Just replace the example declarations (the margin stuff) with whatever CSS you would like to use.

    @Andrew W.: If you’re running WordPress, there are several great plugins that mobilize your site with a few clicks. Specifically, look at WPtouch – an excellent plugin and highly recommended.

    @Vladimir Carrer: Nice! Thanks for sharing :)

  3. Wow Jeff Starr, you really are on the bleeding edge of Web Design.

  4. Jeff Starr 2010/10/27 9:50 pm

    Not really Craig, but I do enjoy sharing what I’ve learned with others. I have to admit though, “the bleeding edge of Web Design” does sound wicked smart.

    Maybe someday, huh?

  5. Hi Jeff, Thanks for sharing, One question since I have neither those devices so I can’t know what is not being displayed right. But I’m thinking for example this could come handy for example I have pictures bigger than the ipad and iphone screen width, does this mean that I can provide diferent image files of different widths to suit each device in the stylesheets? or are you just talking about tweaking layout and code differences to the computer browser version?

    Sorry for my lousy english.

  6. Hi Danilo,

    The @media queries target matching devices (such as iPhone/iPad), but they don’t apply any styles of their own. So to prevent images from breaking layout, we need to apply that property specifically:

    @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
           img { max-width: 768px; }
           }

    But as you say, this only resizes the images to fit the screen, it won’t change the actual file size.

    I hope this helps!

  7. that’s great
    but, is there by any chance attribute from can detect user agent?

    such as
    or something like that…

    i think it will gonna be easier for developing CSS :-)

  8. oops, sorry i mean

    can attribute from style detect user agent?

    such as style user-agent="iPad"

  9. Jeff Starr 2010/10/29 8:38 am

    I wish! But no, CSS can’t do this (yet). You can use PHP or JavaScript to to do the job.

  10. Great article.

    I’ve made a test rig for CSS3 media queries that might interest you or anyone that wants to experiment with them. Source is on github as well.

    http://swervo.tumblr.com/post/1448434311/testrigformediaqueries

    I haven’t included iPad as it starts getting a bit big but it could be extended easily enough.

  11. This sort of thing kind of annoys me as it implies all these ‘cool’ CSS3 goodies are only applicable to Apple products.

    You can use @media queries to target all mobile devices (even if they don’t understand them), no matter how old or unstylish they are, which is surely more useful than catering solely for iPhones and iPads.

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
Wizard’s SQL for WordPress: Over 300+ recipes! Check the Demo »
Thoughts
I disabled AI in Google search results. It was making me lazy.
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources. Also: “The Web” is not a valid answer.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.