48

I want to change my web page CSS for web browsers running on cell phones, like the iPhone and Android. I've tried something like this in the CSS file:

@media handheld {
  body {
    color: red;
    }
  }

But it doesn't seem to have any effect, at least on the iPhone. How can I write my CSS to work differently on the iPhone etc, ideally without using javascript?

Colen
  • 13,428
  • 21
  • 78
  • 107
  • 1
    Most smartphones tell CSS they're "screen" ...which sorta makes sense considering their relatively large size and high resolution. A few parts of HTML/CSS standards are not very useful, and CSS "handheld" is one of them. Instead of CSS device classes, use CSS Media Queries to check actual dimensions (but even that is somewhat tricky because of the "logical viewport"). – Chuck Kollars Apr 08 '13 at 19:38
  • 6
    In my humble opinion, "handheld" is a perfectly valid axis for CSS -- sometimes you really do not care what exact size and DPI users screen is -- what you *do* care about though is the kind of interaction they do. A handheld implies that the device, screen included, is held in a hand, thus giving you some pretty accurate assumptions, for instance that the screen size is most probably not bigger than 9" and that it is intended to be used at arms length or so. These things are important when designing for an audience. We have to stop all the pixel counting and get on to a more "general" plateau. – Armen Michaeli Apr 17 '16 at 10:54
  • 1
    When CSS first came out with "handheld" and the first small screen devices appeared on the scene (2005-2007) the first primitive browsers DID support "handheld". Its was GREAT! I built my first small screen website around 2005 and it displayed perfectly. What we had long ago was far superior to HTML5 today. Its a crappy standard build by lazy developers that refuse to follow standards. Now we hack our way through device pixels and media queries which will complicate what used to be a very simple CSS standard. – Stokely Jan 15 '21 at 11:49

5 Answers5

36

You can use @media queries:

<link rel="stylesheet" href="path/to/iphone.css" media="only screen and (max-device-width:480px)"/>

This particular version will target the iPhone (and any other device with a screen of max-device-width of 480px.

Apple, for the iPhone, though this is from memory so I can't be entirely sure of its accuracy, chose to disregard the use of handheld or mobile stylesheets, since it, and other iOS devices, were capable of rendering css more or less on a par with desktop browsers, via Safari. For other devices I'm unsure, exactly, how faithful they are, though the A List Apart article (linked-to above) gives a brief run-through of some.


Edited in response to comment, from @Colen:

Hmm, it looks like a lot of new mobile devices have higher resolutions (e.g. droid X is 854x480). Is there any way to detect those? I don't think those are being handled with this query.

I'm unable to say for certain, since I've no access to those devices, however another A List Apart Article: Responsive Web Design notes that:

Thankfully, the W3C created media queries as part of the CSS3 specification, improving upon the promise of media types. A media query allows us to target not only certain device classes, but to actually inspect the physical characteristics of the device rendering our work. For example, following the recent rise of mobile WebKit, media queries became a popular client-side technique for delivering a tailored style sheet to the iPhone, Android phones, and their ilk.

So I presume that they, Android devices, must be target-able by @media-queries, but, as noted, I'm unable to say with any certainty.

To target device-resolution, there is an example of:

<link rel="stylesheet" type="text/css" media="screen and (max-device-width: 480px) and (resolution: 163dpi)" href="shetland.css" />

Further reading: W3 Candidate Recommendation for media queries.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • It should be "max-device-width" (you have a space in there), but otherwise this was great. Thanks! – Colen Oct 08 '10 at 19:03
  • 2
    Hmm, it looks like a lot of new mobile devices have higher resolutions (e.g. droid X is 854x480). Is there any way to detect those? I don't think those are being handled with this query. – Colen Oct 08 '10 at 19:18
  • 1
    Another nice reading: http://www.rkblog.rk.edu.pl/w/p/optimizing-websites-iphone-and-android/ – Andrea Lazzarotto Dec 16 '11 at 08:09
  • For Android-specific queries, see here: http://developer.android.com/guide/webapps/targeting.html Android does a lot of scaling and settings some of these can help you get things pixel-perfect on Android devices. – Brandon Jan 23 '13 at 18:04
  • For what it's worth, Google currently "strongly discourages" using the max-device-width query and recommends using max-width and min-width instead: https://developers.google.com/web/fundamentals/layouts/rwd-fundamentals/use-media-queries#a-note-on-min-device-width – Matt Dec 01 '14 at 07:00
  • This uncertainity makes whole stylesheet messy... Why they think their browser should not have `handheld` option if top device is handheld? – vintprox Feb 28 '19 at 07:29
  • Its impossible to know what pixels to design for as each phone stuffs multiple CSS pixels into 1 Device pixel now. 1 CSS pixel does not equal 1 Phone device pixel so there's no standard. That's why you should NEVER design websites in pixels. Design for font size and let the device calculate the width it needs to display your website in its own pixel translation and dimensions. – Stokely Jan 15 '21 at 11:54
30

From this site there are a few other media queries that are useful in targeting iPhones/Android Phones:

    // target mobile devices
@media only screen and (max-device-width: 480px) {
    body { max-width: 100%; }
}

// recent Webkit-specific media query to target the iPhone 4's high-resolution Retina display
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
    // CSS goes here
}

// should technically achieve a similar result to the above query,
// targeting based on screen resolution (the iPhone 4 has 326 ppi/dpi)
@media only screen and (min-resolution: 300dpi) {
    // CSS goes here
}

I was able to successfully use the max-device-width media query to successfully target Android phones, although I had to adjust the width up to 800px rather than the 480 listed. For iPhone 4, the -webkit-min-device-pixel-ratio worked to target the iPhone4 (max-device-width: 480px did not, I presume that will target the iPhone3 but didn't have one handy to test.)

I can see this getting quite messy, but if you have to support a multitude of devices and have custom CSS for each of them, as long as they support media queries it appears as it is possible to do what you have to to tweak each platform. And yes, I would code to standards first, so that as much CSS is resuable, but many times we are talking about presenting alternate layouts these days sized appropriately for the devices being used.

LocalPCGuy
  • 6,006
  • 2
  • 32
  • 28
22

@media handheld refers only to those ancient tiny proto-html cellphones from years past which couldn't even really display web pages. The ePUB, MOBI, Tablet, community of vendors all said emphatically "H*ck no, we are not @media handheld devices!" because they were correctly worried that this would land them forever in a no-man's land subservient to "real" web pages.

With today's small devices with very high resolution displays we still don't have a good way to tell HTML how to display things "correctly" on large displays with relatively low resolution vs. small displays with very high resolution. And as a certified old fart my eyes would like to remind you that no, the answer is not just to make everything including fonts 2X smaller.

Sirko
  • 72,589
  • 19
  • 149
  • 183
Jim Adcock
  • 221
  • 2
  • 2
  • Is it worth it to look at media queries on DPI/DPCM values, screen sizes in cm/inches, and aspect ratios, instead? – Gert Sønderby Apr 19 '13 at 09:47
  • That was not future-proof design. We still need such option in CSS because not all handheld devices have resolution lesser than `ANOTHER_OPINIONATED_CONSTANT_MAX_RESOLUTION_VALUE_FROM_ANOTHER_ARTICLE`... I'm sorry for caps) – vintprox Feb 28 '19 at 07:32
8

No, neither iPhone or Android browsers supports CSS @media handheld.

Jonas Äppelgran
  • 2,617
  • 26
  • 30
-2

Look at using the media query 'hover'.

Put this in your SCSS file:

// At this point the CSS would target screens above 990px - but only
// if they support hover (i.e. laptops, PC's etc).
$point-at-which-use-large-screens: (min-width 990px) (hover hover);

.some-class-you-want-to-target {

  // Some CSS to only apply to larger screens with mouse available.
  @include breakpoint($point-at-which-use-large-screens) {
    color: red;
  }
}

After running grunt etc on the SCSS this will produce CSS looking like:

@media (min-width: 991px) and (hover: hover) {
  color:red;
}
bailey86
  • 67
  • 1
  • 7
  • 1
    Sorry but you did not even try to answer the question. And a breakpoint mixin makes no sense if used this way, take a look at how [include-media](https://include-media.com/) works. And the hover query is not supported on older browsers. – Fabian von Ellerts Dec 05 '19 at 13:28