How to change CSS based on mobile device?

后端 未结 5 2158
一整个雨季
一整个雨季 2021-02-07 05:30

In the following example, I want to display only one div, depending on the device.

I\'m familiar with how to use @media to override a CSS cla

5条回答
  •  难免孤独
    2021-02-07 05:42

    Hm i think its easier to create 2 or more css files and load them via javascript based on the device. Something like that inside the onload function:

    var cssPath = "standard.css";
    if (navigator.platform == "iPad")
        cssPath = "iPad.css";
    
    var fileref = document.createElement("link");
    
    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", cssPath);
    
    document.getElementsByTagName("head")[0].appendChild(fileref);
    

    Shorter and more readable css files easy to exchange.

提交回复
热议问题