Table cell width in IE7 with "table-layout: fixed" table - html

I have a table I am using as a toolbar on a web page. It works great in all browsers except for IE7. The issue is that it expands the table to fit the contents (pushing the content off the screen) unless I specify "table-layout: fixed" in the CSS. When I set the table layout to fixed it makes all the cells the same size, but I want them to size automatically to fit the content (and word wrap if needed). Setting "width: auto" on the cell does not do anything.
Here is the HTML:
<table id="ToolbarTable" cellspacing="0px">
<tr>
<td class="ToolbarCell" align="center">
Button1
</td>
<td class="ToolbarCell" align="center">
Button2 Button3 Button4
</td>
<td class="ToolbarCell" align="center">
Button5 Button6 Button7 [Button8 Button9 - not always visible]
</td>
</tr>
</table>
And the CSS:
#ToolbarTable
{
margin-top: 1px;
width: 100%;
min-height: 24px;
table-layout: fixed;
word-wrap: break-word;
}
#ToolbarTable td
{
min-width: 50px;
width: auto;
border: solid 1px #000000;
}
In IE7, how can I make a table a specific width (and have IE7 honer it), while still letting the width of the table cells resize automatically?
EDIT: added code examples.

Try using min-width in connection with a set width. I believe this combination should work with IE 7.

You need to set a table width for the entire table.
And otherwise there's only one option, setting the table width's manually (easy to gather by using developer tools that most modern browsers include (IE8+, FireFox, Chrome, Safari, etc.), you can read the width's there). Otherwise I wouldn't know.

Although I had spaces between the buttons I also had some spaces there and it was preventing the word wrap and causing the table width to exceed the width I specified. Removing from between the buttons fixed the problem. Each was surrounded by a space (I just wanted to add more space) and all other browsers were doing the line break correctly, but IE7 must either remove the extra spaces or assume they should be spaces.

Related

CSS table > td - force content to one line cross-browser

This is working on Chrome/Edge:
<style>
.o {
width: 1px;
white-space: nowrap;
}
</style>
<table style="width:100%; position: relative; border-collapse: collapse">
<tr>
<td class="o">text no wrap</td>
<th>Head</th>
<td class="o">text no wrap</td>
</tr>
</table>
white-space: nowrap; ... stops wraping
width: 1px; ... auto expend content to minimum needed width
This are the problems:
internet explorer: auto-expand width not working. (customers still use them, although i don't like ie)
So there is a wrap if width is set. Without width tds are to big (empty space) because of the missing minimize to content.
cross-browser: if content is too big, without wrapping, it expands table-witdh over 100%. Better would be a "only wrap if really needed".
side information: i cannot use a fixed layout, because content is filled from a database
You need to respect HTML structure in IE. And not just set randomly your style and table.
And it seems to work fine the same way in all browser for me. ie is indicating : width: 79.98px when you inspect and check the calculated value. So auto expand works on ie 11
You might have more css or html but just as you gave. IE11 is making the job as per capture:

Using CSS td width absolute, position

Please see this JSFIDDLE
td.rhead { width: 300px; }
Why doesn't the CSS width work?
<table>
<thead>
<tr>
<td class="rhead">need 300px</td>
<td colspan="7">Week #0</td>
<td colspan="7">Week #1</td>
<!-- etc..-->
</tr>
<tr>
<td class="rhead"></td>
<td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>
<td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>
<!-- etc... -->
</tr>
<thead>
</table>
Also, what are the effects of position:fixed, absolute etc have on td widths if any? I am looking for a reason more than a fix. I am hoping to understand how it works.
This may not be what you want to hear, but display: table-cell does not respect width and will be collapsed based on the width of the entire table. You can get around this easily just by having a display: block element inside of the table cell itself whose width you specify, e.g
<td><div style="width: 300px;">wide</div></td>
This shouldn't make much of a difference if the <table> itself is position: fixed or absolute because the position of the cells are all static relative to the table.
http://jsfiddle.net/ExplosionPIlls/Mkq8L/4/
EDIT: I can't take credit, but as the comments say you can just use min-width instead of width on the table cell instead.
You're better off using table-layout: fixed
Auto is the default value and with large tables can cause a bit of client side lag as the browser iterates through it to check all the sizes fit.
Fixed is far better and renders quicker to the page. The structure of the table is dependent on the tables overall width and the width of each of the columns.
Here it is applied to the original example: JSFIDDLE, You'll note that the remaining columns are crushed and overlapping their content. We can fix that with some more CSS (all I've had to do is add a class to the first TR):
table {
width: 100%;
table-layout: fixed;
}
.header-row > td {
width: 100px;
}
td.rhead {
width: 300px
}
Seen in action here: JSFIDDLE
The reason it doesn't work in the link your provided is because you are trying to display a 300px column PLUS 52 columns the span 7 columns each. Shrink the number of columns and it works. You can't fit that many on the screen.
If you want to force the columns to fit try setting:
body {min-width:4150px;}
see my jsfiddle: http://jsfiddle.net/Mkq8L/6/
#mike I can't comment yet.
The reason, is, because you did not specify the width of the table, and your whole bunch of td's are overflowing.
This for example, i've given the table a width of 5000px, which I thought would fit your requirements.
table{
width:5000px;
}
It is the exact same code you provided, which I merely added in the table width.
I believe what is happening, is because your TD's are way past the default table width. Which you could see, if you pull out about 45 of your td's in each tr, (i.e. the code you provided in your question, not jsfiddle) it works exactly fine
Try this it work.
<table>
<thead>
<tr>
<td width="300">need 300px</td>
Try to use
table {
table-layout: auto;
}
If you use Bootstrap, class table has table-layout: fixed; by default.
My crazy solution.)
$(document).ready(function() {
$("td").each(function(index) {
var htmlText = "<div style='width:300px;'>" + $(this).text() +"</div>";
$(this).html(htmlText);
});
});
Use table-layout property and the "fixed" value on your table.
table {
table-layout: fixed;
width: 300px; /* your desired width */
}
After setting up the entire width of the table, you can now setup the width in % of the td's.
td:nth-child(1), td:nth-child(2) {
width: 15%;
}
You can learn more about in on this link: http://www.w3schools.com/cssref/pr_tab_table-layout.asp
If table width is for example 100%, try using a percentage width on td such as 20%.
Wrap content from first cell in div e.g. like that:
HTML:
<td><div class="rhead">a little space</div></td>
CSS:
.rhead {
width: 300px;
}
Here is a jsfiddle.
You can also use:
.rhead {
width:300px;
}
but this will only with with some browsers, if I remember correctly IE8 does not allow this. Over all, It is safer to just put the width="" attribute in the <td> itself.

