3

I'm writing CSS for project and need to convert pixel to centimeter with JavaScript. how do I calculate this conversion?

Grokify
  • 13,266
  • 6
  • 50
  • 73

4 Answers4

6

You could use window.devicePixelRatio to check for device caps. It is used in most browsers.

Bee
  • 71
  • 2
  • 2
1

There are 2.54 centimeters per inch; if it is sufficient to assume 96 pixels per inch, the formula is rather simple:

centimeters = pixels * 2.54 / 96

There is a way to get the configured pixels per inch of your display for Microsoft Windows called GetDeviceCaps. Microsoft has a guide called "Developing DPI-Aware Applications", look for the section "Creating DPI-Aware Fonts".

sample code

Community
  • 1
  • 1
Json
  • 36
  • 3
0

If you just want to convert the pixels to cm using javascript, you can use following formula:

1px = 0.026458 cm;

Please take care this is only for conversion of px to cm.

I am not aware if you are taking DPI of screen into consideration.

jmsds
  • 215
  • 1
  • 7
-2

I think it's useful for you ...

     `1px = 0.02645833 cm;` OR `1 cm = 37.795276px;`

Ref : Pixel to Centimeter? ,

Pixel 2 CM convert Javascript

Balaji G
  • 41
  • 10