What Is the Value of the CSS 'Ex' Unit

What is the value of the css 'ex' unit?

It is useful when you want to size something in relation to the height of your text's lowercase letters. For example, imagine working on a design like so:


alt text



In the typographic dimension of design, the height of letters has important spatial relationships to the rest of the elements. The lines in the source image above are intended to help point out the x-height of the text, but they also show where guidelines would be if designing around that text.

As Jonathan pointed out in the comments, ex is simply the height version of em (width).

Why is CSS 'ex' unit defined using the 'first available font'?

Why is the em unit defined in terms of the font actually used to render the text, and the ex unit using the first available font?

This shouldn’t be the case: both units are intended to be relative to the current font. The definition you provided mentions “font-relative lengths such as ‘ex’,” which also includes the ‘em’ unit.

That said, it seems like the authors agreed that the definition of “first available font” should be clarified: https://github.com/w3c/csswg-drafts/issues/4796

The section you quoted seem to imply that if the first font in the font-family list exists, but the U+0020 (space) character isn’t in the font, then the next font should be used. In practice, it sounds like browsers weren’t doing this anyway, and that probably wasn’t the original intent.

You can see the change that is being made to the definition here, as summarized in that issue: https://github.com/w3c/csswg-drafts/commit/7c2108c1764f328e0b60fffed47d3885a3dc7c11?diff=split

Why does the algorithm look for the space to compute the height of the letter 'x'? An explanation in layman terms would be very appreciated.

For the purpose of collecting and calculating font metrics, the U+0020 space is most likely the earliest and most common code point that could contain that information and would make sense to check. Many metrics are being calculated then, like the line height and em unit, not just the ex unit.

Beyond that, CSS ex unit section gives more detail on how that value is determined:

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.

How is the 'ex' unit in CSS useful?

I'd think ex should be used instead of em when dealing with vertical rather than horizontal measurements (e.g., height instead of width).

What is difference between CSS em and ch units?

I've looked at all the answers and also your comments on them and I got the feeling that you want using em and ch units mainly for width or height properties.

[...] they are not good for setting a field width then?

And from my perspective, I would not recommend that.


CH-Units

First of all, I've never worked with ch before and honestly I do not see any real use for it - maybe an input for the year, so that there is always the width for four numbers, but not more - because it never tells you exactly how wide an element will end up.

I changed my mind and now I see a good real use for ch. :)

The example of an input for the year, so that there is always the width for four numbers, will persist. But ch is also very useful for correctly defining the text width of a paragraph.

With pixel, relative or percentage values it is very difficult - especially for a responsive design - to set the perfect text width per line including spacing for a paragraph. However, this can be a liability for the user experience on the website. An abstract by Atlassian Design is a perfect match:

Set the reading environment to suit the reader. Wide lines of text are difficult to read and make it harder for people to focus. While there is no right way to measure the perfect width for text, a good goal is to aim for between 60 and 100 characters per line including spacing. Setting an optimal line length breaks up content into easily digestible blocks.

Source: Atlassian Design

This is where ch can be used perfectly as a unit. For a better view, you can look at the examples at the end.

But now finally to the definition of ch ;)

As often been said, ch refers to the width of the 0 character to its font size.

As example: The body has a font-size: 16px and the 0 character has a width of 10px from the selected font-family, then 1ch equals 10px. And even that is still inaccurate, because e.g. italic or bold can change the width of the character.


EM- & REM-Units

As also often said, em - and to bring one more player in - also rem are relative to the font-size.

Where rem is always relative to the root element font-size, em is relative to the font-size of the own element or the last parent element (can be also the root element) with a font-size.

As em example: Only the body has a font-size: 16px; and the element got no font-size himself, then 1em equals 16px. If a parent of the element or the element himself got a font-size: 20px;, then 1em equals 20px. em units can also multiply upon themselves.

As rem example: The body has a font-size: 16px;, then 1rem equals 16px. Even if the element himself or a parent element got a font-size: 20px;, 1rem still equals to the body settings of 16px.

[...] if ch is size of '0' in font, why is not em the size of 'M' in font [...]

em was originally based on the typographic measurement of the current font M character but that is already outdated. As you can see now, it always references to a "fixed start-value" and not the width of a character.


Recommended usage

As I said, from my perspective I would not recommend to use ch, em or rem for width or height properties. These units are more useful for "textual" properties.

Some examples:

  • All h2 should be four times as big as the text: font-size: 4rem;
  • All p should always have a margin of a half line: margin-bottom: 0.5em;
  • The line-height of elements should be 20% greater than the font-size: line-height: 1.2em;
  • A input for the year, should always have the width of four numbers: width: 4ch;
  • The text width of a p should always have a width of 80 characters per line including spacing: width: 80ch;

but pixels are not flexible when dealing with different devices

For this, as a conclusion, I would advise you to simply work with percentages width: 80%; or the viewport width and viewport height height: 100vh; width: 100vw;. Or at least always with a maximum value: width: 1000px; max-width: 100%;.


Here are a few snippets - just for the examples with width properties - for a better understanding:

em - Relative to the font size of the own element or the last parent element with a font size

body {
font-size: 16px;
}

p {
font-weight: bold;
}

div {
width: 1em;
height: 50px;
background: lightgray;
}

.em-0-5 {
width: 0.5em;
}

.em-10 {
width: 10em;
}

.fs-10 {
font-size: 10px;
}

.parent {
font-size: 0.5em;
}

.parent div {
width: 10em;
}
<p>1em (body font-size * 1 = 16px)</p>

<div></div>

<p>0.5em (body font-size * 0.5 = 8px)</p>

<div class="em-0-5"></div>

<p>10em (body font-size * 10 = 160px)</p>

<div class="em-10"></div>

<p>10em from 10px own element font-size (element font-size * 10 = 100px)</p>

<div class="em-10 fs-10"></div>

<p>10em from 0.5em parent font-size (body font-size * parent font-size * 10 = 80px)</p>

<span class="parent">

<div></div>

</span>

What is the difference between px, em and ex?

  1. Pixels (px) are browser dependent. It is the absolute size that you would see on your screen.
  2. Em are sort of like percentages. Ems is referring to the base text size. The value of 1 em means the same thing as a value of 100 percent. But you can also say it in the opposite way: A percentage value is just an em multiplied by 100.
  3. Points(pt) are what you would want to use in print media.

how to find ex value of specific font at specific size

One way, without using any external libraries, is to create a block with a height of 1ex and measure how high it is.

var div = document.createElement('div'); div.style.fontSize = '1000px';div.style.height = '1ex';document.body.append(div);var xHeight = div.scrollHeight;document.body.removeChild(div);
console.log('1ex = 0.'+xHeight+'em');

Calculate an expression with css units

You can probably take help of the mathematical operators. Assume that you have two quantaties, "50%" & "100%", as given above. Now try to find out the symbol between them. If it's a "+", or "-", then don't remove the "%", or if it's "/" just remove it. It can go something like this :

if ( contains("+") || contains("-") ) {
// Code such that % don't get remove
}
else if ( contains("/") ) {
// Code such that % gets removed
}

Here, the contains() is just to make you understand, it's not a pre-defined function. Now what can you do to find out the symbol out ? It's not that perfect, but yay you can use regex ! For e.g. we had the quantaties "50%" & "100%", you can do /%(.*?)%/gm to find the content between, then remove all the empty spaces, letters and numbers, which can be done again by regex. Therefore I think you get a preety sure idea. Well it's not a perfect solution, but I think you can come arround with it ! As @Anonymous told this is gonna be a complex function, but not an impossible one.

When specifying a 0 value in CSS, should I explicitly mark the units or omit?

I argue you should also omit the units.

From a programmer's perspective, 0 == null == none == false, where 0px == 0px only.

Which means that if you specify a border width of 0 then no border will be there, but if you specify a 0px border, then a border of 0 px will be created (that's the idea behind it, in reality 0px gives the exact same result like 0).

Further Points

  • unit-less 0 makes it easier to read as it is easily distinguishable from normal unit'ed values.
  • It makes sense to remove the units as they have no point in being there (0 could mean size, color, etc.).

Conclusion: Omit the units in 0. They're not needed and confusing.

What is the resultant unit type of some VW value + some REM value?

It's complicated.

From the linked spec we see that the computed value of your clamp expression will resolve to a single value, because it can be so resolved. But had it contained percentages, or something else that depended on layout, then the computed value might be the expression, or a simplified version of the expression, and the single value won't be obtained until the used value is determined.

The actual unit of the single value is not determined. However, for serialization (as you'd see in the browser's dev tools for example). lengths are represented in pixels. Not all calculations are represented in pixels though. Angles, for example, calculating on degrees and radians and turns, will be represented in degrees. Other types of values have other canonical units.

When/Why Do CSS Property Values Need to Specify px ?

All CSS properties that take a length value require units (of which px is an example) for values other than 0.

This has always been the rule for CSS, so this is nothing to do with the maintainers of the CSS specification.

For backwards compatibility with the buggy browsers of the 1990s, when the page's Doctype triggers quirks mode, lengths default to px units. This is an intentional emulation of bugs by browser developers.

I assume that this backwards compatibility rule does not apply for newer properties that didn't exist in the days that those browsers were around.


Always use a Doctype that triggers Standards mode. The inconsistencies of Quirks mode are more trouble then they are worth. Always specify units for non-zero lengths.



Related Topics



Leave a reply



Submit