Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

css

overflow

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/):

  • main div is 200px width, with overflow: auto (i.e. scrolls contents if wider than 200px).
  • So, as contents div is 300px wide, it scrolls horizontally.
  • So, I would expect container div to be 300px as well (as elements inside it are 300px wide), but it is not! It's 200px wide.

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

like image 882
German Latorre Avatar asked Nov 30 '25 14:11

German Latorre


1 Answers

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.

like image 91
zdesam Avatar answered Dec 03 '25 05:12

zdesam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!