Rhiannon
Rhiannon

Reputation: 211

How to make the height of a div box depend on how much text is in it

I fixed the width to 900px but I want the height to depend on the amount of text and objects in it. Is there a way to do this, this is the CSS I currently have for the div

#mainbox {
padding:0;
margin:0 auto;
width:900px;
border:solid 2px  

}

Upvotes: 1

Views: 3569

Answers (2)

brentonstrine
brentonstrine

Reputation: 22732

The code you have does that. Here's a JSFiddle demo. The only thing I changed is I made your ID into a class, since I was putting multiple divs in there.

If you don't specify a height, and the div has not been set to position: absolute, position: fixed and its content is in normal flow (e.g. not float:left or float:right or with a weird position) then the height should adjust fine with the content.

Upvotes: 0

Rob
Rob

Reputation: 12872

You would do this by not setting a height at all which is equivalent to height: auto;. By default the div will wrap the content. If it's not doing that then post a jsfiddle.

Upvotes: 3

Related Questions