Media Queries: How to target desktop, tablet, and mobile?

前端 未结 16 1825
清酒与你
清酒与你 2020-11-21 04:53

I have been doing some research on media queries and I still don\'t quite understand how to target devices of certain sizes.

I want to be able to target desktop, ta

相关标签:
16条回答
  • 2020-11-21 05:24
    @media (max-width: 767px)   {
    
          .container{width:100%} *{color:green;}-Mobile
    
        }
    
    
        @media (min-width: 768px)  {
    
         .container{width:100%} *{color:pink  } -Desktop
    
        }
        @media (min-width: 768px) and (orientation:portrait)  {
    
           .container{width:100%} *{color:yellow  } -Mobile
    
        }
        @media (min-width: 1024px)  {
    
           .container{width:100%} *{color:pink  } -Desktop
    
        }
        @media (min-width: 1200px)  {
    
        .container{width:1180px} *{color:pink   } -Desktop
    
        }
    
    0 讨论(0)
  • 2020-11-21 05:25

    Nowadays the most common thing is to see retina-screen devices, in other words: devices with high resolutions and a very high pixel density (but usually smaller than 6 inches physical size). That's why you will need retina display specialized media-queries on your CSS. This is the most complete example I could find:

    @media only screen and (min-width: 320px) {
    
      /* Small screen, non-retina */
    
    }
    
    @media
    only screen and (-webkit-min-device-pixel-ratio: 2)      and (min-width: 320px),
    only screen and (   min--moz-device-pixel-ratio: 2)      and (min-width: 320px),
    only screen and (     -o-min-device-pixel-ratio: 2/1)    and (min-width: 320px),
    only screen and (        min-device-pixel-ratio: 2)      and (min-width: 320px),
    only screen and (                min-resolution: 192dpi) and (min-width: 320px),
    only screen and (                min-resolution: 2dppx)  and (min-width: 320px) { 
    
      /* Small screen, retina, stuff to override above media query */
    
    }
    
    @media only screen and (min-width: 700px) {
    
      /* Medium screen, non-retina */
    
    }
    
    @media
    only screen and (-webkit-min-device-pixel-ratio: 2)      and (min-width: 700px),
    only screen and (   min--moz-device-pixel-ratio: 2)      and (min-width: 700px),
    only screen and (     -o-min-device-pixel-ratio: 2/1)    and (min-width: 700px),
    only screen and (        min-device-pixel-ratio: 2)      and (min-width: 700px),
    only screen and (                min-resolution: 192dpi) and (min-width: 700px),
    only screen and (                min-resolution: 2dppx)  and (min-width: 700px) { 
    
      /* Medium screen, retina, stuff to override above media query */
    
    }
    
    @media only screen and (min-width: 1300px) {
    
      /* Large screen, non-retina */
    
    }
    
    @media
    only screen and (-webkit-min-device-pixel-ratio: 2)      and (min-width: 1300px),
    only screen and (   min--moz-device-pixel-ratio: 2)      and (min-width: 1300px),
    only screen and (     -o-min-device-pixel-ratio: 2/1)    and (min-width: 1300px),
    only screen and (        min-device-pixel-ratio: 2)      and (min-width: 1300px),
    only screen and (                min-resolution: 192dpi) and (min-width: 1300px),
    only screen and (                min-resolution: 2dppx)  and (min-width: 1300px) { 
    
      /* Large screen, retina, stuff to override above media query */
    
    }
    

    Source: CSS-Tricks Website

    0 讨论(0)
  • 2020-11-21 05:27

    Best solution for me, detecting if a device is mobile or not:

    @media (pointer:none), (pointer:coarse) {
    }
    
    0 讨论(0)
  • 2020-11-21 05:29

    Don't target specific devices or sizes!

    The general wisdom is not to target specific devices or sizes, but to reframe the term 'breakpoint':

    • develop the site for mobile first using percentages or ems, not pixels,
    • then try it in a larger viewport and note where it begins to fail,
    • redesign the layout and add a CSS media query just to handle the broken parts,
    • repeat the process until you reach the next breakpoint.

    You can use responsivepx.com to find the 'natural' breakpoints, as in 'breakpoints are dead' by Marc Drummond.

    Use natural breakpoints

    The 'breakpoints' then become the actual point at which your mobile design begins to 'break' i.e. cease to be usable or visually pleasing. Once you have a good working mobile site, without media queries, you can stop being concerned about specific sizes and simply add media queries that handle successively larger viewports.

    If you're not trying to pin design to exact screen size, this approach works by removing the need to target specific devices. The integrity of the design itself at each breakpoint ensures that it will hold up at any size. In other words, if a menu/content section/whatever stops being usable at a certain size, design a breakpoint for that size, not for a specific device size.

    See Lyza Gardner's post on behavioral breakpoints, and also Zeldman's post about Ethan Marcotte and how responsive web design evolved from the initial idea.

    Use semantic markup

    Further, the simpler and more semantic the DOM structure with nav, header, main, section, footer etc. (avoiding abominations like div class="header" with nested inner div tags) the easier it will be to engineer responsiveness, especially avoiding floats by using CSS Grid Layout (Sarah Drasner's grid generator is a great tool for this) and flexbox for arranging and re-ordering according to your RWD design plan.

    0 讨论(0)
  • 2020-11-21 05:34

    The best breakpoints recommended by Twitter Bootstrap

    /* Custom, iPhone Retina */ 
        @media only screen and (min-width : 320px) {
    
        }
    
        /* Extra Small Devices, Phones */ 
        @media only screen and (min-width : 480px) {
    
        }
    
        /* Small Devices, Tablets */
        @media only screen and (min-width : 768px) {
    
        }
    
        /* Medium Devices, Desktops */
        @media only screen and (min-width : 992px) {
    
        }
    
        /* Large Devices, Wide Screens */
        @media only screen and (min-width : 1200px) {
    
        }
    
    0 讨论(0)
  • 2020-11-21 05:36

    If you want to target a device then just write min-device-width. For example:

    For iPhone

    @media only screen and (min-device-width: 480px){}
    

    For tablets

    @media only screen and (min-device-width: 768px){}
    

    Here are some good articles:

    • How to fit your website for the Apple iPad
    • CSS3 Media Queries
    0 讨论(0)
提交回复
热议问题