473,499 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to do a multicolumn layout where the columns size to fit the contents?

Hello,

Sorry if this is covered somewhere, but I've looked at countless sites
explaining how to do multicolumn layouts in CSS, but have yet to find
one that does what I want.

In the old days, I would use something like this (deliberately small
fragment of air code, so no comments about validity please!!) to produce
a two-column layout, where the left column was (say) for site links...

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top">
<small>
<a href="...">Ferrets</a>
<br><a href="...">Gibbons</a>
<br><a href="...">Voles</a>
</small>
</td>
<td align="left" valign="top">
This is where the main content goes...
</td>
</tr>
</table>

The advantage of this is that the left column would only be wide enough
to fit the links, and the right column would expand to fill the rest of
the window (assuming it had enough content of course).

I have been looking to do such a layout in CSS, but can't see how. All
the examples I've seen use DIVs for the columns, and float them. They
set the width of the left DIV either as a fixed value in pixels, ems,
etc, or as a percentage. Either way, the left column's width is
independent of the contents. If the text of the links are all short,
then you waste space in the left column. If there are long texts, they
overflow the DIV and spill into the right column.

Is there any way to set it so that the left column is only as wide as
needed by the contents, and the right one uses the rest? As I said, I've
searched and searched, I've followed loads of links from posts in this
group, but I've yet to find a layout that is as flexible as the old
table one. I would be very grateful if anyone can point me in the right
direction.

TIA

--
Alan Silver
(anything added below this line is nothing to do with me)
Jan 24 '06 #1
14 1910
Alan Silver wrote:

The advantage of this is that the left column would only be wide enough
to fit the links, and the right column would expand to fill the rest of
the window (assuming it had enough content of course).

I have been looking to do such a layout in CSS, but can't see how. All
the examples I've seen use DIVs for the columns, and float them. They
set the width of the left DIV either as a fixed value in pixels, ems,
etc, or as a percentage. Either way, the left column's width is
independent of the contents. If the text of the links are all short,
then you waste space in the left column. If there are long texts, they
overflow the DIV and spill into the right column.

<http://css-discuss.incutio.com/>
<http://www.csszengarden.com/>
<http://www.positioniseverything.net/>
<http://css.maxdesign.com.au/>

--
jmm (hyphen) list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)
Jan 25 '06 #2
Hmm, it seems my reply of two days ago never made it into the newsgroup.
Here goes for a second try...
Thanks for your links Jim, but I've been through those already, and
unless I missed one, none of them do what I want. They all have either
fixed width, or a fixed percentage. I'm looking for a way to have the
left column (DIV) only take as much space as the contents need, and have
the right column take the rest.

If I missed a sample that does that, please could you provide a more
specific link as I couldn't see one.
<http://css-discuss.incutio.com/>
Great site, loads of sample, but all suffer from the problem mentioned.
<http://www.csszengarden.com/>
Beautiful site, but almost every design fixes the font size and the
width.
<http://www.positioniseverything.net/>
Also a great site, but see comments above
<http://css.maxdesign.com.au/>


I presume you were referring to the floatutorial. Again, AFAICS, no
example of automatic sizing.

It could be that CSS simply isn't up to this yet and I'm going to have
to stick with tables a while longer. If anyone has any idea, please
reply. TIA.

--
Alan Silver
(anything added below this line is nothing to do with me)
Jan 26 '06 #3
Alan Silver schrieb:
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top">
<small>
<a href="...">Ferrets</a>
<br><a href="...">Gibbons</a>
<br><a href="...">Voles</a>
</small>
</td>
<td align="left" valign="top">
This is where the main content goes...
</td>
</tr>
</table> .... Is there any way to set it so that the left column is only as wide as
needed by the contents, and the right one uses the rest?
Your table-solution doesn't fit your description, the link-column
is maybe at about 20% for me. Try to understand floating and div and
you get exactly what you want:

<div>
<!-- floating always reduces the div to the minimum needed size! it's
automatically 'display: inline;' -->
<div style="float: left;">
<small>
<a href="...">Ferrets</a><br/>
<a href="...">Gibbons</a><br/>
<a href="...">Voles</a>
</small>
</div>
<!-- nothing always expands the div to the maximum available size!
that is the default behaviour of every unstyled div -->
<div style="">
This is where the main content goes...
</div>
<!-- stop making garbage with the following content -->
<div style="clear: both;" />
</div>
abc
TIA


Niels

Jan 26 '06 #4
Alan Silver wrote:

Thanks for your links Jim, but I've been through those already, and
unless I missed one, none of them do what I want. They all have either
fixed width, or a fixed percentage. I'm looking for a way to have the
left column (DIV) only take as much space as the contents need, and have
the right column take the rest.

