Relation between dp - sp and PX

前端 未结 4 1610
悲哀的现实
悲哀的现实 2021-02-04 03:45

I am not asking what is the difference between dp,sp and px.

I am designing a website based on google\'s new material design, all the measurements are in dp (for grid)

4条回答
  •  -上瘾入骨i
    2021-02-04 04:31

    A safe rule of thumb is to use 1 px = 1 dp.

    This should give you a good safe size on just about any device. It will appear a bit large on some devices, notably the iPad (regular).

    Here's why:

    "A dp corresponds to the physical size of a pixel at 160 dpi" (https://developer.android.com/training/multiscreen/screendensities.html#TaskUseD)

    160 dpi means:

    160 dots = 1 inch

    Therefore:

    160 dp = 1 inch (25.4 mm)

    So when Google recommends that buttons have a touchable target height of 48 dp, they're saying that they need to be 0.3 inches (7.6 mm) tall.

    So how many px is this? Well, that depends on the device.

    Examples for 48 dp (7.6 mm) button height:

    • iPad mini: 48 px Why: The iPad mini screen is about 120 mm wide and uses 768 px to fill that space. You therefore need 162 px to take up an inch (25.4 mm), or 48 px for your button height of 7.6 mm.

    • Kindle Fire (7"): 43 px

    • Kindle Fire (6"): 50 px

    • iPhone: 48 px

    • Nexus 7: 48 px

    • Regular iPad: 39 px

    (I may have fudged the rounding up/down a tiny bit.. I like 48 better than 49!)

    Screen mm and CSS px width for examples: I calculated the screen width using the CSS px screen dimensions and diagonal length.

    • iPad mini: 1024 x 768 resolution and 201 mm diagonal = 120 mm width.
    • Kindle Fire 7": 858 x 533 resolution and 178 mm diagonal = 94 mm width.
    • Kindle Fire 6": 853 x 533 resolution and 152 mm diagonal = 81 mm width.
    • iPhone: 568 x 320 resolution and 102 mm diagonal = 50 mm width.
    • Nexus 7: 960 * 600 resolution and 178 mm diagonal = 94 mm width.
    • iPad regular: 1024 x 768 resolution and 246 mm diagonal = 148 mm width.

    Note that for calculating the px height of the button you need to use the device CSS px dimensions. These numbers are not necessarily the same as the resolutions stated in the specs.

提交回复
热议问题