I'm trying to find out how many monospace characters fit in an element (e.g. div
), knowing the size and the font-size
.
For instance I expected the result being:
{
x: Math.floor(divWidth / fontSize)
, y: Math.floor(divHeight / lineHeight)
}
But it seems that they are not right: for a font size 50px
and width: 100px
, the expected answer would be 2
, but it is 3
:
div {
font-family: monospace;
background: black;
color: lightgreen;
font-weight: bold;
width: 100px;
height: 100px;
font-size: 50px;
}
<div>
123
123
</div>
For the example above the answer should be:
{
x: 3 // 3 chars horizontally
, y: 1 // 1 char vertically
}
How to compute these values automatically?
var $div = $("div");
var divSize = {
w: $div.width()
, h: $div.height()
};
var fontSize = parseInt($div.css("font-size"));