Why doesn't IE respect table width with fluid image child

Consider the following code:
HTML:
<table>
<tr>
<td><img src="http://watduck.jpg.to" /></td>
</tr>
</table>
CSS:
table { width: 10% }
img { max-width: 100% }
The image should obviously be a 10th the width of the window, which is exactly what it is in every browser except IE, where it simply falls back to its original size.
However, consider this:
HTML:
<div><img src="http://watduck.jpg.to" /></div>
CSS:
div { width: 10% }
img { max-width: 100% }
which IE does get right, and displays at a 10th of the window width.
So, here's the question: what causes this behavior, and what could possibly be done to force IE to respect the table's width?
Tested in IE8 & IE9 (don't care about IE7 and below).
If you specify table-layout: fixed; in the table css it works.
There seems to be some contradictory terminology in the standard regarding table layouts. In particular, table-layout: auto; says this:
The column width is set by the widest unbreakable content in the cells
Since the images content is unbreakable, it sets the width of the cell to the size of the content. The max-width seems to be overriden by it.

In quirks mode is it possible to have img's resize to their containing table cell?

This may seem like a blast from the past but due to project constraints I am stuck with quirks mode and tables...
The tables that i'm dealing with have a single image in each cell where all the cells should be the same size. The tables width and height are set as percentages of a parent container.
The problem is the images don't resize down, they stay at their original size seemingly no matter what I do. Then the table doesn't adhere to its set size, it has resized to hold all of the images. In standards mode I believe 'width: 100%' on the image gets closer to what I want to achieve.
I'm considering a javascript solution which loops over each image calculating what their size should be and resizing manually. But this is probably going to cause a bit of a loading time at the start which isn't ideal.
Edit:
I have written a basic example at JSBin. What I want to achieve is to be able to set the size of the table and have the images resize, whether growing or shrinking to their cell.
The 4th jsbin revision uses the dummy images.
I think I've solved this.
I've tested this demo in Chrome, Firefox, Internet Explorer, Safari, Opera; they all render consistently.
I had to add a wrapper div in each cell. I know this isn't awesome, but it had to be done to make it work in Chrome.
I added table-layout: fixed to make it work in Internet Explorer.
Live Demo
CSS:
#mycontainer {
width: 300px;
height: 300px;
border: 1px solid red;
}
#mytable {
height: 50%;
width: 50%;
table-layout: fixed;
}
#mytable div {
width: 100%;
height: 100%;
}
#mytable img {
width: 100%;
height: 100%;
}
HTML:
<div id="mycontainer">
<table id="mytable" cellpadding="0" cellspacing="0">
<tr>
<td><div><img src="http://dummyimage.com/28x28/000/fff.png&text=Dummy" /></div></td>
...
</tr>
...
</table>
</div>
I have solved my problem via JavaScript. I couldn't find a way to make all the browsers play nice without forcing them.
I basically loop over each image checking what their parent tables size was supposed to be, then divide that by the number of rows to find the image height and by columns for width.

