I have read the following:
-autoformatting-1. HTML table: keep the same width for columns
-autoformatting-1. "If you set the style table-layout: fixed; on your table, you can override the browser's automatic column resizing. The browser will then set column widths based on the width of cells in the first row of the table." - This doesn't work in Firefox 66.0.4 and never has for me. Check out https://jsfiddle.net/x342vchn/ :
table {
table-layout: fixed;
}
td {
border: 1px solid #000;
overflow: hidden;
overflow-wrap: break-word;
}
.narrow {
max-width: 20px; /* Without this, the first table doesn't work either! */
width: 20px;
}
<table>
<tbody>
<tr>
<td class="narrow">sdkajdwaudawjdjawhdwjahdawjdhajh</td>
<td>hi</td>
</tr>
<tr>
<td>hi</td>
<td>hi</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td class="narrow">hi</td>
<td>hi</td>
</tr>
<tr>
<td>sdkajdwaudawjdjawhdwjahdawjdhajh</td>
<td>hi</td>
</tr>
</tbody>
</table>
The first table "works" because the wide content is in the cell that has the max-width restriction. The second table ignores everything.
-autoformatting-2. Set the table column width constant regardless of the amount of text in its cells?
-autoformatting-2. In this case, the answer suggests setting the same width on all cells.
If I want to set the same height for cells in a row, I can target them with a class on the row. So how do I set the same fixed width on cells in a column without specifically setting the max-width and width of all affected cells?
I have done some web searching, and blogs suggest the same as the first answer, but it does not work.
I have also tried setting the width of the first cell (th) in a thead. That made no difference either.
Am I missing something or is Firefox broken?
Currently, I'm using the same class on all cells of a certain width, even if they are empty because the contents can be modified with JS.
Have you tried using a col/colgroup approach (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/col) and setting the width attribute?
You'd have something similar to this:
td {
border: 1px solid #000;
}
<table>
<tbody>
<col width="40px" />
<col width="20px" />
<tr>
<td>hi</td>
<td>hi</td>
</tr>
<tr>
<td>hi</td>
<td>hi</td>
</tr>
</tbody>
</table>
I think this is the way to go for your case.
Related
Is using the "hidden" attribute semantically correct when hiding the table cell because of the rowspan and colspan attribute of another table cell?
I could find only examples which entirely remove the table cells to make space for the cell(s) spanning multiple rows and cols (having the rowspan and colspan attributes).
Example:
<table>
<tr>
<td colspan="3" rowspan="2">#1</td>
</tr>
<tr></tr>
<tr>
<td>#7</td>
<td>#8</td>
<td>#9</td>
</tr>
</table>
But is it semantically valid to do something like this?
<table>
<tr>
<td colspan="3" rowspan="2">#1</td>
<td hidden></td>
<td hidden></td>
</tr>
<tr>
<td hidden></td>
<td hidden></td>
<td hidden></td>
</tr>
<tr>
<td>#7</td>
<td>#8</td>
<td>#9</td>
</tr>
</table>
Just to give a little context: I'm working on a rich text editor having a constraint that empty span elements are being added to empty block elements. Having a span element inside an empty table row would be considered invalid HTML (tr can only have td and th as child elements).
I've been researching a bit but couldn't find a definitive answer to my question.
It's incorrect, and you don't need to write these cells in the HTML at all. The correct HTML for your example is as follows.
Having an identical rowspan value over all cells of a row (like in your first cell which spans all columns) does not make sense – this isn't a grid system but just a table which will contain whatever content you put into it.
table {
border-collapse: collapse;
width: 300px;
}
td {
border: 1px solid #ddd;
vertical-align: top;
}
tr:first-child > td {
height: 2.8em;
}
<table>
<tr>
<td colspan="3">#1</td>
</tr>
<tr>
<td>#7</td>
<td>#8</td>
<td>#9</td>
</tr>
</table>
So say I have the following HTML:
<table>
<tr>
<th>Heading:</th><td>text</td>
</tr>
<tr>
<th>Heading 2:</th><td>text</td>
</tr>
<tr>
<th colspan="2">Really long heading:</th>
</tr>
<tr>
<td></td><td>text</td>
</tr>
</table>
This creates a table with two columns, one for headers (left) and one for values (right) - except for one row, where a long header is taking up both columns, and the value is showed in the right most column on the next row. Along with this, I have some styling; most importantly:
td:last-child {
width: 100%;
}
th, td {
white-space:nowrap;
}
This makes it so the right most column takes any extra space. And what I have is working, even though the "really long" header is longer/wider than the header column. However, when I add a few more words to the really long header (but not making it long enough to increase the width of the whole table), after a certain point the width of the left column starts to increase with the length of the long header, decreasing the width of the right column. What's going on?
You can't define width:100% of an cell of a table with multiple column.
You should use colgroup tag to define width of cell:
.col-left {
width: 20%;
}
.col-right {
width: 80%;
}
<table>
<colgroup>
<col class="col-left">
<col class="col-right">
</colgroup>
<tr>
<th>Heading:</th><td>text</td>
</tr>
<tr>
<td colspan="2">Really long heading:</td>
</tr>
<tr>
<th>lorem ispum dolores hello</th><td>text</td>
</tr>
</table>
Another tricks to set a cell at the minimum and obviously the another cell take the rest of the width, are to set the first one at 1px:
table {
width: 200px;
}
th, td {
border: 1px solid #000;
}
th {
width:1px;
}
<table>
<tr>
<th>hello</th>
<td>world</td>
</tr>
<tr>
<td colspan="2">hi</td>
</tr>
<tr>
<td></td>
<td>text</td>
</tr>
</table>
I'm trying to create a "fixed-width" table, but it somehow changes the column width whenever the data in column is bigger than rest of them.
For example, following table changes the width on the last number, which is 10.
<table>
<tr>
<td rowspan="3">1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td rowspan="2" colspan="2">7</td>
</tr>
<tr>
<td>6</td>
</tr>
<tr>
<td>8</td>
<td colspan="2">9</td>
<td>10</td>
</tr>
</table>
Here's my CSS:
table {
border: 1px solid;
width: 400px;
height: 300px;
}
There are 2 algorithms for tables in CSS, triggered by the property table-layout:
table-layout:fixed will adapt cell widths to what the author (you) want, as far as possible
table-layout:auto (default value) will adapt cell widths to their content.
CSS
table {
table-layout: fixed;
width: 400px;
height: 300px;
border: 1px solid;
}
Here's a fiddle: http://jsfiddle.net/tk4J8/
Don't use column-width to specify width of table columns - it's a suggested optimal guideline for browsers that isn't really meant for fixed width layouts. Use colgroups instead.
Remove the column-width style in your stylesheet, and add this to your table tag before any tr or td tags:
<colgroup>
<col span="1"></col>
<col span="1"></col>
<col span="1"></col>
<col span="1"></col>
</colgroup>
And add this to your stylesheet:
colgroup col {
width: 100px;
}
jsFiddle.
Here's the Mozilla documentation for it.
Let's take 4 table columns - ID, Text, Date, Action. In my case table have always constant width - in example 960px.
How can I create such table as :
*-*------------------------------------*----------*----*
|1| Some text... |May 2011 |Edit|
*-*------------------------------------*----------*----*
|2| Another text... |April 2011|Edit|
*-*------------------------------------*----------*----*
As we can see, ID, Date and Action adjust their width to content, Text is as long as possible....
Is that possible to do without setting specific width of columns ? When ID = 123 or Date = November 2011, columns should automatically be wider...
Using a 100% width on the wide td and a fixed width for the table along with white-space:nowrap, this can be done:
Demo
HTML
<table>
<tr>
<td>1</td>
<td width="100%">Some text... </td>
<td>May 2011</td>
<td>Edit</td>
</tr>
<tr>
<td>2</td>
<td width="100%">Another text... </td>
<td>April 2011</td>
<td>Edit</td>
</tr>
</table>
CSS
table
{
...
width:960px;
}
td
{
...
white-space:nowrap;
}
basically, it's just like this: http://jsfiddle.net/49W5A/ - you have to set the cell-width to something small (like 1px) to make them stay as small as possible.
but as you'll see, theres one problem with the date-fields doing a line-wrap. to prevent this, just add white-space: nowrap; for your text-field: http://jsfiddle.net/ZXu7U/
working example:
<style type="text/css">
.table{
width:500px;
border: 1px solid #ccc;
}
.table td{
border: 1px solid #ccc;
}
.id, .date, .action{
width:1px;
}
.date{
white-space: nowrap;
}
</style>
<table class="table">
<tr>
<td class="id">1</td>
<td class="text">Some Text...</td>
<td class="date">May 2011</td>
<td class="action">Edit</td>
</tr>
<tr>
<td class="id">2</td>
<td class="text">Another Text...</td>
<td class="date">April 2011</td>
<td class="action">Edit</td>
</tr>
</table>
My best advice to you is to not touch the widths of the table, the table automatically layouts in a way that does all cells best.
However, if you'd like to push through, I'd use width: 1px; on the cells that needs adjusting (one of each column is enough). Also use white-space: nowrap on all cells. that will make sure the lines don't break.
Try this:
.id, .date, .action is the table cells (td).
CSS:
.id, .date, .action {
width: 1em;
}
It worked for me.
The width:1em will not cut the text but force the width size to the minimum.
The best way that I've found for setting table column widths is to use a table head (which can be empty) and apply relative widths for each table head cell. The widths of all cells in the table body will conform to the width of their column head. Example:
HTML
<table>
<thead>
<tr>
<th width="5%"></th>
<th width="70%"></th>
<th width="15%"></th>
<th width="10%"></th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Some text...</td>
<td>May 2018</td>
<td>Edit</td>
</tr>
<tr>
<td>2</td>
<td>Another text...</td>
<td>April 2018</td>
<td>Edit</td>
</tr>
</tbody>
</table>
CSS
table {
width: 600px;
border-collapse: collapse;
}
td {
border: 1px solid #999999;
}
View Result
Alternatively, you can use colgroup as suggested here.
I have a HTML table consisting of 3 columns. It has a fixed width of 600px.
<table>
<tr>
<td>Name</td>
<td>Qty</td>
<td>Actions</td>
</tr>
</table>
I want the Qty and Actions columns to be as small as possible (keeping the content to one line) and the Name column to take up the rest of the available space. The size of the Qty and Actions column change depending on content/font size so fixed widths will not work in this case.
Is there a way of doing this in HTML/CSS? Or is this something I need to break out the Javascript for?
You can apply width="99%" on that column. For example:
<table>
<tr>
<td width="99%">Name</td>
<td>Qty</td>
<td>Actions</td>
</tr>
</table>
you can use max-width:99%; on the first column and give fixed sizes on the other columns (I used pixels sized columns).
<table style="width: 100%;">
<tr>
<td style="max-width: 99%">
Will max
</td>
<td style='width:110px;'>
fixed size here
</td>
</tr>
</table>
For every column you want to be the minimum width: (1) set the CSS width to zero and (2) use white-space: nowrap to prevent the text from wrapping onto multiple lines.
table {
width: 100%;
}
:where(th, td):not(.max) {
width: 0;
white-space: nowrap;
}
/* For demo purposes */
table, th, td {
border: 1px solid gray;
}
<table>
<tr>
<th class="max">Name</th>
<th>Qty</th>
<th>Actions</th>
</tr>
<tr>
<td class="max">Victoria Adelaide Mary Louisa</td>
<td>233,546,443</td>
<td>Abort Retry Fail</td>
</tr>
</table>