Gerald
Gerald

Reputation: 23479

Force a div to grow wider than the browser window

How can I force a DIV to grow wider than the browser window to accommodate it's children horizontally, rather than forcing them on new rows

i.e. consider the following code with fiddle. There are 6 child elements inside of a container element, all with a minimum width of 200px, and all set to float: left. When the window is resized wide enough they are all on one row. When it is narrowed, they start pushing to new rows. Ideally I would like them to remain on a single row and have the browser display a scroll bar.

http://jsfiddle.net/krippy2k/x8sDp/20/

.container {
}

.child-element {
    min-width: 200px;
    float: left;
    height: 100px;
}

.child1 {
    background-color: purple;
}
.child2 {
    background-color: orange;
}
.child3 {
    background-color: black;
}
.child4 {
    background-color: green;
}
.child5 {
    background-color: blue;
}
.child6 {
    background-color: red;
}

<div class="container">
    <div class="child-element child1"></div>
    <div class="child-element child2"></div>
    <div class="child-element child3"></div>
    <div class="child-element child4"></div>
    <div class="child-element child5"></div>
    <div class="child-element child6"></div>
</div>

Upvotes: 7

Views: 6437

Answers (4)

Mathew Thompson
Mathew Thompson

Reputation: 56429

Just set the .container to display:table and the .child-elements to be display:table-cell and remove float:left from .child-element:

.container {
    display: table;
}

.child-element {
    min-width: 200px;
    display: table-cell;
    height: 100px;
}

DEMO

Upvotes: 2

Andrew Clody
Andrew Clody

Reputation: 1181

Instead of floating the children, set them to inline block and set white-space:nowrap on the container.

.container {
    white-space:nowrap;
}

.child-element {
    display:inline-block;
    min-width: 200px;
    height: 100px;
}

http://jsfiddle.net/x8sDp/24/

Upvotes: 7

cernunnos
cernunnos

Reputation: 2806

Adding a huge width to the container works

.container {
  width: 999999px;
}

Though im not sure why you would want to do something like this, from a usability/interface point of view.

Upvotes: 0

Mr. Alien
Mr. Alien

Reputation: 157314

Either you can provide a width to the parent element, else you can use float: left; with display: inline-block; and white-space: nowrap;

.container {
   white-space: nowrap;
}

.child-element {
    min-width: 200px;
    display: inline-block;
    height: 100px;
}

Demo

For the white-space of 4px between each element, you can use margin-left: -4px;

Demo 2

Upvotes: 14

Related Questions