I'd like to display multiple lines of text with a semitransparent background. When these lines are too far away from each other, there's a gap in the background. When they're too close, the background overlaps and transparency is reduced. The background should be one homogeneous area of the same colour and transparency level.
Unfortunately, browsers and fonts are all different and I can't find one correct value to be set for the line-height
that works for all situations. Subpixel positioning adds to it in that the gaps and overlaps change with the exact window or element size.
Here's the sample code:
.bg1
{
background: khaki;
padding: 20px;
font-size: 1.3em;
line-height: 1.1;
}
.bg2
{
margin-top: 1em;
background: khaki;
padding: 20px;
font-size: 1.3em;
line-height: 1.5;
}
.text
{
background: rgba(255, 69, 0, 0.5);
padding: 2px 8px;
box-decoration-break: clone;
-webkit-box-decoration-break: clone;
}
<div class="bg1">
<span class="text">
Line 1<br>
Second line<br>
Text background overlapping
</span>
</div>
<div class="bg2">
<span class="text">
Line 1<br>
Second line<br>
Gaps between text lines
</span>
</div>
Line height values in the sample are exaggerated and an example to increase the chance that you see the intended result. Multiple lines are enforced with <br>
here, but wrapping can also occur in my usage, i.e. manually spreading the lines over separate block elements is not a solution. It has to be inline content.
What value can I use for line-height
to ensure that the text lines exactly touch each other with no overlap or gaps?
I can't accept line-height: normal
for reasons explained here and because I need additional inner padding on the text lines, as shown in the example.