Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 button height, etc.

I'm working on a web application, and I'm getting a lot of criticism on the UI design saying it does not conform with Android design standards. Obviously, my application is going to look different since it is using CSS and HTML instead of the Android Holo theme, but I would still like to make it conform as much as possible. However CSS does not allow density independent measurements.

When I test my application on different resolutions and pixel densities, it does not look good, and sometimes, it is way out of proportion so it's not even functional. CSS doesn't have dp units like Android native development does, but I was wondering what some alternatives are.

Can I somehow get the pixel density using Javascript and manually scale everything appropriately? What is the best way for making a web app that looks and works nicely on all resolutions/densities?

like image 797
Brad Avatar asked May 06 '13 01:05

Brad


People also ask

What is dp unit CSS?

A dp is equal to one physical pixel on a screen with a density of 160. To calculate dp: dp = (width in pixels * 160) / screen density. When writing CSS, use px wherever dp or sp is stated. Dp only needs to be used in developing for Android.

What are the units of dp?

As dp is a physical unit it has an absolute value which can be measured in traditional units, e.g. for Android devices 1 dp equals 1/160 of inch or 0.15875 mm.

What is px unit in CSS?

The px unit is the magic unit of CSS. It is not related to the current font and usually not related to physical centimeters or inches either. The px unit is defined to be small but visible, and such that a horizontal 1px wide line can be displayed with sharp edges (no anti-aliasing).

What is dp in pixel?

DP: A virtual pixel unit used to communicate layout dimensions or location in a density-independent manner while creating UI layout. The density-independent pixel corresponds to one physical pixel on a 160 dpi screen, which is the system's baseline density for a “medium” density screen.


2 Answers

I disagree with the currently accepted answer. As uber5001 suggests, a px is a fixed unit, and in a similar spirit to the efforts of the Android-specifc dp.

Per Material's spec:

When writing CSS, use px wherever dp or sp is stated. Dp only needs to be used in developing for Android.

Additionally

When designing for the web, replace dp with px (for pixel).

like image 151
Tohuw Avatar answered Sep 30 '22 19:09

Tohuw


http://www.w3.org/TR/css3-values/#lengths

The closest unit available in CSS are the viewport-percentage units.

  • vw - Equal to 1% of the width of the initial containing block.
  • vh - Equal to 1% of the height of the initial containing block.
  • vmin - Equal to the smaller of vw or vh.
  • vmax - Equal to the larger of vw or vh.

The only mobile browser to be aware of that doesn't support these units is Opera. http://caniuse.com/#feat=viewport-units

like image 33
cimmanon Avatar answered Sep 30 '22 18:09

cimmanon