According to the CSS spec, when a table has the table-layout:fixed property, its width is calculated like this:
...the width of each column is determined as follows:
A column element with a value other than 'auto' for the 'width' property sets the width for that column.
Otherwise, a cell in the first row with a value other than 'auto' for the 'width' property determines the width for that column. If the cell spans more than one column, the width is divided over the columns.
Any remaining columns equally divide the remaining horizontal table space (minus borders or cell spacing).
The width of the table is then the greater of the value of the 'width' property for the table element and the sum of the column widths (plus cell spacing or borders).
My understanding of this is:
the width of each column is calculated (with the width property of <col> elements, if present, taking precedence)
the column widths are totalled
if the column width total is greater than the width property of the <table> element, then the table will be as wide as the column width total.
However — in the following example, I have a table with one row containing three cells. Each column’s width is set to 100 pixels using the <col> element. The table element has no width assigned to it, and it’s the child of a <div> that’s 200 pixels wide.
I’d expect the width of the table to be 300 pixels, and for it to therefore overflow its parent <div>. However, instead, the table is only 200 pixels wide (i.e. as wide as its parent <div>), and each column is therefore narrowed to 66.6 pixels wide. (I’ve checked in the latest Safari, Chrome, Firefox, and Edge.)
Why is the table only 200 pixels wide, and not 300 pixels?
<div style="background:black; color:white; width:300px;">300px</div>
<br>
<div style="background:black; color:white; width:200px;">200px</div>
<br>
<div style="width:200px; background:green; overflow:scroll;">
<table style="table-layout:fixed; border-collapse:collapse;">
<col style="width:100px;">
<col style="width:100px;">
<col style="width:100px;">
<tr>
<td>1</td>
<td>2</td>
<td style="background:red;">3</td>
</tr>
</table>
</div>
(I’ve given the <div> a green background and overflow:hidden, and the third cell a red background, to make the unexpected result clearer.)
You are correct on you interpretation of the rules. The problem is that the width of the table is not set, so the browser can't compare with the column width sum. Just set the width for the table, even width: 0px will work.
<div style="background:black; color:white; width:300px;">300px</div>
<br>
<div style="background:black; color:white; width:200px;">200px</div>
<br>
<div style="width:200px; background:green; overflow:scroll;">
<table style="table-layout:fixed; border-collapse:collapse;width:0px">
<col style="width:100px;">
<col style="width:100px;">
<col style="width:100px;">
<tr>
<td>1</td>
<td>2</td>
<td style="background:red;">3</td>
</tr>
</table>
</div>
Related
I have a table (auto width) and want the columns only to stretch to fit the cell content width, but then shrink and use text-overflow:ellipsis when the screen is not wide enough. I have tried different widths, values and units, but can only get one or the other function to work.
<table border="1">
<tr>
<td style="max-width:fit-content; overflow:hidden; text-overflow:ellipsis; white-space:nowrap">fit cell width to content and truncate text when needed</td>
</tr>
</table>
Can you explain why these two HTML codes result in different look when rendered? fiddle1 and fiddle2
The only difference in the code is that in fiddle1 style="width: 50px;" is in the first row of the table, while in fiddle2 it is in the second row. But this results in different widths for each one.
According to Chrome Dev Tools fiddle1 both cells/columns have 51px width (should be 50px anyways), and in fiddle2 the first cells/columns have 49px width and the second 50px width.
Markup in Fiddle 1
<table>
<tr>
<td style="width: 50px;">Test</td>
<td style="width: 50px;">Testing 1123455</td>
</tr>
<tr>
<td>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
<td>B</td>
</tr>
</table>
Markup in Fiddle 2
<table>
<tr>
<td>Test</td>
<td>Testing 1123455</td>
</tr>
<tr>
<td style="width: 50px;">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
<td style="width: 50px;">B</td>
</tr>
</table>
CSS for both fiddles
table {
table-layout: fixed;
width: 100px;
}
td {
border: 1px solid black;
overflow: hidden;
}
That is the magic of 'table-layout: fixed'.
As specified in this link
The table-layout CSS property specifies the algorithm used to lay out cells, rows, and columns.
When the value is 'fixed': Table and column widths are set by the widths of table and col elements or by the width of the first row of cells. Cells in subsequent rows do not affect column widths.
In your first case: As you specified the width for columns of first row. Rendering algorithm takes the width and renders it with that width. The extra 1px you see, is just the border width.
In Second Case: As there is no width for first row specified. It takes the table width and try to adjust the row columns in it. As for two columns there will be three borders, we left with the 97px to be divided among the two columns. As pixel cannot be divided into decimals, you get 48px and 49px width of columns. extra 1px is for the border width
I would like to have a table that has a percentage width and each column will also have a percentage width. I want to use use css to do overflow:hidden; text-overflow:ellipsis; inside certain columns however it does not seem to be working for me.
Here is what I have:
<table style='width:50%;'>
<thead>
<tr>
<td style='width:15%;'>column 1</td>
<td style='width:15%;'>column 2</td>
<td style='width:70%;'>column 3</td>
</tr>
</thead>
<tbody>
<tr>
<td style='border:1px solid red;'>
<div style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>this is some data 1</div>
</td>
<td style='border:1px solid red;'>
<div style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>this is some data 1</div>
</td>
<td style='border:1px solid red;'>No hiding</td>
</tr>
</tbody>
</table>
As the page gets smaller the third column gets smaller instead of the first two columns show ellipsis and shrinking as I would expect.
Here is a jsfiddle.
table {
table-layout:fixed;
}
This might be what you are looking for.
http://jsfiddle.net/wGznj/2/
More info on 'Table Width Algorithms' part, # http://reference.sitepoint.com/css/tableformatting
"With the fixed table layout algorithm, the widths of columns and of the table are not governed by the contents of the table’s cells."Instead, the width of each column is determined as follows:
Column objects whose width is not auto set the width for that column.
A cell in the first row, whose width is not auto, sets the width of the column it belongs to. If the cell spans more than one column, the width is divided over the columns.
Any remaining columns equally divide the remaining horizontal space, minus any borders or cell spacing."
I have a HTML table
Required details are mentioned in JSFiddle.
This is working well on Firefox, but it's not on Chrome and IE!
what's wrong with them? why they expand the cell in the last row which has a fixed height, instead of second row which is the only cell with not-determined height in first column.
Here is the HTML code:
<table style="width: 100%;" dir="rtl" cellpadding="0" cellspacing="0">
<tr>
<td style="height: 100px; width: 176px; background:blue">
this is a fixed size cell
</td>
<td rowspan="3" style="background:gray; height: 360px;">this cell is spanned 3 rows<br /> lots of content may be placed here</td>
</tr>
<tr>
<td style="background:red">this should expand depending on gray region</td>
</tr>
<tr>
<td style="height: 46px; background:blue">this is a fixed size cell</td>
</tr>
seems like height algorithm for your case is underfind. You want td {height: auto} to extend all available space, but
A 'height' value of 'auto' for a 'table-row' means the row height used
for layout is MIN. MIN depends on cell box heights and cell box
alignment....In CSS 2.1, the height of a cell box is the minimum height required by the content
and finaly
CSS 2.1 does not specify how cells that span more than one row affect
row height calculations except that the sum of the row heights
involved must be great enough to encompass the cell spanning the rows.
see 17.5.3 Table height algorithms
I have some text in table column which is greater than the column width I want. So text is overflowing from it. So I apply overflow:auto but in this case it is showing scroll on every page where data is less than the width of the column.
I don't want to show the scroll on that page where data is less than the width of column just want to show only where data is greater than its length.
Does any one have some suggestions?
<td style=" width:50%;overflow:auto;>
Try to wrap td content in div with fixed width and overflow: auto
For example:
<table>
<tr>
<td>some content</td>
<td> <!-- your fixed width column -->
<div style="width: 100px; overflow: scroll;">
Loremipsumdolorsitametconsectetuadipisicingelit
</div>
</td>
</tr>
</table>
use this <td style=" width:50%;overflow:scroll;">
This will insert horizontal and vertical scrollbars.
They will become active only if the content requires it.