Reputation: 39248
I am wondering, is it possible to calculate the pixel width of a character in a monospace font? I am using monospace fonts because I need every character to occupy the same width in my DIV, but I am unsure how to reliably get the actual pixel width of it. I need to fix the width in order to do calculations in JavaScript. Here's my css:
font-family: monospace;
font-size: 15px;
Is this possible?
Upvotes: 26
Views: 21692
Reputation: 1325
I've faced with the same question and decided to make a calculator))
Using test-span with 10000 symbols and it's offsetWidth
;
Run the snippet below and click 'OK'.
(function(){
let test = document.getElementById('test');
let font = document.getElementById('font');
let size = document.getElementById('size');
document.getElementById('le').innerText = "Test length: " + test.innerText.length + " symbols.";
document.getElementById('ok').addEventListener('click', function(){
test.style.fontFamily = font.value;
test.style.fontSize = parseFloat( size.value ) + 'px';
document.getElementById('result').innerText = 'Your font-width: ' + test.offsetWidth / 10000 + 'px';
});
})();
<h2>Input Font-family and font-size:</h2>
<input id="font" placeholder="font-family:" value='"Lucida Console", Monaco'>
<input id="size" placeholder="font-size:" value="18">
<button id="ok">OK</button>
<p id="result"></p>
<p id="le"></p><span id="test">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</span>
Upvotes: 4
Reputation: 90
I'm not sure that it is possible to be absolutely sure of the width, or even height, of a letter, Monospace or otherwise, across all platforms. the PX suffix on font-size doesn't even represent actual pixels on the screen (take a look here for more information).
I suspect the only way would be to use a fixed width span for each character.
Upvotes: 0
Reputation: 3141
The best way I can think of to do this, is to create a span off screen (position: absolute; top: -100px;
) with a single character in it. Then you can measure the width of the span to get the width of 1 character in the monospaced font. Ensure the padding
is set to 0px
on the span, however, as this is included in the width.
If you only need an approximation, the ex
unit is often helpful. 1ex
is the height of a lower case 'x'. This is often approximately the same as the width. But it isn't exact.
Upvotes: 18