tags:

views:

35

answers:

3

I'm trying to adjust a CSS page layout using min-width and max-width. To simplify the problem, I made this test page. I'm trying it out in the latest versions of Firefox and Chrome with the same results.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <head>
    <title>Testing min-width and max-width</title>
    <style type="text/css">
       div{float: left; max-width: 400px; min-width: 200px;}
       div.a{background: orange;}
       div.b{background: gray;}
    </style>
  </head>
  <body>
    <div class="a">
      (Giant block of filler text here)
    </div>
    <div class="b">
      (Giant block of filler text here)
    </div>
  </body>
</html>

Here's what I expect to happen:

  • With the browser maximized, the divs sit side by side, each 400px wide: their maximum width
  • Shrink the browser window, and they both shrink to 200px: their minimum width
  • Further shrinking the browser has no effect on them

Here's what actually happens, starting at step 2:

  • Shrink the browser window, and as soon as they can't sit side-by-side at their max width, the second div drops below the first
  • Further shrinking the browser makes them get narrower and narrower, as small as I can make the window

So here's are my questions:

  • What does max-width mean if the element will sooner hop down in the layout than go lower than its maximum width?
  • What does min-width mean if the element will happily get narrower than that if the browser window keeps shrinking?
  • Is there any way to achieve what I want: have these elements sit side-by-side, happily shrinking until they reach 200px each, and only then adjust the layout so that the second one drops down?

And of course...

What am I doing wrong?

+1  A: 

The reason they're "dropping down" is due to the changing size of their parent element (in this case body). Put a wrapper div around them with a width of 400 pixels or more and you can keep them sitting side-by-side.

Amber
Wrapped a div around them both and gave it one rule: `width: 900px;`. Now they don't ever sit side by side. Tried adding a left float in case it was collapsing. No change.
Nathan Long
+1  A: 

I think (and this is just from my own personal experience), that max-width applies to the content inside the div. So if there was only 1 word inside the div, it would be 200px wide but if there were 300 words in a div, the width would be 400px wide.

I think there is a way to do what you want to do. This article might relate to it:

http://net.tutsplus.com/tutorials/design-tutorials/quick-tip-different-layouts-for-different-widths/

Good luck.

codedude
A: 

To answer at least a bit:

min-width really is about when the browser should show a horizontal scroll bar for the page.

Whilst experimenting floats only really work well when they have a width set, so I'd use a parent div with a min and max width set and add width: 50% to the child divs. This solves all your problems apart from the divs changing layout. For this I'd probably refer to javascript.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <head>
    <title>Testing min-width and max-width</title>
    <style type="text/css">
       div.cont {max-width: 700px; min-width: 400px;}
       div.a, div.b {float: left; width: 50%; }
       div.a{background: orange;}
       div.b{background: gray;}
    </style>
  </head>
  <body>
      <div class="cont">
    <div class="a">
      Morbi malesuada nulla nec purus convallis consequat... 
    </div>
    <div class="b">
      Vivamus id mollis quam. Morbi ac commodo nulla... 
    </div>
    </div>
  </body>
</html>
Jakub Hampl