What's dp (density independent pixels) units with CSS?

前端 未结 8 1338
陌清茗
陌清茗 2020-12-23 10:39

For Android, people recommend using dp (density independent pixels) measurements for UI elements, and there are conventions that exist such as using 48dp for a

8条回答
  •  别那么骄傲
    2020-12-23 11:46

    As of CSS3, there are no CSS units that are truly device-independent. See the W3C spec on absolute lengths. In particular, the absolute units might not match their physical measurements.

    If physical units were true to their purpose, you could use something like points; points are close enough to dps:

    1 in = 72 pt
    1 in = 160 dp
    
    1 dp = 72 / 160 pt
    

    If you use SCSS, you can write a function to return in pts:

    @function dp($_dp) {
      @return (72 / 160) * $_dp + pt;
    }
    

    And use it:

    .shadow-2 {
      height: dp(2);
    }
    

提交回复
热议问题