Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining css width/heights in device independent units?

Tags:

html

css

I have a div and I'm trying to figure out how to get it to occupy the same amount of screen space regardless of device display density.

For example, let's say I have two devices that are each 5 inches wide. The first display has a device-pixel-ratio=1, and the second has a device-pixel-ratio=2.

Device 1: 5 inches wide, device-pixel-ratio=1
Device 2: 5 inches wide, device-pixel-ratio=2

So the second device has twice as many pixels packed into the same space.

My div style:

.myDivStyle {
    width: 100px;
    height: 50px;
}

If I understand correctly, Device 2 would appear to render the div at half the width/height as on Device 1.

If that's the case, is there a way to define our width/height in a device-independent unit? Or do we have to scale all our styles manually on page load etc after we examine the device-pixel-ratio attribute?

Thank you

like image 755
user291701 Avatar asked Nov 27 '12 15:11

user291701


People also ask

How do you specify units in the CSS?

A CSS unit is used to determine the property size, which we set for an element or its content. The units in CSS are required to define the measurement such as margin: 20px; in which the px (or pixel) is the CSS unit. They are used to set margin, padding, lengths, and so on.

Can you use DP in CSS?

When writing CSS, use px wherever dp or sp is stated. Dp only needs to be used in developing for Android. When designing for the web, replace dp with px (for pixel).

What are the CSS length unit identifiers?

Absolute Lengths: the cm, mm, Q, in, pt, pc, px units.


1 Answers

Redefining your css size by using em unit would be good.

Some good links in this reference. Please check these

  1. w3.org
  2. w3.org
  3. css-tricks

All the above links urge that, em is best suitable in cases when you want your document to behave well on wide range of devices.

like image 194
mtk Avatar answered Oct 15 '22 04:10

mtk