Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading