Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

force width of a column in a table 6

Status
Not open for further replies.

nix45

MIS
Nov 21, 2002
478
US
I have a CGI/Perl script that generates a table with columns like this...

<form method=&quot;POST&quot; <action=&quot;/cgi-bin/stuff.pl&quot;>
<table border=&quot;1&quot; width=&quot;100%&quot;>
<tr>
<td><b>Select</b></td>
<td><b>Number</b></td>
<td><b>From</b></td>
<td><b>To</b></td>
<td><b>Subject</b></td>
<td><b>Reason</b></td>
<td><b>Date/Time</b></td>
</tr>


Sometimes the &quot;From&quot; field will have a long email address inside that will cause that one column to spam the width of the page. How can I force each column to be a certain width regardless of whats inside each box? I've tried specifically setting the width like below, but it doesn't work if there is a single email address (or word) that is longer than the width of the column.

<td width=&quot;50&quot;><b>From</b></td>

Thanks,
Chris
 
Unfortunately, if the string you are placing in your table cell is continuous with no spaces in it (such as an email address - eg [ignore]someone@someone.somthing[/ignore]), the cell will exand to accommodate the contents.

If you have text WITH spaces in it and it is wrapping when you don't want it to, obviously you can use the NOWRAP option on the TD tag. However, there is not much you can do to force wrapping to occur... I believe.

Would it be possible to create each field on a new line, and lay them out vertically rather than horizontally... or are you outputting many results?

For example:
Code:
<form method=&quot;POST&quot; action=&quot;/cgi-bin/stuff.pl&quot;>
  <table border=&quot;1&quot; width=&quot;100%&quot;>
  <tr>
    <td><b>Select:</b> ... <BR>
        <b>Number:</b> ... <BR>
        <b>From:</b> ... <BR>
        <b>To:</b> ... <BR>
        <b>Subject:</b> ... <BR>
        <b>Reason:</b> ... <BR>
        <b>Date/Time</b> ...</td>
  </tr>
  </table></form>

or even:
Code:
<form method=&quot;POST&quot; action=&quot;/cgi-bin/stuff.pl&quot;>
  <table border=&quot;1&quot; width=&quot;100%&quot;>
  <tr><td><b>Select:</b></td><td> ... </td></tr>
  <tr><td><b>Number:</b></td><td> ... </td></tr>
  <tr><td><b>From:</b></td><td> ... </td></tr>
  <tr><td><b>To:</b></td><td> ... </td></tr>
  <tr><td><b>Subject:</b></td><td> ... </td></tr>
  <tr><td><b>Reason:</b></td><td> ... </td></tr>
  <tr><td><b>Date/Time:</b></td><td> ... </td></tr>
  </table></form>

Pete.