Hmm. There is the float suggestion by Niels. That would work.
You could also try display: table-cell. I do not know if IE supports
that, though.
--
jmm (hyphen) list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)
Jan 26 '06 #5
<snip>
Is there any way to set it so that the left column is only as wide as
needed by the contents, and the right one uses the rest?
Your table-solution doesn't fit your description, the link-column
is maybe at about 20% for me.


If you add more text to the main content part then it will shrink the
link column down to just the size needed. I didn't bother adding that
in, so as not to fill the post with unnecessary text.
Try to understand floating and div and
you get exactly what you want:

<snip>

Thanks for the sample, it isn't bad, but isn't actually what I wanted.

The sample I posted (with the table) would have a left column that is as
long as the right column. The one you posted floats the left column at
the top left of the containing DIV, allowing the contents of the main
column to expand out under the links. I was looking for something that
would mimic the table layout, where the two columns fill the full
vertical space used.

I tried adding style="float:right;" to the main content div, which gave
something a lot closer to what I wanted in IE, but put the div *below*
the links on in Firefox, so still no joy ;-(

Thanks for the reply. If you have any further suggestions, please do so.

--
Alan Silver
(anything added below this line is nothing to do with me)
Jan 26 '06 #6
>> Thanks for your links Jim, but I've been through those already, and
unless I missed one, none of them do what I want. They all have either
fixed width, or a fixed percentage. I'm looking for a way to have the
left column (DIV) only take as much space as the contents need, and have
the right column take the rest.
Hmm. There is the float suggestion by Niels. That would work.


Unfortunately not, see my reply to him.
You could also try display: table-cell. I do not know if IE supports
that, though.


I just did a quick search, and apparently it doesn't.

Thanks for the reply anyway. Any further suggestions greatly welcome.

--
Alan Silver
(anything added below this line is nothing to do with me)
Jan 26 '06 #7
ni*************@seies.de wrote:

<!-- floating always reduces the div to the minimum needed size!


FYI, if the browser follows the CSS 2.0 spec, an explicit width is
required for non-replaced elements. It will default to 100% width if
none is specified. MacIE falls into this category. Most current browsers
follow the 2.1 spec, which allows for "shrink-wrapping".

I realize MacIE is a dead browser, but it is an exception to your
"always" rule. A graceful fallback for non-supporting browsers is always
A Good Thing.

--
Reply email address is a bottomless spam bucket.
Please reply to the group so everyone can share.
Jan 26 '06 #8
> I tried adding style="float:right;" to the main content div, which gave
something a lot closer to what I wanted in IE, but put the div *below*
the links on in Firefox, so still no joy ;-(


<div>
<div style="float: left;">
Menu
</div>
<div style="float: right;">
This is where the main content goes...
This is where the main content goes...
</div>
<!-- this little space works wonders ;-) you can
even setup 'font-size: 1px;' for making it smaller
or using another element -->
&nbsp;
<div style="clear: both;" />
abc
</div>

Ciao
Niels

Jan 26 '06 #9
No that was a too fast a shoot. ;)
Niels

Jan 26 '06 #10
Alan Silver <al*********@nospam.thanx.invalid> wrote in
news:8h**************@nospamthankyou.spam:
<http://css.maxdesign.com.au/>


I presume you were referring to the floatutorial. Again, AFAICS, no
example of automatic sizing.

It could be that CSS simply isn't up to this yet and I'm going to
have to stick with tables a while longer. If anyone has any idea,
please reply. TIA.


I have also fought with this one and haven't found a way to do what you
are referring to except for the use of em references. The only way
I've found is to determine the content width of the left column in ems,
then set the width using ems and set the margin of the right column
also using ems. It's not automatic sizing, but it is based upon your
content changing size with font changes.

I know it's not exactly what you're looking for but I do hope this
helps.

--
Stan McCann "Uncle Pirate" http://stanmccann.us/pirate.html
Webmaster/Computer Center Manager, NMSU at Alamogordo
http://alamo.nmsu.edu/ There are 10 kinds of people.
Those that understand binary and those that don't.
Jan 27 '06 #11
>> It could be that CSS simply isn't up to this yet and I'm going to
have to stick with tables a while longer. If anyone has any idea,
please reply. TIA.


I have also fought with this one and haven't found a way to do what you
are referring to except for the use of em references. The only way
I've found is to determine the content width of the left column in ems,
then set the width using ems and set the margin of the right column
also using ems. It's not automatic sizing, but it is based upon your
content changing size with font changes.

I know it's not exactly what you're looking for but I do hope this
helps.


Thanks for the reply, I was afraid this was going to be the answer.

