Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent floated div elements from wrapping when the browser is re-sized?

Tags:

html

css

How do you make DIV's that are floated left next to each other not wrap when the browser is re-sized such that the viewport becomes smaller?

div {   float: left; } 

For example when the browser is fully maximized the divs line up like this:

|div| |div| |div| |div| |div| |div| |div| |div| 

But when the browser re-sized smaller this happens:

|div| |div| |div| |div| |div| |div| |div| |div| 

How can I make the divs not wrap when the browser is re-sized smaller?

like image 575
Marcus Avatar asked Feb 16 '10 18:02

Marcus


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 do I keep my DIV from wrapping?

If you want to prevent the text from wrapping, you can apply white-space: nowrap; Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code).

What property can be used to help position floating elements properly on a Web page?

In CSS, the float property specifies how an element should float. The floated element will be removed from the normal flow of the page, but it will remain part of the flow — meaning, the element will be shifted to the left or right until it touches the edge of its container or another floated element.


2 Answers

Wrap them in another div, which has a width (or min-width) specified.

<div class="parentContainer">     <div class="floater"></div>     <div class="floater"></div>     <div class="floater"></div> </div>  .parentContainer {     /* The width of the parent needs to be equal to the total width of the children.     Be sure to include margins/padding/borders in the total. */     width: 600px;     overflow: auto; } 

It also helps to have overflow: auto specified on the containing div, to allow its height to match the child floats.

like image 117
AaronSieb Avatar answered Oct 09 '22 09:10

AaronSieb


I'm pretty late to the game, but here's what I've found that works:

Let's say you have a navigation structured as:

<nav>     <ul>         <li><a href="#">Example Link</a></li>         <li><a href="#">Example Link</a></li>         <li><a href="#">Example Link</a></li>     </ul> </nav> 

To get it to display the links inline, without ever wrapping, you can simply use:

nav ul {     white-space: nowrap; }  nav ul li {     display: table-cell; } 

No fixed widths or anything required.

Fiddle: http://jsfiddle.net/gdf3prb4/

like image 37
JacobTheDev Avatar answered Oct 09 '22 09:10

JacobTheDev