Lotus Notes Web Developer / Aptrix (LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Hi guys,

There is an IE workaround, but not sure of any cross browser way.


One way that works in certain circumstances is to replace certain chars in the string with their HTML equivelant, and most browsers will wrap the string at those characters.

Hope this helps

Wullie


The pessimist complains about the wind. The optimist expects it to change. The leader adjusts the sails. - John Maxwell
 
You can set it do overflow: auto; Any content that is larger than the specified width will get scrollbars in order to accomodate it within the cell. This does not work directly on the td, you will need to add another div like this:

<td style=&quot;width: 50px;&quot;><div style=&quot;overflow: auto;&quot;><b>Very long from</b></div></td>
 
Wartookman, I need to lay them out horizontally as there could be anywhere from 1 to 5000 rows.

Wullie, that looks like it could work, but we use others browsers besides IE (IE, Opera, Mozilla, Safari). I looked at the code in the example, but I'm not sure exactly how to write that in the table. Can you please give me an example?

Vragabond, that sounds like it could work, but I'm not sure how to incorporate that into the table. I tried something like this, but it doesn't do anything...

<table border=&quot;1&quot; width=&quot;80px&quot;>
<tr>
<td style=&quot;width: 20px;&quot;><div style=&quot;overflow: auto;&quot;>longasssemailaddresstoolongtofitnormalcolumn@yahoo.com</td>
<td width=&quot;20px&quot;>normal@address.com</td>
<td width=&quot;20px&quot;>stuff</td>
<td width=&quot;20px&quot;>morestuff</td>
</div>
</tr>


Thanks guys,
Chris
 
Firstly, you forgot the closing [ignore]</div>[/ignore] at the end of the email address...

Pete.


Lotus Notes Web Developer / Aptrix (LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Same result...

<table border=&quot;1&quot; width=&quot;50px&quot;>
<tr>
<td style=&quot;width: 20px;&quot;><div style=&quot;overflow: auto;&quot;>longasssemailaddresstoolongtofitnormalcolumn@yahoo.com</div></td>
<td width=&quot;20px&quot;>normal@address.com</td>
<td width=&quot;20px&quot;>stuff</td>
<td width=&quot;20px&quot;>morestuff</td>
</tr>
</table>


Am I doing something wrong here?

Thanks,
Chris
 
Try - I've added the scrolling &quot;feature&quot; to each entry...

Code:
<table border=&quot;1&quot;>
  <tr align=&quot;center&quot; valign=&quot;top&quot; height=40>
    <td><div style=&quot;height: 100%; width: 50; overflow-x: auto;&quot;>longasssemailaddresstoolongtofitnormalcolumn@yahoo.com</div></td>
    <td><div style=&quot;height: 100%; width: 50; overflow-x: auto;&quot;>normal@address.com</div></td>
    <td><div style=&quot;height: 100%; width: 50; overflow-x: auto;&quot;>stuff</div></td>
    <td><div style=&quot;height: 100%; width: 50; overflow-x: auto;&quot;>morestuff</div></td>
  </tr>
</table>

Pete.


Lotus Notes Web Developer / Aptrix (LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Make that
[tt]
<table border=&quot;1&quot;>
<tr align=&quot;center&quot; valign=&quot;top&quot; height=40>
<td><div style=&quot;height: 100%; width: 50px; overflow-x: auto;&quot;>longasssemailaddresstoolongtofitnormalcolumn@yahoo.com</div></td>
<td><div style=&quot;height: 100%; width: 50px; overflow-x: auto;&quot;>normal@address.com</div></td>
<td><div style=&quot;height: 100%; width: 50px; overflow-x: auto;&quot;>stuff</div></td>
<td><div style=&quot;height: 100%; width: 50px; overflow-x: auto;&quot;>morestuff</div></td>
</tr>
</table>
[/tt]
it seems to make a difference in IE6.

-- Chris Hunt
 
Another thing you might want to change is overflow-x: auto; to overflow: auto; overflow-x is a IE only property and it won't work in other browsers.

Actually, this is the same code I proposed earlier, I just did not have time to test it on anything else than Mozilla before. Mozilla (and Opera) had no problems rendering my original code, but I noticed IE has problems with understanding width: 100%; (the default value for div element) in this case.
Code:
<div style=&quot;height: 100%; width: 50px; overflow: auto;&quot;>longasssemailaddresstoolongtofitnormalcolumn@yahoo.com</div>
This code should convince even IE.
 
Thanks guys, it works great except for one problem. Everything is perfect in Opera and Mozilla, but in IE, in some of the columns, all you see is the scroll bar and thats it. Here's the code I'm using...

<table border=&quot;1&quot;>
<tr>
<td width: 10px><b>Select</b></td>
<td width: 10px><b>Number</b></td>
<td><div style=&quot;width: 200px; overflow: auto;&quot;>From</b></div></td>
<td width: 100px><b>To</b></td>
<td width: 100px><b>Subject</b></td>
<td width: 50px><b>Reason</b></td>
<td width: 50px><b>Date/Time</b></td>
</tr>


As you can see, I only need the overflow in the From column.

Thanks,
Chris
 
Hi mate,

You have not specified a height, so the browser doesn't know you want to show any more:

<td><div style=&quot;width: 200px; height: 30px; overflow: auto;&quot;>From</b></div></td>

Hope this helps

Wullie


The pessimist complains about the wind. The optimist expects it to change. The leader adjusts the sails. - John Maxwell
 
After a lot of tweaking, I finally came up with a combination that the 3 browsers can live together with...

<table border=&quot;1&quot; width=&quot;100%&quot;>
<tr height=100%>
<td style=&quot;width: 10px;&quot;><b>Select</b></td>
<td style=&quot;width: 10px;&quot;><b>Number</b></td>
<td><div style=&quot;height: 40px; width: 200px; overflow: auto;&quot;>From</b></div></td>
<td style=&quot;width: 100px;&quot;><b>To</b></td>
<td style=&quot;width: 250px;&quot;><b>Subject</b></td>
<td style=&quot;width: 50px;&quot;><b>Reason</b></td>
<td style=&quot;width: 50px;&quot;><b>Date/Time</b></td>
</tr>

What a pain in the ___! Everything was perfect in Opera and Mozilla, then I made a change to make it look better in IE, which made it look like crap in Mozilla/Opera. I was going back and forth until I found the happy medium above.

Thanks everyone for the help,
Chris
 
Medic - tried out your code from the FAQ in Netscape Navigator/Communicator 4.73... and it didn't quite work (actually at all). So, I thought, perhaps it is just NN4.73.

But according to Bradsoft's Top Style 3.1, it suggests that the &quot;table-layout:fixed&quot; tag is only supported by IE6, and that it isn't supported by (not even partially):
1. CSS1
2. IE4
3. NS4
4. O7

Pete.


Web Developer / CMS (Aptrix / LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
WartookMan,

I used NN6, IE6, and the latest Opera version to test that code. Thanks for informing me of existing browsers which do not support the style. If only I could get copies of those older browser versions, it might help me tweak and improve the code to support a wider group of browsers.
I'll start looking for those old browser to enhance my FAQ solution.

Thanks,

Medic
 
&quot;table-layout:fixed&quot; is an official CSS2 property - it should work in modern browsers, it probably won't in older ones.

I wouldn't lose too much sleep over it - hardly anybody uses v4 browsers any more, and those that do will still see your table, it just won't have its size constrained as you would like.


-- Chris Hunt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top