German Latorre
German Latorre

Reputation: 11158

DIV not adjusting width to contents inside another DIV with overflow: auto

I have the following HTML code:

<div class="main">
    <div class="container">
        <div class="contents">
            Some funny stuff in here
        </div>
    </div>
</div>

With the following CSS:

.main {
    overflow: auto;
    width: 200px;
}
.container {
    border: 1px solid black;
}
.contents {
    width: 300px;
}

This is what this page does (see it at http://jsfiddle.net/C7RDh/7/):

How come? I want it to be as wide as its contents (300px), how can I achieve that?

Upvotes: 3

Views: 10485

Answers (3)

semir.babajic
semir.babajic

Reputation: 751

You need to slightly adjust your CSS. This will work:

.main {
  overflow: auto;
  width: 200px;
  float: left;
}
.container {
  border: 1px solid black;
  float: left;
}

.contents {
  width: 300px;
  float: left;
}

Upvotes: 0

Bruce.zhang
Bruce.zhang

Reputation: 1

Actually you should add the overflow: auto in container css not main css

Upvotes: -1

zdesam
zdesam

Reputation: 2986

You just need to make you container float

.container {
     border: 1px solid black;
     float: left;
 }

Float will automatically adjust your outer div to inner div width.

Upvotes: 2

Related Questions