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?
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.
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.
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).
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.
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).
http://www.w3.org/TR/css3-values/#lengths
The closest unit available in CSS are the viewport-percentage units.
The only mobile browser to be aware of that doesn't support these units is Opera. http://caniuse.com/#feat=viewport-units
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With