Disable changing of width in html table - html

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.

Related

Same width cells in table column?

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.

Maximize CSS table-cell width

On my website I have a CSS table with two table cells.
One is 400px wide, and I want the other one to take up the rest of the page.
How do I do that? I have tried applying width 100% to it, but that doesn't work on chrome, and width: auto doesn't work at all.
.wrapper{
display: table;
width: 100vw;
max-width: 100vw;
}
[...]
.sidebar{
display: table-cell;
border-right: 1px solid #707070;
width: 400px;
}
[...]
.content{
display: table-cell;
width: 100%;
}
HTML:
<div class="wrapper">
<div class="sidebar">
<!-- Stuff -->
</div>
<div class="content">
<!-- Titles and text, all that usual blog stuff. Oh, and a big, wide header. -->
</div>
</div>
Set your wrapper to be display:table and not display:table-cell because otherwise the wrapper gets an anonymous table wrapper at auto width (shrink to fit for tables).
.wrapper{
display: table;
width: 100%;
table-layout:fixed;
}
I wouldn't use vw for the width either as that includes the scrolbar and will cause a horizontal scrollbar when content is below the fold.
Apply style="width:100%" to the table element, keeping the 400px limit on the first cell. The second cell will expand to meet the table's width.
The table should automatically scale to full width (assuming it's width is 100%), even with one column fixed and the other(s) flexible. I threw up a little test/example # http://jsfiddle.net/41uc6Lsq/1/
<table width="100%">
<thead>
<tr>
<th width="20px">Col 1</th>
<th>Col 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
</tbody>
</table>
<table width="100%">
<tbody>
<tr>
<td width="20px">1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
</tbody>
</table>
EDIT: just saw your CSS edit - could you post the HTML too please?

TD column width value not working with fixed table-layout

I have following table structure:
<table class="tableStyle">
<tr>
<td width="20px">col1</td>
<td width="50px">col2</td>
<td width="50px">col3</td>
<td width="15px">col4</td>
<td width="25px">col5</td>
<td width="20px">col6</td>
<td width="20px">col7</td>
<td width="20px">col8</td>
</tr>
</table>
CSS class definition is:
.tableStyle{
table-layout:fixed;
margin: 0 auto; width: 960px;
}
The problem is that all columns are displaying with equal width despite the fact that i am explicitly defining each column width.
Why are above width values are not working? Any suggestion to make it work with fixed table layout?
The "archaic" width attribute does not take a unit, it expects something like width="20".
However, the "most correct" way to define a table is like so:
<table>
<colgroup>
<col style="width:20px" />
<col style="width:50px" span="2" />
<col style="width:15px" />
<col style="width:25px" />
<col style="width:20px" span="3" />
</colgroup>
<tbody>
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
<td>col4</td>
<td>col5</td>
<td>col6</td>
<td>col7</td>
<td>col8</td>
</tr>
</tbody>
</table>
This works especially well for large tables, because the browser only needs to read the <colgroup> element to know exactly how the entire table should be laid out, without needing to calculate widths based on individual cell styles.
You have to use:
<td width="20">
or
<td style="width: 20px">
You should the attribute width without the unit px. Probably there are some modern browsers that accept the attribute with the units, but is not the correct way!
You have a similar issue in this another Stackoverflow case:
The width property does not support px for td, if you want to write the width in px, you need to provide css as below
<td style="width: 20px">
Seems like works as intended for me. please check the below fiddle.
.tableStyle{
table-layout:fixed;
margin: 0 auto; width: 960px;
}
http://jsfiddle.net/9x56E/
suggest such an option
HTML
<table class="tableStyle">
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
<td>col4</td>
<td>col5</td>
<td>col6</td>
<td>col7</td>
<td>col8</td>
</tr>
</table>
CSS
.tableStyle{
table-layout:fixed;
margin: 0 auto; width: 960px;
background: #ddd;
}
td:nth-child(1n) {
width: 20px;
background: #876342;
}
td:nth-child(3n+1) {
width: 100px;
}
demo
Instead of putting the width on the td, try adding it to the th using css.
For example,
HTML
<table>
<tr>
<th class="column-1">Heading 1</th>
<th class="column-2">Heading 2</th>
<th class="column-3">Heading 3</th>
</tr>
<tr>
<td>TD 1</td>
<td>TD 2</td>
<td>TD 3</td>
</tr>
</table>
CSS
.column-1 {
width: 50%;
}
.column-2 {
width: 25%;
}
.column-3 {
width: 25%;
}
I had the exact same problem and found this resource helpful:
https://css-tricks.com/fixing-tables-long-strings/

Adjusting table cell width

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.

How do I make one table column fill all the spare horizontal space?

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>