Forum Moderators: not2easy

Message Too Old, No Replies

Getting a <div> to Adapt to Content

A perennial issue

         

cmarshall

8:13 pm on Apr 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Folks,

One of the biggest issues I have that prevents me from using <div> elements in place of <table><tr><td> elements is that a <div> reacts to its context, not its content, while a <table> reacts to its content.

This means that a <div> will always be 100%, or whatever specified width it is assigned, while a <table> will try to be whatever you assign, except when its content decides otherwise.

The most obvious incarnation of the <div> problem is the hideous "<div> overflow" that so many sites these days display.

My question is this: Is there a viable* way to get a <div> to react to its content? Can you make a <div> shrink to accommodate its content, as does a <table>? Can you make a <div> stretch when it has a big fat piece of content (like a big <img> or alotofrunontextwithabsolutelynowhitespacewhatsoeverthatforcesthedivtooverflow)?

Notice that there is no overflow here, because this site uses a <table> layout.

Discuss.

I'm actually very interested in this, as I am at yet another of those crossroads that tends to make me choose to use a <table> instead of a <div>.

*Making a <div> into a bad simulacrum of a <table> by using the dicey display:table CSS properties doesn't count. You're better off using a <table>.

[edited by: cmarshall at 8:18 pm (utc) on April 2, 2007]

londrum

8:17 pm on Apr 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



at the risk of sounding dumb, can't use just set it to display:inline?

cmarshall

8:21 pm on Apr 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We need the block-level elements. Inline gives up a serious amount of control.

The case I have now is a display of tabular data. In most cases, a <table> layout is the best solution, but this will also be getting output as WML (cWAP) and XHTML-MP, so <div> or <p> layout is a bit better for layout flow.

However, you have a good point, and I should experiment a bit more.

londrum

8:52 pm on Apr 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



the {display} property can also be set to things like:
inline-block, inline-table, and table-cell

i can't say i've ever used them myself, so i don't know what exactly would happen. but maybe in there somewhere is your solution?

Dabrowski

9:06 pm on Apr 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unfortunately support for these display types is sketchy in IE.

I have tried tableless layouts and found I can't for the same reason. I am working on a combination of TABLE/DIV with a little touch of JS to make it work. So far working very well, I'll publish my results when I'm done in the next few days hopefully.

But the answer is, 'display: inline' but as it was rightly pointed out, this option isn't really viable, unless you start using container DIVs but then you end up with more markup than if you'd just used a table in the first place.

Enough buts yet?

Setek

1:52 am on Apr 3, 2007 (gmt 0)

10+ Year Member



If it's doable within your surrounding layout, you could try floating it and setting a maximum width of 100%—obviously with the maximum width javascript expression for IE 6.

That's probably the best generic solution I could think of—I don't think using funky positioning would work in as many different scenarios, so it's not worth mentioning.