Pixel width of monospace character
css, fonts, javascript
Solution
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.
Problem
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?