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>
Related
I have this table
table td {
width: 200px;
text-align: center;
}
.red {
border:1px solid red;
}
<table>
<thead>
<tr>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td class="red">$ 11.122,00</td>
</tr>
<tr>
<td class="red">$ 11.1,00</td>
</tr>
<tr>
<td class="red">$ 11.122,00232</td>
</tr>
<tr>
<td class="red">$ 11.122,00</td>
</tr>
</tbody>
</table>
I need to have my numbers centered in the td itself, but I can't find a way to position the numbers one under another so the end result will look like this
So I need centered text in the td but the number is vertically aligned by the, and the . from the right
So at the end result will be in the centered td:
$ 11.122,00
$ 11.1,00
$ 11.122,00232
I don't need text-align:right on this, because onthat way they will be aligned just right, the numbers will be one under another, but the whole content in the td will be not centered - it will be just right aligned.
If I've understood correctly, you want the cell header centered, and the cells right aligned? If so, just add a style for the th like so:
table td {
width: 200px;
text-align: center;
}
table td span {
width: 50%;
border:1px solid red;
display:inline-block;
text-align:right;
}
.red {
border:1px solid red;
}
<table>
<thead>
<tr>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td class="red">
<span>
$ 11.122,00
</span>
</td>
</tr>
<tr>
<td class="red"><span>$ 11.1,00</span></td>
</tr>
<tr>
<td class="red"><span>$ 11.122,00232</span></td>
</tr>
<tr>
<td class="red"><span>$ 11.122,00</span></td>
</tr>
</tbody>
</table>
Quoting this, since I don't have the right reputation to comment...
Thank you. I thought the same thing you suggest. The problem is that the width is fixed - so what if some number is bigger than the whole width - 50% of the parent ?
Set your parent width to width:auto; and it will actually get the right width based on it's child width. So it wont be a problem if it comes a row with more characters than you want.
I'm trying to find the best way to dynamically set the width of a table column, without having to set the width of each cell individually.
I have a table where each column should have a fixed width, regardless of the contents. This works fine if every cell has a width and a max-width set individually. But this is awkward to set dynamically.
I'm trying to use the colgroup and col tags to set the width of each column, but this doesn't seem to work.
table, th, td {
border: 1px solid black;
}
th, td {
overflow: hidden;
white-space: nowrap;
}
<table>
<colgroup>
<col style="width: 50px; max-width: 50px;">
<col style="width: 50px; max-width: 50px;">
<col style="width: 50px; max-width: 50px;">
</colgroup>
<tr>
<th>ISBN</th>
<th>Title</th>
<th>Price</th>
</tr>
<tr>
<td>3476896</td>
<td>My first HTML</td>
<td>$53</td>
</tr>
<tr>
<td>5869207</td>
<td>My first CSS</td>
<td>$49</td>
</tr>
</table>
I expected the example to show the first and second columns of the table truncated, but it doesn't work.
So I wonder if this will do the trick - you can set width/max-width on the headers only, and then max-width: 0 for all cells and it seems to achieve what you want...
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
th, td {
overflow: hidden;
white-space: nowrap;
}
td {
max-width: 0;
}
</style>
</head>
<body>
<table>
<tr>
<th style="width:50px; max-width:50px">ISBN</th>
<th style="width:50px; max-width:50px">Title</th>
<th style="width:50px; max-width:50px">Price</th>
</tr>
<tr>
<td>347689643</td>
<td>My first HTML</td>
<td>$53</td>
</tr>
<tr>
<td>5869207</td>
<td>My first CSS</td>
<td>$49</td>
</tr>
</table>
</body>
</html>
Looks like if you add a span="3" and a width="#ofPixels" ie: width="100" to your colgroup element it will specify a width for your columns.
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.
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>