getting text block length with css (preferred) or javascript

Is there a way to know the actual length of div below after filling with text? with css (preferred) or javascript.

*{
  margin:0;
  padding:0;
}

div{
  display:inline-block;
  background-color:steelblue;
  font-family: roboto;
}
<div>what is my length?</div><br/>
<div>what is my length then????</div>

>Solution :

In JS, you can use offsetWidth. You also have offsetHeight for the height.

console.log(document.getElementById("a").offsetWidth);
console.log(document.getElementById("b").offsetWidth);
*{
  margin:0;
  padding:0;
}

div{
  display:inline-block;
  background-color:steelblue;
}
<div id="a">what is my length?</div><br/>
<div id="b">what is my length then????</div>

Leave a Reply