473,385 Members | 1,396 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,385 software developers and data experts.

DIV that auto-sizes?

Hello,
Is there a way to have a div size itself to the width of its contents?
"width:auto" does not work.
The div contains a UL and I want the div to fit the longest line in the
list. The div is centered.

--
jmm dash list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)
Jul 21 '05 #1
22 39580
Jim Moe <jm***************@sohnen-moe.com> wrote:
Is there a way to have a div size itself to the width of its contents?
The table-cell, inline-block or inline-table values for the display
property would achieve that, browser support varies.
"width:auto" does not work.


It's not supposed to.

--
Spartanicus
Jul 21 '05 #2
Jim Moe wrote:
Hello,
Is there a way to have a div size itself to the width of its contents?
"width:auto" does not work.
The div contains a UL and I want the div to fit the longest line in
the list. The div is centered.

read a few postings back, 26 jan
http://groups-beta.google.com/group/...ab59977f6cd143

could help you
Jul 21 '05 #3
On Sat, 29 Jan 2005 08:19:27 -0700 Jim Moe wrote:
Hello,
Is there a way to have a div size itself to the width of its
contents?
"width:auto" does not work.
The div contains a UL and I want the div to fit the longest line in
the
list. The div is centered.

<div id="one">
<ul>
<li><div id="two"><text></div></li>
</ul>
</div>

div#one {width:800px;}
div#two {width:100%; text-align:center;}

Division one is the container for your UL.
Division two is set at 100% so it will also be 800px wide.
To indent a little: div#two { margin-left:5%; width:95%;}
To center it div#two {margin-left:5%; margin-right:5% width:90%;}
And of course, div#two {margin:5%; width:90%}
Jul 21 '05 #4
"Richard" <An*******@127.001> wrote:
On Sat, 29 Jan 2005 08:19:27 -0700 Jim Moe wrote:
Is there a way to have a div size itself to the width of its
contents?


div#one {width:800px;}


So width: 800px means size itself to the width of the contents? Try to
at least read the question before posting your drivel.

Steve

--
"My theories appal you, my heresies outrage you,
I never answer letters and you don't like my tie." - The Doctor

Steve Pugh <st***@pugh.net> <http://steve.pugh.net/>
Jul 21 '05 #5
Steve Pugh <st***@pugh.net> writes:
"Richard" <An*******@127.001> wrote:
On Sat, 29 Jan 2005 08:19:27 -0700 Jim Moe wrote:
Is there a way to have a div size itself to the width of its
contents?
So width: 800px means size itself to the width of the contents? Try to
at least read the question before posting your drivel.


But the question remains, and for me is an interesting one, although
in my case, I'd like the div vertical size to adjust itself to the
amount of a fixed-width text, so that if more text is entered, the
vertical size of the div will increase.

--
Haines Brown

Jul 21 '05 #6
Haines Brown <br****@teufel.hartford-hwp.com> wrote:
Steve Pugh <st***@pugh.net> writes:
"Richard" <An*******@127.001> wrote:
>On Sat, 29 Jan 2005 08:19:27 -0700 Jim Moe wrote:
>
>> Is there a way to have a div size itself to the width of its
>> contents?
So width: 800px means size itself to the width of the contents? Try to
at least read the question before posting your drivel.


But the question remains,


As other people have said there are several ways to create 'shrink to
fit' blocks in CSS. CSS tables are the best way but IE doesn't support
them. Under CSS 2.1 floats also behave like this but not under CSS 2
(and Mac IE follows the CSS 2 rules).
and for me is an interesting one, although
in my case, I'd like the div vertical size to adjust itself to the
amount of a fixed-width text, so that if more text is entered, the
vertical size of the div will increase.


Um, that's the default.

Steve

--
"My theories appal you, my heresies outrage you,
I never answer letters and you don't like my tie." - The Doctor

Steve Pugh <st***@pugh.net> <http://steve.pugh.net/>
Jul 21 '05 #7
Haines Brown wrote:
Steve Pugh <st***@pugh.net> writes:

"Richard" <An*******@127.001> wrote:
On Sat, 29 Jan 2005 08:19:27 -0700 Jim Moe wrote:
Is there a way to have a div size itself to the width of its
contents?


So width: 800px means size itself to the width of the contents? Try to
at least read the question before posting your drivel.

But the question remains, and for me is an interesting one, although
in my case, I'd like the div vertical size to adjust itself to the
amount of a fixed-width text, so that if more text is entered, the
vertical size of the div will increase.


