Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can we make a div take width according to its content automatically

I want to create an effect like that of fieldset and label.

alt text

When the headings change along the side, the width that the white background of the heading remains the same.

If I can make the heading or div element to take the width enough to fit its content, it will give the proper effect.

If there is any other solution please let me know.

like image 480
Harsha M V Avatar asked Oct 02 '10 12:10

Harsha M V


2 Answers

You can use float on the div you want to shrink, or display: inline-block (though that won't work in IE6 (since it applies inline-block only to elements inline by default) without specifying a width. Or you can use both, obviously. Though I'm not sure why you'd want to make a floated-block act as if it was inline.

Demo at jsbin

like image 199
David Thomas Avatar answered Sep 19 '22 04:09

David Thomas


I would recommend this

#element {
    display: table; /* IE8+ and all other modern browsers */
}
like image 29
Imran Ali Avatar answered Sep 17 '22 04:09

Imran Ali