Media query for devices supporting hover

后端 未结 6 1297
醉梦人生
醉梦人生 2020-12-08 04:24

I\'d like to provide separate behaviour for browsers supporting hover (e.g. desktop browsers) and ones which don\'t (e.g. touchscreen devices). Specifically I want to declar

6条回答
  •  爱一瞬间的悲伤
    2020-12-08 04:45

    To support Firefox (see https://bugzilla.mozilla.org/show_bug.cgi?id=1035774 ), you need to potentially write some rules twice. Note, although not specified in the question I've added pointer: coarse on the assumption that the purpose of these rules is to target mobile screens:

    /* BEGIN devices that DON'T pinch/zoom */
    /* If https://bugzilla.mozilla.org/show_bug.cgi?id=1035774 
    is fixed then we can wrap this section in a ...
    @media not all and (pointer: coarse) and (hover: none) {
    .. and do away with the 'NEGATE' items below */
    
    .myelement {
      /* some styling that you want to be desktop only (style as an anchor on desktop) */
      font-size: 14px;
      text-decoration: underline;
      border: none;
    }
    /* END devices that DON'T pinch/zoom */
    
    /* BEGIN devices that DO pinch/zoom */   
    @media (pointer: coarse) and (hover: none) {
      .myelement {
        /* style as a large button on mobile */
        font-size: inherit;  /* maintain e.g. larger mobile font size */
        text-decoration: none;  /* NEGATE */
        border: 1px solid grey;
      }
    
      .myelement:hover {
        /* hover-only styling */
      }
    }
    /* END devices that DO pinch/zoom */
    

    The combination of (pointer: coarse) and (hover: none) should become more useful to target mobile devices as mobile screens become larger and we lose the correlation between pixel dimensions and mobile vs. desktop (already the case when you wish to distinguish between tablet and desktop)

提交回复
热议问题