Jiaaro Jiaaro - 1 year ago 124
CSS Question

CSS floats - how do I keep them on one line?

I want to have two items on the same line using 'float: left' for the item on the left.

I have no problems achieving this alone. The problem is, I want the two items to stay on the same line even when you resize the browser very small. You know... like how it was with tables.

The goal is to keep the item on the right from wrapping no matter what.

So how to I tell the browser using css that I would rather stretch the containing div than wrap it so the the

float: right;
div is below the
float: left;

what I want:

+---------------+ +------------------------/
| float: left; | | float: right; \
| | | /
| | |content stretching \ Screen Edge
| | |the div off the screen / <---
+---------------+ +------------------------\

Answer Source

Wrap your floating <div>s in a container <div> that uses this cross-browser min-width hack:

.minwidth { min-width:100px; width: auto !important; width: 100px; }

You may also need to set "overflow" but probably not.

This works because:

  • The !important declaration, combined with min-width cause everything to stay on the same line in IE7+
  • IE6 does not implement min-width, but it has a bug such that width: 100px overrides the !important declaration, causing the container width to be 100px.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download