is this a jukebox or what ?! apparently it is. as i just said, the
solution was given just 3 days ago. so for the very hesitating or simply
mentally poor, follow the link, and check post #26 (in google numbering)
Jul 21 '05 #8
Spartanicus wrote:
Is there a way to have a div size itself to the width of its contents?


The table-cell, inline-block or inline-table values for the display
property would achieve that, browser support varies.

Only by setting both the div and ul to display:table-cell did the
shrink to fit work. Only now the div completely ignores the margin:auto.
:-( And, of course, it looks differently in IE and Mozilla.

Demo URL: <http://www.sohnen-moe.com/test/test.html>
--
jmm dash list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)
Jul 21 '05 #9
Ali Babba wrote:
Is there a way to have a div size itself to the width of its
contents? "width:auto" does not work.
The div contains a UL and I want the div to fit the longest line in
the list. The div is centered.


read a few postings back, 26 jan
http://groups-beta.google.com/group/...ab59977f6cd143

Yes, I saw that. It does not work for a contained UL.

--
jmm dash list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)
Jul 21 '05 #10
Jim Moe wrote:
Only by setting both the div and ul to display:table-cell did the
shrink to fit work. Only now the div completely ignores the margin:auto.
:-( And, of course, it looks differently in IE and Mozilla.


Try text-align:center on a container instead of margin:auto on the div.
You've changed the div from block to in-line and they work
differently. You might try putting &nbsp; around the element if it
doesn't respond to the text-align:center.

We'll never get past the IE/other thing though.

--
Stan McCann "Uncle Pirate" http://stanmccann.us/pirate.html
Webmaster/Computer Center Manager, NMSU at Alamogordo
Coordinator, Tularosa Basin Chapter, ABATE of NM; AMA#758681; COBB
'94 1500 Vulcan (now wrecked) :( http://motorcyclefun.org/Dcp_2068c.jpg
A zest for living must include a willingness to die. - R.A. Heinlein
Jul 21 '05 #11
On Sat, 29 Jan 2005 22:11:30 GMT Haines Brown wrote:
Steve Pugh <st***@pugh.net> writes:
"Richard" <An*******@127.001> wrote:
>On Sat, 29 Jan 2005 08:19:27 -0700 Jim Moe wrote:
>
>> Is there a way to have a div size itself to the width of its
>> contents?
So width: 800px means size itself to the width of the contents? Try to
at least read the question before posting your drivel.

But the question remains, and for me is an interesting one, although
in my case, I'd like the div vertical size to adjust itself to the
amount of a fixed-width text, so that if more text is entered, the
vertical size of the div will increase.


Do not define a height for either division.
Although you can define a height for div#two initially. Just to show
something.
The height will automatically adjust as needed.
If you have nothing in the division to begin with, the division will appear
to be nonexistant.
As soon as something is in it, it expands as needed.
The width remains the same as you have defined that.

Jul 21 '05 #12
Jim Moe <jm***************@sohnen-moe.com> wrote:
Is there a way to have a div size itself to the width of its contents?
The table-cell, inline-block or inline-table values for the display
property would achieve that, browser support varies.

Only by setting both the div and ul to display:table-cell did the
shrink to fit work. Only now the div completely ignores the margin:auto.
:-(


Table cells can't have margins, tables can (use display:table).
And, of course, it looks differently in IE and Mozilla.


The only way around that is to code it as an inline-block for IE, and a
css table for proper browsers:
http://www.spartanicus.utvinternet.ie/test/jim.htm

--
Spartanicus
Jul 21 '05 #13
In article <cu********************@giganews.com>,
Jim Moe <jm***************@sohnen-moe.com> wrote:
Hello,
Is there a way to have a div size itself to the width of its contents?
"width:auto" does not work.
The div contains a UL and I want the div to fit the longest line in the
list. The div is centered.


I've not tried grappling with ULs like that, but with headings I find
things like width: 0em; white-space: nowrap; will usually do what I
want in IE6 and Firefox.

-- Mark
Jul 21 '05 #14
Spartanicus wrote:
Is there a way to have a div size itself to the width of its contents?

The table-cell, inline-block or inline-table values for the display
property would achieve that, browser support varies.

Only by setting both the div and ul to display:table-cell did the
shrink to fit work. Only now the div completely ignores the margin:auto.


Table cells can't have margins, tables can (use display:table).

display:table works quite nicely with Mozilla. Not IE, as you noted.
Your suggestion of special IE conditionals is an interesting workaround
to the browser's limitations. One I am not willing to have cluttering my
pages.
So I have decided, "Screw the Purity Of Essence, I am using a table for
layout." A table is standard, and it works for existing browsers (IE, Moz,
Opera). In 10 years when IE catches up to trailing edge tech and IE6 is
hardly used, I'll remove the table.

--
jmm dash list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)
Jul 21 '05 #15
On Sun, 30 Jan 2005, Jim Moe wrote:
So I have decided, "Screw the Purity Of Essence, I am using a
table for layout." A table is standard, and it works for existing
browsers


And that's *exactly* what's wrong with using tables for layout. They
"work" - in the sense of imposing the author's idea of a suitable
layout for a limited range of browsing situations - even when that
layout is counter-productive. And browsers can't, in general, know
whether the table is meant to be expressing an inherent relationship
between the cells (in which case the best compromise may be to retain
the table layout, scrollbars and all), or merely a suggestion which
would be better ignored when the browsing situation could benefit from
that.

Jul 21 '05 #16
Alan J. Flavell wrote:
So I have decided, "Screw the Purity Of Essence, I am using a
table for layout." A table is standard, and it works for existing
browsers


And that's *exactly* what's wrong with using tables for layout. They
"work" - in the sense of imposing the author's idea of a suitable
layout for a limited range of browsing situations - even when that
layout is counter-productive.

HTML+CSS is supposed to assist in "imposing the author's idea of a
suitable layout," not prohibit it.
It is not the layout which is counter-productive; it is the lack of
multi-browser support which, I suppose, could be considered a "limited
range of browsing situations." I had no success using CSS to convince the
browsers to treat a block of formatted text (the UL) as a block.
So it came to: "Which ugly hack should I use?" I chose one that at
least conforms to (generally supported) standards rather than a
browser-specific one.

--
jmm dash list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)
Jul 21 '05 #17
On Mon, 31 Jan 2005, Jim Moe wrote:
And that's *exactly* what's wrong with using tables for layout.
They "work" - in the sense of imposing the author's idea of a
suitable layout for a limited range of browsing situations - even
when that layout is counter-productive.
HTML+CSS is supposed to assist in "imposing


"Proposing", yes. "Imposing", no. Confucius say "force does not work
on the WWW". (Well, he would've done, I reckon...)
the author's idea of a suitable layout," not prohibit it.
Eh?
It is not the layout which is counter-productive; it is the lack
of multi-browser support
CSS is optional, by design. In situations where the proposals make no
sense (for example, text colour on a speaking browser) the CSS
proposals are going to be ignored anyway. By grasping that principle,
and capitalising on it, more-flexible and accessible web designs
emerge.
So it came to: "Which ugly hack should I use?"
Does it? To me it comes down to "can I really risk my substantive
content to the vagaries of browser behaviour for the sake of this
particular design feature - maybe it would be safer to do without
it?".
I chose one that at least conforms to (generally supported)
standards


Table layout violates at least one important "standard": the WAI
guidelines.
Jul 21 '05 #18
Alan J. Flavell wrote:
"Proposing", yes. "Imposing", no. Confucius say "force does not work
on the WWW". (Well, he would've done, I reckon...)
Mr Lee or Mr Wang?
Table layout violates at least one important "standard": the WAI
guidelines.


In fact, WCAG 1.0 allows using HTML tables for layout unless they don't
make sense when linearized. HTML 4 says, that you should not use tables
purely for layout.
--
Johannes Koch
In te domine speravi; non confundar in aeternum.
(Te Deum, 4th cent.)
Jul 21 '05 #19
On Mon, 31 Jan 2005, Johannes Koch wrote:
Table layout violates at least one important "standard": the WAI
guidelines.


In fact, WCAG 1.0 allows using HTML tables for layout unless they
don't make sense when linearized.


Let's have that in context! In fact, WCAG 1.0 section 5 says:

Tables should be used to mark up truly tabular information ("data
tables"). Content developers should avoid using them to lay out pages
("layout tables").

I think that's clear enough.

Item 5.3 conceded, at the time it was written:

5.3 Do not use tables for layout unless the table makes sense when
linearized. [...]
Note. Once user agents support style sheet positioning, tables
should not be used for layout.

Those guidelines were developed prior to their publication in May
1999. Some years have passed since then! If there's any argument to
be made, it's about the precise meaning of "Once user agents support
style sheet positioning", and whether that time has yet come.
"Positioning" in the sense of floating is surely working already, and
has been for quite some time, in browsing situations where it makes
any sense, and it does little harm in situations where it's not
supported or where it isn't helpful. "Positioning" in the sense of
precise placement on the canvas is tricky, and in the general WWW
context is best avoided anyway, IMHO.

So, I say that time is long since here, and the 1999 interim solution
is no longer appropriate (at least for new designs). YMMV.
Jul 21 '05 #20
Alan J. Flavell wrote:
Let's have that in context! In fact, WCAG 1.0 section 5 says:

Tables should be used to mark up truly tabular information ("data
tables"). Content developers should avoid using them to lay out pages
("layout tables").

I think that's clear enough.
ACK
Item 5.3 conceded, at the time it was written:

5.3 Do not use tables for layout unless the table makes sense when
linearized. [...]
Note. Once user agents support style sheet positioning, tables
should not be used for layout.
You are right.
Those guidelines were developed prior to their publication in May
1999. Some years have passed since then!


Unfortunately, some WAI people still cited this (5.3) very strongly in
February 2004 to show that WCAG 1.0 allow layout tables.
--
Johannes Koch
In te domine speravi; non confundar in aeternum.
(Te Deum, 4th cent.)
Jul 21 '05 #21
Alan J. Flavell wrote:

HTML+CSS is supposed to assist in "imposing
"Proposing", yes. "Imposing", no. Confucius say "force does not work
on the WWW". (Well, he would've done, I reckon...)
the author's idea of a suitable layout," not prohibit it.


Eh?

Hey, they are *your* words.
Once again, it is a matter of practicality, of choosing the change the
design to conform to browser limitations, or using what browsers currently
support now (and in the future) to implement the desired design.
I chose one that at least conforms to (generally supported)
standards


Table layout violates at least one important "standard": the WAI
guidelines.


Guidelines vs standards. Hmm. . .

--
jmm dash list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)
Jul 21 '05 #22
On Mon, 31 Jan 2005, Jim Moe wrote:
Table layout violates at least one important "standard": the WAI
guidelines.


Guidelines vs standards. Hmm. . .


It's OK, I know what you're trying to do. A process of attrition over
terminology, in the hope of losing sight of the issue. You're doing
fine with that so far.

Try to get this straight. The only formal /standard/ you'll find,
anywhere near this context, is ISO HTML.

After that, you're left with Technical Recommendations, such as
http://www.w3.org/TR/html401/struct/tables.html

"Tables should not be used purely as a means to layout document
content" [...]

, and Guidelines such as the WAI.

Sure: in the end, it's your choice what you do with those, as an
author. And it's your readers' choice what they'll do with what you
authored. We just discuss some of the issues which might arise.
Jul 21 '05 #23

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

Similar topics

2
by: Manlio Perillo | last post by:
Hi. This post follows "does python have useless destructors". I'm not an expert, so I hope what I will write is meaningfull and clear. Actually in Python there is no possibility to write code...
1
by: Glabbeek | last post by:
I'm changing the layout of my site. Instead of using tables, I will use DIVs. It's working fine, except for 1 thing: In IE6 some DIVs are not the correct width. Mozilla and Opera are showing the...
5
by: Robert Downes | last post by:
I'm using the following in a page that I'm testing in Mozilla: p.actionLinkBlock {border: 1px #000000 dashed; padding: 0.2cm; width: auto} But the dashed border is extending to the right-edge...
20
by: Vijay Kumar R. Zanvar | last post by:
Hello, Unlike register, auto keyword can not be used to declare formal parameter(s). Is there any specific reason for this? Kind regards, Vijay Kumar R. Zanvar
6
by: Alpha | last post by:
I retrieve a table with only 2 columns. One is a auto-generated primary key column and the 2nd is a string. When I add a new row to the dataset to be updated back to the database. What should I...
5
by: Samuel | last post by:
Hi, I am running into a problem of mixing UICulture = auto and allowing users to select culture using a dropdown list. I am detecting a querystring, "setlang", and when found, setting the...
5
by: maya | last post by:
at work they decided to center divs thus: body {text-align:center} #content {width: 612px; text-align:left; margin: 0 auto 0 auto; } this works fine in IE & FF, EXCEPT in FF it doesn't work if...
22
by: nospam_news | last post by:
I currently get asked about my usage of "auto". What is it for? The keyword is clearly superflous here. In contrast to the huge majority of C/C++ developers I write definitions very explicitly...
2
by: Piotr K | last post by:
Hi, I've encountered a strange problem with Firefox which I don't have any idea how to resolve. To the point: I've <divelement with a style "height: auto" and I want to retrieve this value...
21
by: JOYCE | last post by:
Look the subject,that's my problem! I hope someone can help me, thanks
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.