vendredi 30 janvier 2015

How to calculate number of characters of monospace font that fit into div


I have code like this:



div {
font-family: monospace;
font-size: 12px;
line-height: 14px;
}

$(function() {
var div = $('div');
var space = $('<span>&nbsp;</span>').appendTo(div);
var num_chars = div.width() / space.width();
var chars = '';
for (var i=0; i<num_chars; ++i) {
chars += 'd';
}
div.html(chars);
space.remove();
});


but number of characters is way to small to fill the width of the div. Anyone know how to calculate the number of characters that will fill one line of a div?





Aucun commentaire:

Enregistrer un commentaire