Sony packman
Sony packman

Reputation: 840

Make div NOT auto stretch to container width

My question is simple:

Can you stop a div from auto-stretching to the width of its containing element and have it only stretch horizontally as much as the content inside it forces?

Kind of like the default vertical behavior of divs but applied horizontally. Is this possible?

Upvotes: 21

Views: 26001

Answers (2)

Ben
Ben

Reputation: 57217

Actually, display:inline will have better browser support, but may not achieve the results you want, it will keep the div in line with the content, a la <span>.

There's two types of elements: block, and inline. Block elements stretch to width and break the line. Inline stretches to content and stays inline. (!)

display:inline-block is getting better support, but the older browsers won't do it.

Upvotes: 5

Ross McLellan
Ross McLellan

Reputation: 1881

You'd have to set the display property to inline-block

<div style="display: inline-block">Text</div>

Upvotes: 32

Related Questions