Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Div Size Automatically size of content

Tags:

css

I am trying to make a h2 header for sidebar widgets but I want the width of the div class to be whatever width the content becomes. It seems I can't just set a width because those headlines with longer content then shorter content makes it break.

How can I simply make width stretch/change depending on the length of content there is? Any help would be greatly appreciated.

like image 204
Zach Smith Avatar asked Sep 20 '10 12:09

Zach Smith


3 Answers

As far as I know, display: inline-block is what you probably need. That will make it seem like it's sort of inline but still allow you to use things like margins and such.

like image 122
Svish Avatar answered Oct 18 '22 19:10

Svish


If you are coming here, there is high chance width: min-content or width: max-content can fix your problem. This can force an element to use the smallest or largest space the browser could choose…

This is the modern solution. Here is a small tutorial for that.

There is also fit-content, which often works like min-content, but is more flexible. (But also has worse browser support.)

This is a quite new feature and some browsers do not support it yet, but browser support is growing. See the current browser status here.

like image 37
rugk Avatar answered Oct 18 '22 19:10

rugk


If display: inline; isn't working, try out display: inline-block;. :)

like image 13
Kyle Avatar answered Oct 18 '22 20:10

Kyle