How many pixels is a space equal to?

zjm1126 picture zjm1126 · Nov 26, 2010 · Viewed 21.5k times · Source

This is my code:

<div style="position:absolute;top:300px;width:50px;height:50px;background:red;color:black;word-wrap:break-word;">
        <div contenteditable=true>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            aaaaa
            bbbbbbbbbbb
        </div>
</div>

I want to fill all of the space in the div.

How many pixels is the &nbsp;?

Answer

bcosca picture bcosca · Nov 26, 2010

You can create a <div> containing &nbsp; and assign an id. Set the font size and attributes. Then read the current width and height using:

var space=document.getElementById("space");
var height=(space.clientHeight+1)+"px";
var width=(space.clientWidth+1)+"px";