473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Maximum width

Is it possible, with CSS, to determine a certain _maximum_ width for a
text, paragraph, division, etc.?

Apr 15 '07 #1
4 5074
p.********@suomi24.fi wrote in
news:11*********************@d57g2000hsg.googlegro ups.com:
Is it possible, with CSS, to determine a certain _maximum_ width
for a text, paragraph, division, etc.?
Only by going round the houses, and defining a max width by
constraining it to a certain max width due to the surrounding elements.
i.e. You cannot set a max width on a specific container, only an
absolute width. 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.

Rob Kerr
--
"It's impossible for an Englishman to open his mouth without making
some other Englishman despise him."
-- G.B.S., "Pygmalion"
Apr 15 '07 #2
Scripsit Rob Kerr:
p.********@suomi24.fi wrote in
news:11*********************@d57g2000hsg.googlegro ups.com:
>Is it possible, with CSS, to determine a certain _maximum_ width
for a text, paragraph, division, etc.?

Only by going round the houses, and defining a max width by
constraining it to a certain max width due to the surrounding
elements.
Huh? What are you talking about?
You cannot set a max width on a specific container,
only an absolute width.
Please check CSS manuals and references.
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.

The short answer to the original question is that you can set max-width for
a block element, and this is well-supported except by IE. On IE, there is no
support before IE 7, and on IE 7, the support has some bugs. But in simple
cases, max-width is fine, if you are not too worried about the fact that IE
users will see the width unlimited unless they have already upgraded to IE
7. Oh, and you need to make sure your page is treated in Standards Mode;
this might be nontrivial if it's an old, poorly coded page.

Example:

body { max-width: 40em; }

There are ways to overcome the limitations (i.e. to set a maximum width in a
manner that works on IE 6 and older, too), but they involve some trickery
(like an auxiliary single-cell table).

--
Jukka K. Korpela ("Yucca")
http://www.cs.tut.fi/~jkorpela/

Apr 15 '07 #3
On 2007-04-15, Jukka K. Korpela <jk******@cs.tut.fiwrote:
Scripsit Rob Kerr:
>p.********@suomi24.fi wrote in
news:11*********************@d57g2000hsg.googlegr oups.com:
>>Is it possible, with CSS, to determine a certain _maximum_ width
for a text, paragraph, division, etc.?

Only by going round the houses, and defining a max width by
constraining it to a certain max width due to the surrounding
elements.

Huh? What are you talking about?
>You cannot set a max width on a specific container,
only an absolute width.

Please check CSS manuals and references.
>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. Remember the computed value for a shrink-to-fit
width is min(max(content min, available), content preferred) where
"content min" is the width of the longest unbreakable sequence in the
content, and "content preferred" is its width with no line breaks at
all (except where they're explicitly asked for).

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>

If we add more words to the inner div, its width will increase but not
go past 400px (unless it contains a single "word", or unbreakable
sequence, longer than 400px).
Apr 15 '07 #4
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/

Apr 16 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: abracad | last post by:
Is there a way to force a maximum width for an image, table or table cell? cheers
6
by: Henro V | last post by:
And if there is one, is there a way to make forms that are wider? For every day on the form (first field) it needs to be followed by 52(!) datefields. I've widened the form as far as I can but I...
10
by: fjleon | last post by:
Hi, i am stuck using older ASP and have to dinamically fill a converted-word file as a form. Let´s suppose a row on a table has the name of someone like in a declaration: <span>Some random...
3
by: mrpeter05 | last post by:
I can not find a way to measure the height of a string by setting a maximum width! I need the strings maximum width to be the width of the screen and want to use that to calculate the height. Is...
2
by: RKSpangler | last post by:
Greetings: Is there any way to determine the maximum width of all data elements in a particular column in a table? Sorting a column won't do it, of course, since it will use a collating...
4
Johanus Dagius
by: Johanus Dagius | last post by:
I've written a lot of C programs but only a few in C++. In C you can set both the minimum and maximum width of a formatted I/O output by using standard printf() formats. For example, if I have a very...
3
by: =?iso-8859-1?q?Jean-S=E9bastien?= | last post by:
hello, is it possible to resize an image with same proportion. having a maximum height and a maximum width. the solution should work on main browsers (do not use max-height and max-width). ...
53
by: Gianni Mariani | last post by:
Do you have a preference on maximum line width for C++ code? I've seen the craziest debates on this most silly of topic. I have witnessed engineers spent oodles of time fiddling with line...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.