Scripsit Ben C:
>>But you can allow a container to float, and
increase in size until it is prevented by an abutting container.
I'm finding it hard to think of a situation in which such would be
desirable, however.
That sounds _very_ confused.
I think it makes sense.
_Your_ text makes sense, though you are not correct in an essential point,
but the answer I was commenting on was very confused, starting from the
point where it claimed that maximum width cannot be set directly in CSS. (As
I described, there are still serious problems in browser support, but that's
a different issue.)
This effectively gives the float in this example a width of up to
400px, but its width will be much less than that since the width of
"Hello world" is likely to be shorter than 400px:
<div style="width: 400px">
<div style="float: left">
Hello world
</div>
</div>
This approach also makes the width of the outer div exactly 400 pixels, and
that's something that we usually want to avoid when setting _maximum_ width.
There are good reasons for limiting the width of paragraphs by setting a
maximum width, _without_ setting a fixed width. Nominally, your approach
does that for the inner div, but at the cost of setting that fixed width,
which forces horizontal scrolling when the available space is narrower than
400 pixels.
If you want to set a maximum width of 400 pixels for a piece of text without
setting any fixed width (or minimum width) for anything, and you are
prepared to using tricks in order to make things work on IE 6 and older too,
then the old approach of using a single-cell table is still the practical
way:
<table><tr><td width="400">The text goes here</td></tr></table>
You can then add a style="width: ..." attribute into the <tdtag to set a
maximum width in more reasonable units, like em, for CSS-enabled browsers.
Granted, this will set the width to exactly 400 pixels (or the width
property setting), if there is available space. This should not be a
problem, if the purpose is just to limit the line length in paragraphs - the
good and common cause - or something similar. You normally don't even see
what the width is, unless you have a border or a background (and if that's a
problem, use an inner <spanelement and set border and background for it
rather than the cell). The point is that due to the way tables are handled
in graphic browsers, the width automatically becomes smaller than declared
if there is not enough room.
This approach implies that the text box will be wider than you set, if there
are long strings no line break points recognized by the browser. But this is
a separate issue and should be treated separately anyway.
--
Jukka K. Korpela ("Yucca")
http://www.cs.tut.fi/~jkorpela/