3

What I want to do

I would like to measure the 1ex in px.

What I have learned

  • 1ex is a relative unit -- equal to the height of lower case "x" of the font in use:

An instructive image describing the situation:

enter image description here

link to CSS spec:

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

Any information is appreciated!

EDIT:

Some people suggests that this is a dupe of

JavaScript: convert ex to px

but there are NO ACTUAL answers: The first answer explains only relativity of 1ex and the second one is even wrong -- document.getElementById('upper').style.height returns "" // empty char.

Community
  • 1
  • 1
N. Shimode
  • 71
  • 1
  • 8
  • 1 ex equals 6 pixels in both windows and linux, and in most browsers as default. so shouldn't be to hard to figure out, even if it is the most useless unit of all time. – adeneo Dec 09 '13 at 03:12
  • http://kb.mozillazine.org/Em_units_versus_ex_units – adeneo Dec 09 '13 at 03:14
  • I mistook the id as an indication of a css property for font size. Large is referring to a capital X, while small refers to lowercase. I didn't catch that. – Rhyono Dec 09 '13 at 03:34
  • thanks anyway! I renamed those ids to avoid confusion. – N. Shimode Dec 09 '13 at 03:38
  • This might help. http://stackoverflow.com/questions/4392868/javascript-find-divs-line-height-not-css-property-but-actual-line-height – JCOC611 Dec 09 '13 at 03:40
  • The questions are similar but there wasn't enough answer there as I wrote above! – N. Shimode Dec 09 '13 at 03:45
  • 1
    It’s clearly a duplicate. If the answers to the old question are not adequate, we should try and improve them, instead of spawning new copies of a question, resulting in the same question asked multiple times with different sets of answers. – Jukka K. Korpela Dec 09 '13 at 07:12
  • I appreciate your opinion, I think I get your point. In your definition, this may be called a "duplicate". Yes, the same question has already been raised, and the answers are already "accepted". I agree, it is the questioner who determines whether he/she had an answer. But that answer, in this case, is not enough TO ME (actually I guess that was not enough for everyone). That's why, I think, it is worth raising this question again. – N. Shimode Dec 09 '13 at 08:00

2 Answers2

1

Best way I can think of:

  • Fill a canvas with white
  • Draw the letter on the canvas in black
  • Measure the vertical distance between the first non-white pixel to the last non-white pixel

You should be able to achieve this using getImageData.

Alternatively you could also try using the canvas method measureText which should return the width of the text string passed in. Considering x is often quite symmetrical, you could get reasonably accurate results with just that.

Jani Hartikainen
  • 42,745
  • 10
  • 68
  • 86
  • This does not work with fonts that do no have the letter "x". The unit ex has nothing to do with the actual "x" as Illya mentions below. – CookieEater Jul 11 '14 at 09:46
1

Here is what the current CSS spec says about ex unit:

Equal to the used x-height of the ... font. The x-height is so called because it is often equal to the height of the lowercase "x". However, an ‘ex’ is defined even for fonts that do not contain an "x".

The x-height of a font can be found in different ways. Some fonts contain reliable metrics for the x-height. If reliable font metrics are not available, UAs may determine the x-height from the height of a lowercase glyph. One possible heuristic is to look at how far the glyph for the lowercase "o" extends below the baseline, and subtract that value from the top of its bounding box. In the cases where it is impossible or impractical to determine the x-height, a value of 0.5em must be assumed.

So 'x-height' doesn't have to do anything with the actual character "x", it's just a font metric, either stored in the font file or calculated by the browser. And the best way to measure it, IMO, is to create a test block with the same font properties, set to it, e.g., width: 10ex and then measure its clientWidth (or jQuery's width()).

Ilya Streltsyn
  • 13,076
  • 2
  • 37
  • 57
  • As I pasted a link above, I know the spec of 'ex'.. but thanks, your solution actually works. (Though I was not talking about 'width' but 'height'.) – N. Shimode Dec 10 '13 at 06:24
  • 1
    Just a note to future visitors that `clientWidth` and jQuery's `.width()` rounds down the value. If you need accurate measurement, use [`getBoundingClientRect()`](https://developer.mozilla.org/en-US/docs/Web/API/Element.getBoundingClientRect) instead. – CookieEater Jul 11 '14 at 09:49
  • Also be aware that the ratio of ex to px may differ between height and width... – Jthorpe Mar 31 '17 at 07:12
  • Jthrope, isn't this because of the rounding mentioned above, so slightly different ratio for different values, not different directions? – Ilya Streltsyn Apr 01 '17 at 15:47