简体   繁体   中英

Convert ch to Px in css

Notes: I tried all questions & answers related to this topic.

My Question very simple and clear 1ch = how many px?

My sample code

Relatest search article

  1. Understanding-css-units
  2. css Units
  3. css-unit-conversion-calculator

1ch will be 8px - got this from the sample code below:

 .convertedDiv1 { height: 1ch; width: 1ch; background-color: red; }.convertedDiv2 { height: 2ch; width: 2ch; background-color: green; }.convertedDiv3 { height: 3ch; width: 3ch; background-color: blue; } div { margin-bottom: 12px; }
 <div class="convertedDiv1"></div> <div class="convertedDiv2"></div> <div class="convertedDiv3"></div>

There is no constant ch to px conversion because, like rem and em , ch is a relative unit. 1ch is equal to the width of the 0 glyph (ZERO, U+0030) of the current font.

So if the 0 glyph is 8px wide, then 1ch = 8px (in the example below, this happens to be equal to 0.5em, but this is not a guarantee).

 html { font-size: 16px; }.font-20 { font-size: 20px; }.em { background: red; height: 1em; width: 1em; }.rem { background: blue; height: 1rem; width: 1rem; }.ch { background: green; height: 2ch; width: 2ch; }.px { background: orange; height: 16px; width: 16px; }
 <div> <h1>font-size: 16px (root size)</h1> <div class="ch" title="2ch"></div> <div class="em" title="1em"></div> <div class="rem" title="1rem"></div> <div class="px" title="16px"></div> </div> <div class="font-20"> <h1>font-size: 20px</h1> <div class="ch" title="2ch"></div> <div class="em" title="1em"></div> <div class="rem" title="1rem"></div> <div class="px" title="16px"></div> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM