'Setting the height to a div to a multiple of line height
How do you express the height of a div as a multiple of its line height in pure CSS without hard-coding line height or font size?
Background
I have a box (a DIV) that contains status text. The text will sometimes span multiple lines and often need only one. The status is updated several (five) times per second and this causes the bottom border and everything below the box to "jump" up and down as the box changes size to fits its contents.
One solution to the jumping would be to set the min-height of the DIV to the height of two lines. Status text with more than two lines is rare so that degree of jumping would be acceptable.
Alternatively I could set the height to the exact height of two lines and use overflow:hide to truncate the text. That would eliminate all jumping at the expense of some information.
Which raises the question: How do I express height as a multiple of line height? The status text inherits its appearance from the global style sheet and does not know font size, family, line height, or anything.
Half-solution
I can use the em unit to express height without knowing anything about the font but that still leaves the line height property which also affects the final line height.
Ideally I would prefer to inherit line height but if I override it to a known multiplier then I can force the box to become exactly two lines tall.
.progressStatus
{
line-height: 1.1;
height: 2.2em;
}
This solution is good enough for my needs but I'm curious if there's a better solution that does not involve javascript.
Solution 1:[1]
There is no way to express that in CSS.
The status text inherits its appearance from the global style sheet and does not know font size, family, line height, or anything.
That's not quite correct. Because text in your status div
inherits its values for font-size
, line-height
, etc. via the cascade, it "knows" these and will style itself accordingly. The problem is that CSS doesn't offer a way of using these values for calculations. They are only implicitly considered when declaring new properties.
The only way to achieve exactly what you want is via JavaScript. I made a fiddle here using some jQuery. In my example, a simple body
declaration acts as the ancestor. Run it with different font-size
and line-height
values in the body CSS.
In practice, I would combine this with your method as a fallback for scenarios where
- JavaScript is disabled
- the relevant ancestor's
line-height
is given as a percentage value (descendants inherit the calculated value) and you decide to change your statusfont-size
. Example: The ancestor'sfont-size
is16px
and itsline-height
is120%
(~ 19px). Now, if you decide your status needs more attention and declare.progressStatus {font-size: 24px;}
, it will inherit the calculatedline-height
(still 19px). So you'd have a line-height smaller than the text size. Explicitly declaring aline-height
as in your "half-solution" prevents that case from occuring.
Solution 2:[2]
div {
height: 1em; // that's one line, 2em for 2 lines, etc...
line-height: 1em; // the height of one text line
overflow: hidden;
}
Solution 3:[3]
Maybe using a "shared" variable provides you with a close enough solution to achieve your desired "inherited line height". Declaring a variable avoids defining the line height twice. Taking up your example this solution sets a fixed height of 2.2em
(1.1 * 2em = 2.2em
= 2 lines):
.progressStatus
{
height: calc(var(--line-height) * 2em);
line-height: var(--line-height);
--line-height: 1.1;
}
Change 2em
to 3em
to use 3 line heights (1.1 * 3em = 3.3em).
Optionally the variable -line-height
can also be defined higher up in the DOM hierarchy if helpful as variables are visible in all nested elements, too, until overridden.
Solution 4:[4]
You can use these on the div:
.your-div {
line-height: 1.1em;
max-height: 2.2em;
overflow: hidden;
white-space: normal;
-webkit-line-clamp: 2;
text-overflow: ellipsis;
}
This will cause the end of text displayed as three dots (...) when it's more than two lines.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | showdev |
Solution 3 | |
Solution 4 | noiseymur |