Search code examples
iphonecssiosmedia-queries

How can you hide CSS from the iPhone, but not other browsers?


I know how to hide CSS from all browsers except the iPhone: see How do I apply a stylesheet just to the iPhone (and not IE), without browser sniffing?

But: how do I hide CSS from the iPhone, but not other browsers?


Solution

  • I actually ended up going with a slightly different, and very ridiculous, solution that uses media queries and getComputedStyle to redirect to a mobile site if we’re on an iPhone-like device.

    <style media="only screen and (max-device-width: 480px)">html{border-top-style:dashed;}</style>
    
    <script>
    if(window.location.search.indexOf("?m=t")==-1 && window.getComputedStyle) {
        var mobile = false;
    
        if(window.getComputedStyle(document.getElementsByTagName("html")[0],null).getPropertyValue("border-top-style")=="dashed") {
            var mobile = true;
        }
    
        if(mobile) {
            window.location.replace(window.location+"?m=t");
        }
    }
    </script>
    

    I’m sure I got the getComputedStyle idea on Stack Overflow, but I can’t remember where.