Forum Moderators: open

Message Too Old, No Replies

Stop Table Stretching when Browser Window Is Enlarged

         

GetReal

8:36 pm on Dec 19, 2009 (gmt 0)

10+ Year Member



I’ve defined a table:

<table width="200" height="679" border="0" cellpadding="0" cellspacing="0" >
.
.
.
</table>

The problem I’m running into is when I enlarge the browser window; the table wants to stretch out with the browser. So as I drag the browser’s window right to get more real estate, the table wants to grow to the right with the browser. I want to keep this table fixed at 200 px.

Any ideas?

GR

rocknbil

9:23 pm on Dec 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This really makes no sense, since you have it defined at 200 it's going to stay 200. The exception is if you have a cell set to nowrap and the content is wider, this will override the width attribute.

The only other reason I can think of is you have some validation error in the page that's breaking it, run it through the validator.

P.S. I just tested, "it be fine" and fixed at 200. BTW "height" only works in some browsers and is highly unreliable (though CSS seems to work better, for most,) most of the time it's just as well to leave it off.

Supplementary, it's probably advisable to do this instead, with same results:

<table id="my-table" cellspacing="0">
.
.
.
</table>
<style type="text/css">
#my-table { width:200px; height:679px; }
</style>

Cellspacing is needed because there's no attribute that affects it via css (that I know of.)

GetReal

10:19 pm on Dec 19, 2009 (gmt 0)

10+ Year Member



Hi rocknbil,
Thanks for the reply...

I tried you suggestions, but still same result....

The table that stretches is contained in another table, defined as:

<table width="100%" border="0" cellspacing="0" cellpadding="0" height="70%" >

Not sure if this is a problem?

One thing to note is the table stretches, but the contents of the table stays fixed. So I define the table, the table stretches, but the content of the table stays fixed. Odd…

SuzyUK

11:22 pm on Dec 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1. tables are designed to stretch, that's their default, to try and contain them is at your peril

2. width="200"... 200 what?

rocknbil

8:48 pm on Dec 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Haha . . . Is Suzy UK so distant from old school inline table markup she forgets? :-) What she's referring to is in CSS, a unit must be specified. From what you've provided it's all inline markup.

However, your last comment, combined with hers, leads us to a clue:

The table that stretches is contained in another table, defined as:

<table width="100%" border="0" cellspacing="0" cellpadding="0" height="70%">
<tr><td>
<table width="200" border="1">
......

Should still render as a 200 pixel wide table with a border with the info provided. Did you test my example above?

However, something else has to be at play here. Is there any CSS connected to this document? Run the following code in any browser:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
</table>
<style type="text/css">
table { width:100%; border:1px solid #000000; }
</style>
</head>
<body>

<table width="20" border="0" cellspacing="0" cellpadding="0" height="70%">
<tr><td>
<table id="my-table" width="200" cellspacing="0" >
<tr><td>blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah </td></tr>
</table>
</td></tr>
</body>
</html>

Note the outer table is set to 20 (pixels) and the inner table is set to 200 (pixels) which is really impossible - if you removed the CSS, you'll see the outer is pushed to 200 px.

But note the CSS is set to 100% on **any** table element. So both of them indeed expand to full width, overriding the inline markup.

Let's look at another example, a style applied **only** to "my-table"


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
</table>
<style type="text/css">
table { width:100%; border:1px solid #000000; }
#my-table { width:400px; margin:auto; border:1px solid #ff0000; }
</style>
</head>
<body>

<table width="20" border="0" cellspacing="0" cellpadding="0" height="70%">
<tr><td>
<table id="my-table" width="200" cellspacing="0" >
<tr><td>blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah </td></tr>
</table>
</td></tr>
</body>
</html>

The general table selector sets all tables for 100% width, but we have id'ed my-table and added a specific selector for it. Although the width attribute is still 200 (px,) note the CSS says 400px. Hence, you now have a red-bordered table inside the outer table, centered by margin: auto. And the table width is? 400 pixels, overriding the inline attribute.

You **have** to have CSS mucking up the works (or, more accurately, it's doing as it's told and the use of inline attributes are misleading you.) It's the only explanation.

I'll add, you should avoid tables for layout, but this is an old and tired mantra, mostly ignored.

GetReal

9:25 pm on Dec 20, 2009 (gmt 0)

10+ Year Member



Hey Rock,
Great input. I'm HTML/CSS challenged; I use a design that was provided years ago. The design uses tables as the overall design, then I've added <div> elements to complete the site. I do realize it’s not the best of worlds, but it’s where I’m at, and I’m trying to continue using this design.

I’ll digest your input, and try some of your suggestions….

I’ll get back to you…again thanks for your input..

GR

GetReal

11:01 pm on Dec 20, 2009 (gmt 0)

10+ Year Member



Rock,
I sent you a sticky mail...

GR

SuzyUK

11:40 pm on Dec 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is Suzy UK so distant from old school inline table markup she forgets? :-)

She tells me hasn't had to do inline markup in a while and forget she did ;) LOL, thanks for the pick up!

However in my defence guv', I've noticed over the years that tables are more and more requiring the same accuracy as we're used to in CSS and that often applying the same thinking to them does help.. i.e. a table/table row/table cell height is actually treated as *MIN-HEIGHT* (so the table stretches) - The table-layout algorithms also have restrictions on the width property.. but you have to go the CSS table layout algorithm [w3.org] to get the full low down, reading just the HTML attributes will lead you to believe you can restrict both height and width..

Doctype may also play a part? and it can be quite hard to get tables to behave "the old way" unless in Quirks mode and even then Good browsers won't always play nice because they want to do the 'right' thing

Never thought about the mixed CSS / inline specificity thing, well spotted..

anyway hows my defence case going?

Thanks rocknbil
:)

rocknbil

7:01 am on Dec 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Haha . . I was teasing you, your expertise goes far and above and it's only natural to forget about stuff we don't (shouldn't) do. The only one we can't really control with CSS is cellspacing, which is why you often see

<table cellspacing="0">

Cellpadding seems to react well with td padding.

GR no message received, but have you tried the above suggestions? Find the CSS linked to the document and remove it, or better yet, the selector affecting tables. It may be buried, like

#content .top-section table { width:100%; }

Or just ID that table and add a selector for it as shown, this should fix it.

mathew33

10:05 am on Dec 21, 2009 (gmt 0)

10+ Year Member



Hi rocknbil,

I thinks it was useful to others

Thanks

[edited by: tedster at 10:12 am (utc) on Dec. 21, 2009]

rocknbil

7:54 pm on Dec 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



After seeing the site via PM, I have new information.

I'm completely wrong. :-) It's not the CSS or any other issue mentioned above.

This layout is heavily tabled, which can be extremely confusing, and that is the root of the problem here- confusion, you have the wrong table.

I made a local copy of the page; in that page I searched for just "200". There are many instances, but only one in a table, like this:

<table width="200" border="0" cellspacing="0" cellpadding="0">

Change this table like so, temporarily.

<table width="200" border="3" cellspacing="0" cellpadding="0">

Refresh the page, and note that the table indeed does not change size on browser resize. You have the wrong table. :-)

StoutFiles

3:10 am on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's no way that'll fit on the screen with a width of 200 hobnobs!

GetReal

9:47 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



Hey Rock!

I tired your suggestions, I see the border (3) on the navigation table, yet the background of the table, or at least what the table is residing on stretches when I enlarge the browser... Yikes!

GR

Readie

10:04 pm on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have the table wrapped in a <div></div>, and a background defined for the div?

Regards,

Readie

swa66

8:40 pm on Dec 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only one we can't really control with CSS is cellspacing, which is why you often see

<table cellspacing="0">

Take a look at border-collapse:collapse; and you'll never need that html attribute again on your data tables.

SuzyUK

9:44 pm on Dec 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Take a look at border-collapse:collapse; and you'll never need that html attribute again on your data tables.

Alas if only it were all true ;) - It's true if the value of the cellspacing attribute is to be zero, as it collapses the cell borders into the table border - but if you want it to be anything else, i.e.
cellspacing="10"
, then the support is not quite there for border-spacing [w3.org] Which, when applied to the table element would be the CSS equivalent way of spacing the border of the cell from the border of the table, the opposite of border-collapse.

GetReal

9:38 pm on Dec 27, 2009 (gmt 0)

10+ Year Member



The table in question is ensconced in a table cell.

<td width="16%" height="587" valign="top" bgcolor="#348CB4" >
<table width="200" height="679" border="0" cellpadding="0" cellspacing="0" >

So it’s not the table that is stretching, it’s the container 'td' cell element that is stretching. So I see the background of the cell stretch as the background is a different color than the rest of the layout.

I tried setting the td width to a fixed 200 to match the table, but the cell seems to stretch along with the browser window…

rocknbil

11:35 pm on Dec 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I saw that too. And played with it. Setting that 16% to a fixed width doesn't seem to work because the content is pushing things around, overriding it.

The problem really is it's just too deeply nested to spend any time on without compensation, so I think you're going to have to take the hit and reconstruct it.

One thought I had, but didn't pursue, was to remove the background from that cell and make the "200 px width" table that background color. But then you are going to have to do something with the surrounding cells to make it all join correctly - maybe BG color the one cell above it, I don't know.

If it were me, I'd start ripping out tables and re building from scratch. Not a pretty thought, I know, but it might come down to that.

GetReal

3:33 pm on Dec 29, 2009 (gmt 0)

10+ Year Member



Hey Rock,

Changing the background of the container cell to white, and the nav table to the desired background color worked! Apparently the container cell is stretching, but not noticeable to the user, and the nav table is fixed with the desired bg color....

Thanks to all that took the time to contribute, very much appreciated....

GR