Wintersun
Wintersun

Reputation: 1

CSS Div with fixed height and automatic width

so i want to make a div container, which i give a fixed height and want that it automatically fit its width to the content in it, even if it is over 100% of the screen width. Simply by declaring width:auto on the css of the div won't work, because it's not becoming bigger than 100% of the screen.

.container {
    background: red;
    height: 200px;
    width: auto;   
}

Here is a demo:

http://jsfiddle.net/8MqtN/2/

Upvotes: 0

Views: 10738

Answers (3)

Scott Dallas
Scott Dallas

Reputation: 347

I just added to your css class .container

position: relative; display: table;

Now your content seems to be aware of the the height of your content. The red background you set expands accordingly to the content inside. Is that what you are trying to achieve?

Upvotes: 0

crstamps2
crstamps2

Reputation: 614

Instead of width try using display:inline-block

Upvotes: 0

Devesh
Devesh

Reputation: 4550

Use the following css for your body and try once and see if you get what you want .

      body
          {
          margin : 0;
          padding: 0;
          }

Upvotes: 0

Related Questions