How to code CSS media queries targeting ALL mobile devices and tablets?

后端 未结 3 787
自闭症患者
自闭症患者 2020-12-02 10:09
@media only screen and (max-device-width : 640px) {
/* Styles */
}

@media only screen and (max-device-width: 768px) {
/* Styles */
}

This is what

3条回答
  •  情歌与酒
    2020-12-02 10:40

    Instead of using specific widths initially, or messing around with orientations, or any other nonsense, I suggest using the following media tag...

    @media only screen and (min-resolution: 117dpi) and (max-resolution: 119dpi), only screen and (min-resolution: 131dpi) and (max-resolution: 133dpi), only screen and (min-resolution: 145dpi) and (max-resolution: 154dpi), only screen and (min-resolution: 162dpi) and (max-resolution: 164dpi), only screen and (min-resolution: 169dpi) {
      /* Your touch-specific css goes here */
    }
    @media only screen and (min-resolution: 165dpi) and (max-resolution: 168dpi), only screen and (min-resolution: 155dpi) and (max-resolution: 160dpi), only screen and (min-resolution: 134dpi) and (max-resolution: 144dpi), only screen and (min-resolution: 120dpi) and (max-resolution: 130dpi), only screen and (max-resolution: 116dpi) {
      /* Your click-specific css goes here */
    }
    

    And what do you use these tags for? To set stuff for hover & click vs touch events.

    Touch devices, other than a few devices in grey areas above addressed, have a very different resolution than desktop devices. Do -not- this to set design elements, but navigation elements. Some pudents may cry that some insanity with max-width may be better, but there's so many HD phones it's ridiculous that device-max-width quickly becomes useless.

    However, you should use width media width queries. However, don't bother with max-device-width, just max-width & min-width. Let the above tags address your touch vs not touch users, let min-width vs max-width address based on window size and adjust site visuals.

    Further, using orientation to determine if it's mobile or not is just silly, as even monitors can be placed in various orientations (a common setup I've seen for 3-monitors is a portrait center monitor and landscape side monitors.)

    For width views, focus on making your site clean on various widths, ignoring what kind of device is actually accessing it, just make sure your screen displays cleanly at various sizes. That's good design that applies to both desktop and mobile. If they have your site in a small window at the upper left corner of their screen for reference (or quick distraction) while using the majority of their screen real estate elsewhere, and it should be for them, as well as mobile users, that your smaller widths are built for. Trying anything else is quickly going down a very painful and self-defeating path for web development. So for those smaller widths, you can set your widths to whatever you want, but I'll include a few I personally like.

    So altogether, you should have something like this...

    @media only screen and (min-resolution: 117dpi) and (max-resolution: 119dpi), only screen and (min-resolution: 131dpi) and (max-resolution: 133dpi), only screen and (min-resolution: 145dpi) and (max-resolution: 154dpi), only screen and (min-resolution: 162dpi) and (max-resolution: 164dpi), only screen and (min-resolution: 169dpi) {
        #touch_logo {
           display: inherit;
        }
        #mouse_logo {
           display: none;
        }
      /* Your touch-specific css goes here */
    }
    @media only screen and (min-resolution: 165dpi) and (max-resolution: 168dpi), only screen and (min-resolution: 155dpi) and (max-resolution: 160dpi), only screen and (min-resolution: 134dpi) and (max-resolution: 144dpi), only screen and (min-resolution: 120dpi) and (max-resolution: 130dpi), only screen and (max-resolution: 116dpi) {
        #touch_logo {
           display: none;
        }
        #mouse_logo {
           display: inherit;
        }
      /* Your click-specific css goes here */
    }
    @media (min-width: 541px){
      /* Big view stuff goes here. */
    }
    @media (max-width: 540px) and (min-width: 400px){
      /* Smaller view stuff goes here. */
    }
    @media (max-width: 399px){
      /* Stuff for really small views goes here. */
    }
    

    Although, don't forget to include the following in your page's head:

    
    

    It may still break on some cases, but this should be more concise and more complete than many other solutions.

提交回复
热议问题