Languages
[Edit]
EN

CSS - fit div width to content size

3 points
Created by:
Isaiah89
721

In this article, we would like to show you how to fit div width to content size in CSS.

Quick solution:

 

Practical example

Edit

Note: this solution was intoduced in Microsoft Edge web browser in 2020.

 

Alternative solution

Edit

1. Float based solution

Edit

This solution is based on setting the overflow: hidden on the container element to hide the overflow (disable scrolling) and setting the float: left on the content element that contains the text.

 

2. Position based solution

Edit

This solution required fixed height for container element.

 

See also

Edit
  1. JavaScript - measure text width

References

Edit
  1. overflow - CSS: Cascading Style Sheets | MDN
  2. float - CSS: Cascading Style Sheets | MDN
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join