I want to scale a letter to the exact height of another element with CSS. For example, I might want to have text that is exactly as high as some image:
How can I achieve this?
When I set font-size equal to the height of the image, the letters are smaller. I can try to adjust this manually, until it looks equal, but I'd like to calculate it.
My current solution is to set line height to the height of the image, and to calculate font-size from the font metrics:
.the_image {
height: 28px;
}
.the_M {
line-height: 28px;
font-size: calc(28px * 2048 / 1490);
}
In this case, the font metrics are 2048 UPM (units per em), and the letter "M" is 1490 units high. Dividing the em size (2048 units) by the letter height (1490) results in a factor that scales the "M" to the desired height (here: 28px).
In this case I know the font that I use, because I embed it with @font-face, and I know the metrics of the font because I looked at it in a font editor (I'm using Fontforge and Glyphs to create my fonts), but I'd still like a solution that works for unknown fonts from the user's computer.