Search code examples
javascriptcsswidthspace

How many pixels is a space equal to?


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;?


Solution

  • 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";