The reason why it's such a problem is that the links can be changed at
will by the site owner, so I have no way of knowing in advance what
length the link text will be. With the table layout, this didn't matter
as the links column would just take as much space as it needed and no
more. With a CSS approach, it looks like I would have to guess, which is
a certainty that I would get it wrong fairly often!!

Thanks anyway.

--
Alan Silver
(anything added below this line is nothing to do with me)
Jan 29 '06 #12
>No that was a too fast a shoot. ;)

Ah, that explains why it didn't work <g>

Thanks anyway.

--
Alan Silver
(anything added below this line is nothing to do with me)
Jan 29 '06 #13
Okay, here is the merge, FF table-cell, conditionally IE (only tested
with 6SP1 win):

<html>
<head>
<style type="text/css">div.colright { display: table-cell; }
div.colleft { float: left; }</style><!--[if lt IE 7]>
<style type="text/css">div.colright { display: block; float: left;
}</style><![endif]-->
</head>
<body>
<div>
<div class="colleft">
<small>
<a href="...">Ferrets</a><br/>
<a href="...">Gibbons</a><br/>
<a href="...">Voles</a>
</small>
</div>
<div class="colright">
This is where the main content goes... </div>
&nbsp;
<div style="clear: both;" />
</div>
</body>
</html>

Works just fine and maybe continue working with IE7 (not Dean's :)

Ciao
Niels

Jan 30 '06 #14
>Okay, here is the merge, FF table-cell, conditionally IE (only tested
with 6SP1 win):
That's great, thanks. I tested it with IE5.0 and it worked as well.

OK, one more question!! Maybe I'm asking too much now, but it is
possible to set the height of the links column to be the same as the
main content column? I would like to do this so that I can set the
background colour of the links column, and have the colour go all the
way down the page.

I tried adding "height:100%;" to the colleft style, but it didn't do
anything. I then surrounded both columns with another div, thinking that
the extra style would expand the height of colleft to the full height of
the container (ie the new div), but it didn't work.

Any ideas? Thanks a lot for the help so far.
<html>
<head>
<style type="text/css">div.colright { display: table-cell; }
div.colleft { float: left; }</style><!--[if lt IE 7]>
<style type="text/css">div.colright { display: block; float: left;
}</style><![endif]-->
</head>
<body>
<div>
<div class="colleft">
<small>
<a href="...">Ferrets</a><br/>
<a href="...">Gibbons</a><br/>
<a href="...">Voles</a>
</small>
</div>
<div class="colright">
This is where the main content goes... </div>
&nbsp;
<div style="clear: both;" />
</div>
</body>
</html>

Works just fine and maybe continue working with IE7 (not Dean's :)

Ciao
Niels


--
Alan Silver
(anything added below this line is nothing to do with me)
Jan 30 '06 #15

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

Similar topics

47
9076
by: Neal | last post by:
Patrick Griffiths weighs in on the CSS vs table layout debate in his blog entry "Tables my ass" - http://www.htmldog.com/ptg/archives/000049.php . A quite good article.
7
3651
by: Drew Martin | last post by:
The page at the below URL renders perfectly in IE6, but is failing horribly in Firefox (0.7, 0.8). I can't figure out why and was hoping someone could take a quick look at it. Most of the layout...
5
3051
by: Thomas F.O'Connell | last post by:
We've got a table that has a definition as follows: CREATE TABLE linking_table ( fk int8 REFERENCES source_table( pk1 ), value int8, PRIMARY KEY( fk1, value ) ); I would've thought that...
7
6108
by: Paul Bromley | last post by:
How can I use this please - I need 2 columns. I have been having difficulty finding info on this and the 2005 Treeview control today. Many thanks for any links or info, Paul Bromley
5
633
by: Stephen Poley | last post by:
There are plenty of people around these groups who promote the idea of flexible page design. However, while employing relative units and not fixing column-widths is a big improvement on...
10
4017
by: Rob | last post by:
VS 2005 How can you tell if a value is contained in a specific column (let's say column 1 named Status) of a ListView ? In a list box you could go... If ListBox1.Items.Contains(strWhatever)...
1
1393
by: brixton | last post by:
Hey, I'm trying to make a 3-column layout with CSS and specifying the width of each column in %, not in px (it's a school thing and I'm required to do this). Say it looks like this: div#col1...
5
17721
by: WRH | last post by:
Hello I want to have a multicolumn listbox. I never used one before so I looked at a Help example. I set the multicolumn property and the column width and tested with this example... ...
3
5620
balabaster
by: balabaster | last post by:
Hey guys and gals, I'm not sure if this is the simplest way to do this or if someone can throw any other ideas out there. I'm looking to build a treeview inside a listbox...or have a multicolumn...
0
7178
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
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6899
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
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.