max-width adjusts to fit text?

后端 未结 4 1081
[愿得一人]
[愿得一人] 2020-12-03 16:55

Not the best title, but anyway...

I have an element with a max-width and some text.

If the text is longer than will fit on one line, I get this:

4条回答
  •  悲哀的现实
    2020-12-03 17:53

    I’ve yet to find a method that does not require the use of both a wrapper element and JavaScript, but it’s possible that this one suits your needs.

    My text here, hello everyone!
    ​​​​​​​​​​​​​​​​​​​​​
    ​var content = document.getElementById("content");
    
    content.parentNode.style.width = content.offsetWidth + "px";​​​​
    

    a working example

提交回复
热议问题