Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS non-wrapping floating divs

I need to create a single line containing a variable amount of (floating?) divs: the container dimension is fixed, and it is supposed to add an horizontal scrollbar when needed, never wrapping.

I tried the following, but it doesn't work: it wraps instead.

div#sub {     background-image:url("../gfx/CorniceSotto.png");     width:760px;     height:190px; } div#sub > div#ranking {     position:relative;     top:42px;     left:7px;     width:722px;     height:125px;     overflow-x:auto;     overflow-y:hidden; } div#sub > div#ranking > div.player {     float:left;     width:67px;     height:120px;     margin-left:5px;     background-color:#f3e1bb; } 

I've tried a few things (inline,table-cell,etc.) but they all failed.

Can this be done? If so, how?

like image 805
o0'. Avatar asked Dec 09 '11 15:12

o0'.


People also ask

How do you keep a floating element from wrapping?

Use "overflow: hidden" to avoid floating elements from wrapping a container's text. Unfortunately, any content of the content 's text will wrap underneath it: ![ paja9.

How to clear floats CSS?

To clear a float, add the clear property to the element that needs to clear the float. This is usually the element after the floated element. The clear property can take the values of left , right , and both . Usually you'll want to use both.

Is float still used in CSS?

The float property still exists in CSS as it did when Internet Explorer was a young browser, and still works the same.

What are two valid techniques used to clear floats CSS?

Left and Right can be used to only clear the float from one direction respectively. None is the default, which is typically unnecessary unless removing a clear value from a cascade.


2 Answers

Use display: inline-block instead of floatand give the container white-space: nowrap.

div#sub > div#ranking {     position:relative;     top:42px;     left:7px;     width:722px;     height:125px;     overflow-x:auto;     overflow-y:hidden;     white-space: nowrap; } div#sub > div#ranking > div.player {     display: inline-block;     width:67px;     height:120px;     margin-left:5px;     background-color:#f3e1bb; } 

Here an example: http://jsfiddle.net/D5hUu/3/

like image 129
DanielB Avatar answered Oct 28 '22 14:10

DanielB


inline won't work, table-cell should work - see this jsFiddle I made in answer to a similar question:

http://jsfiddle.net/DxZbV/1/

won't work in <=ie7 though...

like image 20
ptriek Avatar answered Oct 28 '22 13:10

ptriek