Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making floated div shrink instead of dropping to new line

Is there any way I can force a floated div to shrink instead of going to a new line?

I know I can set implicit widths on the divs but it's on a menu which might have variable amounts of items in it. I'm trying to do this while keeping the site's layout fluid if possible.

#left {
  float: left;
  border: 1px solid #000;
}

#right {
  float: right;
  border: 1px solid #000;
}
<div id="left">
  <p>This div represents the logo</p>
</div>
<div id="right">
  <p>When the window's width is reduced and these divs touch I want this div to shrink instead of falling to the next line.
  </p>
</div>

Basically, I want #right to begin shrinking when the browser window is shrunk rather than having it drop a line first, then shrink when the window is further resized.

like image 704
George Kendros Avatar asked Nov 26 '10 01:11

George Kendros


People also ask

How do you stop DIVS from sliding when floating?

Set height and width to 100% for the bottom div and set overflow auto on the bottom div as well. Now the div will not move up to fill space.

How do I make a div stay the same size?

if you set width:500px; and height:300px; (or whatever size you want) the div should stay the same size. Also, you can set overflow:hidden; so that if whatever is in the div goes beyond those bounds, it is not shown, and does not resize the div. Save this answer.

Why is the div not breaking in to the next line?

It is an inline-level element and does not break to the next line unless its default behavior is changed.

How do I keep my div from wrapping?

Alternative way to do this is to use the display:inline-block . Where row element has white-space:nowrap. inline-block makes the element inline but preserves the settings such as width, height, top and bottom margins, paddings.


2 Answers

Have you tried experimenting with giving these two divs relative (such as a percentage) widths?

When you float without explicitly declaring a width, either fixed or relative, the dimensions will default to 'auto'. Auto will force the div to be the width of it's content. When the browser shrinks, the content will still force these boxes to that width, until it is forced to collapse by touching another element.

Using auto widths is not the best way to achieve fluidity in your layout. You'll need to specify some kind of relative dimension somewhere, otherwise this problem will be entirely unavoidable.

There are lots of resources out there which can help you achieve a more fluid layout (a lot of articles on www.alistapart.com discuss this in quite some depth).

like image 174
Jon Avatar answered Sep 27 '22 21:09

Jon


CSS's display property, set it to inline and the div will behave like a span.

like image 40
cambraca Avatar answered Sep 27 '22 22:09

cambraca