Menu Close

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.

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

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

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

For instance, we 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

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

Posted in CSS