How to make a div not larger than its contents with CSS?

Sometimes, we want to make a div not larger than its contents with CSS.

We’ll look at how to make a div not larger than its contents with CSS, in this article.

How to make a div not larger than its contents with CSS?

We make it, to make a div not larger than its contents with CSS, as an inline-block element.

For instance, we can write

<div>
  <div class="yourdiv">content</div>
</div>

to add nested divs.

Then we write

.yourdiv {
  display: inline;
}

to make the inner div an inline-block element with display: inline;.

Conclusion

Then we make it display, to make a div not larger than its contents with CSS, as an inline-block element.

Leave a Reply