How can you get the CSS pixel / device pixel ratio?

前端 未结 4 1848
臣服心动
臣服心动 2020-12-04 16:36

I want to find the ratio between CSS pixels and device pixels.

Edit: I should have realized that this is just zoom level. I\'ve added an answer to t

4条回答
  •  伪装坚强ぢ
    2020-12-04 17:09

    There is a much better CSS/JS solution:

    /** sample media query for pixel-ratio=2" **/
    @media  
    (-webkit-min-device-pixel-ratio: 2), 
    (min-resolution: 192dpi) { 
      .pixel-ratio-holder:before {
       content: "2";
      }
    }
    
    
    function jsPixelRatio(){
       return window.getComputedStyle(document.querySelector('.pixel-ratio-holder'), 'before').getPropertyValue('content').replace(/[^a-z]/g,'') * 1;
    }
    

提交回复
热议问题