Why isn't this HTML table working properly in Chrome?

Consider this simple HTML table:
<!doctype html>
<html>
<head>
<title>Test</title>
</head>
<body>
<table style="border-collapse: collapse; table-layout: fixed; width: 280px;">
<tr>
<td style="padding: 5px; width: 50px; border: 0; word-wrap: break-word;">a</td>
<td style="padding: 5px; width: 100px; border: 0; word-wrap: break-word;">b</td>
<td style="padding: 5px; width: 100px; border: 0; word-wrap: break-word;">c</td>
</tr>
</table>
</body>
</html>
This renders properly in every major browser, except in Chrome, in which the column widths bizarrely come out as 46px, 102px and 102px respectively.
If I take the CSS width declaration out of the table element, then it renders correctly in Chrome. But I need this in there, otherwise word-wrapping won't work correctly.
Any idea why this isn't working? I've tried different doctypes and this hasn't changed anything, so I'm assuming it's not an HTML5 table problem.
EDIT: It turns out that if you specify table-layout: fixed and a pixel width on the table element and pixel widths on each column, then Chrome will assume that your column widths include padding. This contravenes the W3C box model and is in violation of CSS2 required behaviour.
If you don't specify table-layout: fixed or you don't specify a pixel width on the table element, then Chrome will correctly assume your column widths that you specify exclude padding. All other browsers assume that your column widths exclude padding.
In the example above, specifying the widths as 60px, 110px and 110px would fix the problem in Chrome but then break it in every other browser. The values of 46px, 102px and 102px come from Chrome evenly distributing the columns with a ratio of 40:90:90 instead of 50:100:100.
I fixed this problem myself today using table-layout: fixed and box-sizing: border-box to force Chrome to stop taking padding into account. Otherwise you never know what sizing model Chrome will pick, even for two very similar tables.
Well, my math says Chrome's doing what it should:
102 + 102 + 46 = 250px -> Widths
2(5) + 2(5) +2(5) = 10 + 10 + 10 = 30px -> Padding
Widths + Padding = 250 + 30 = 280px, or the width you specified for the table.
my method to fix this, executed for chrome browser only. We only use fixed tables in our web app and it seems working good, it's good to know that sometimes Chrome dimensions the col width perfectly, but sometimes not depending on where the table is located (and most of it inside what it's located).
var correctedWidth;
var extraSize;
//chrome is taking the padding and border to calculate the column width , not other browsers
$('table.default.fixed > thead > tr > th').each(function()
{
if ($(this).attr('width')!='undefined' && $(this).attr('width')!=null)
{
if ($(this).attr('sizeUpdated')=="undefined" || $(this).attr('sizeUpdated')==null) {
extraSize = parseInt($(this).css('padding-left'))+parseInt($(this).css('padding-right'))+2; //2 for the borders (2*1px)
if ( parseInt($(this).attr('width'))!= $(this).width() )
{
correctedWidth=$(this).width()+2*(extraSize);
}
else
{
correctedWidth=$(this).width()+extraSize;
}
$(this).attr('width',correctedWidth+'px');
$(this).attr('sizeUpdated','Y');
}
}
});
I fixed this problem by removing a parent element (fieldset) that had the following property:
display: inline-flex;
It's better not to use styles inline within the HTML text.