Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

force float:left divs to dont go to next line

Tags:

html

css

lets say I got 20 same divs with float:left property and some width. I want them to be in one line and if they dont fit in screen just to make page scroll horizontally.

fiddle

like image 751
Adarchy Avatar asked Oct 16 '12 16:10

Adarchy


People also ask

How do I stop div from going to the next line?

Set size and make inline Because they're block elements, when reducing the size of Div one to make room for the other div, you're left with space next to Div one and Div two below Div one. To move the div up to the next line both div's need to have the inline-block display setting as shown below.

How do I force a div to stay in one line?

You can force the content of the HTML <div> element stay on the same line by using a little CSS. Use the overflow property, as well as the white-space property set to “nowrap”.

How do I force HTML elements to stay on the same line?

To get all elements to appear on one line the easiest way is to: Set white-space property to nowrap on a parent element; Have display: inline-block set on all child elements.


2 Answers

That's basically how floats work. If you want the described behaviour you can do something else instead, for instance white-space: nowrap; on the container and display: inline-block; instead of float.

http://jsfiddle.net/NPzsV/3/

.container {
    white-space: nowrap;
}
.line {
    display: inline-block;
    width: 200px;
    vertical-align: top;
    white-space: normal;
}

One thing to note though: with this approach, newlines/spaces/tabs between the divs will cause a space between them in the rendering.

like image 173
xec Avatar answered Sep 29 '22 11:09

xec


Use display: inline-block instead of float: left on the divs, and add the property white-space: nowrap to their parent container.

Demo: http://jsbin.com/akiniv/1/edit

Demo with your fiddle ;) http://jsfiddle.net/NPzsV/4/

like image 38
tuff Avatar answered Sep 29 '22 12:09

tuff