Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use the Media attribute for handhelds of the HTML Link tag?

Tags:

html

mobile

I currently have the following in my HTML document.

<link REL=StyleSheet HREF="/stylesheets/home.css" TYPE="text/css" MEDIA=screen>
<link REL=StyleSheet HREF="/stylesheets/homedark.css" TYPE="text/css" MEDIA=handheld>

However, nothing changes on a handheld device (my Android phone). Is there something I'm doing wrong, or is it just not this simple?

like image 563
muttley91 Avatar asked Aug 23 '11 19:08

muttley91


1 Answers

You will need to use more advanced media queries, such as those that target screen size

So something more like

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

Inside your stylesheet, it would be something like this

@media only screen and (max-device-width: 480px) {
    //Your styles here
}

http://www.w3.org/TR/css3-mediaqueries/

Simple JS device detection might be helpful:

if( navigator.userAgent.match(/Android/i) || 
    navigator.userAgent.match(/webOS/i) || 
    navigator.userAgent.match(/iPhone/i) || 
    navigator.userAgent.match(/iPod/i)){
     // link to stylesheet
}

An even better thing to do is use some sort of combo... media queries & device / feature detection.

This is a good overview: http://www.csskarma.com/presentations/cssla/slides/mobile_media_touch.pdf (WARNING: pdf file)

like image 127
Jason Gennaro Avatar answered Oct 19 '22 15:10

Jason Gennaro