HTML table widths set proportionally - html

I have a fixed-layout table, with two columns. If I set the width of just one column, things are as expected ( at least how I expected them to be).
However, if I specify the widths of both the columns, cells start dividing themselves proportionally.
table {
table-layout: fixed;
width: 120px;
border-collapse: collapse;
}
td {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
padding: 0;
}
td:first-child {
width: 5%;
}
td:nth-child(2) {
width: 2%;
}
<table>
<tr>
<td>Ed</td>
<td>Wood</td>
</tr>
<tr>
<td>Albert</td>
<td>Schweitzer</td>
</tr>
<tr>
<td>Jane</td>
<td>Fonda</td>
</tr>
<tr>
<td>William</td>
<td>Shakespeare</td>
</tr>
</table>
What I intended was for the first column to take 5% of the row width ( 120 px ) and the second column to take 2% of the row width. However, the first one takes 5/7 of the row width and the second one takes 2/7 of the row width.
Is there some spec that details this behavior?

As mentioned by Itay, the total width has to match the width of the table. When you set the width of one column the only thing that happens is that first column takes 5% and the other ones take remaining 95%, if you add width to all, they split the space piece by piece until they reach 100%.
One way you could get what you're trying to achieve is have an empty column like this
table {
table-layout: fixed;
width: 120px;
border-collapse: collapse;
background: whitesmoke;
}
td {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
padding: 0;
}
td:first-child {
width: 5%;
}
td:nth-child(2) {
width: 2%;
}
td:last-child {
width: auto;
}
<table>
<tr>
<td>Ed</td>
<td>Wood</td>
<td></td>
</tr>
<tr>
<td>Albert</td>
<td>Schweitzer</td>
<td></td>
</tr>
<tr>
<td>Jane</td>
<td>Fonda</td>
<td></td>
</tr>
<tr>
<td>William</td>
<td>Shakespeare</td>
<td></td>
</tr>
</table>
That way, the empty column will be taking that remaining space and let your other columns have the width you specified. And if you do decide that you want the rows to span the full width of the table, width: auto will be 0 and shouldn't cause you any problems.

Related

Adjust HTML table colum size both with percent and fixed sizes?

I want to create a table with the following widths of columns.
Is it possible to use both percents and absoulte widths as shown in my graphic?
This is because some columns make no sense below a certain width and always need the same width and others are more flexible.
Here you go:
#fixed{
table-layout:fixed;
}
td{
border:1px solid red;
}
td:nth-child(even){
background-color: grey;
}
<table id="fixed" border="0" style="width:100%;border-collapse:collapse;">
<tr>
<td style="width:50px;">1</td> <!--Fixed width-->
<td style="width:50%">Title</td>
<td style="width:50%">Interpret</td>
<td style="width:20px">1</td>
</tr>
</table>
What I could understand, you can achieve this by applying styles with table i.e table-layout: fixed; and width:100%;. Also width of td using px and %. According to your design, you are using four columns and I wrote code for the four columns;
Following would be your CSS;
table {
table-layout: fixed;
width: 100%;
}
td:first-child {
width: 50px;
}
td:last-child {
width: 20px;
}
.second-column {
width: 50%;
}
.third-column {
width: 50%;
}
td:nth-child(odd) {
background-color: grey;
}
td:nth-child(even) {
background-color: silver;
}
<table>
<tr>
<td>1</td>
<td class="second-column">2</td>
<td class="third-column">3</td>
<td>4</td>
</tr>
</table>
Hope this is what you required.

Applying width to table cell when colspan is applied

Even though i have applied table-layout fixed, table td cell are not taking width,
is it possible to apply width for td cell without changing table display property.
HTML:
table {
width: 500px;
border: 1px solid #dadada;
border-collapse: collapse;
table-layout: fixed;
}
td {
border: 1px solid #dadada;
}
.td-1 {
width: 480px;
}
.td-2 {
width: 200px;
}
.td-3 {
width: 290px;
}
.td-4 {
width: 100px;
}
.td-5 {
width: 390px;
}
<table>
<tr>
<td colspan="4" class="td-1">This is Amazing</td>
</tr>
<tr>
<td colspan="2" class="td-2">This is beautiful</td>
<td colspan="2" class="td-3">This is kinda of nothing</td>
</tr>
<tr>
<td class="td-4">This is Awesome</td>
<td colspan="3" class="td-5">This is Sick</td>
</tr>
</table>
Tables take their column widths from the first row. With this in mind I would approach this by creating an initial empty ghost row which does not have any colspan. This allows you to specify the exact width of each column. The row can be 1px high so it is barely visible.

How to change a transposed table to make it horizontally scrollable?

I am making a table for products comparison. It's a table with vertical rows, a header at the left side and two or more product descriptions in vertical rows inside table body. It should be horizontally scrollable in case if user chooses a lot of products.
I have used CSS from this answer to transpose a table (i.e. make vertical rows). And now I can't manage to add a horizontal scrollbar inside tbody in case if table exceeds the predefined width. I am looking for something similar to this question but applied to my transposed table.
Here's what I have now: JSFiddle and here's what happens when I limit the table width to 200px:
Try the combination of inline-blocks and nowrap:
table { border-collapse: collapse; white-space: nowrap; }
tr { display: block; float: left; }
th, td { display: block; border: 1px solid black; }
tbody, thead { display: inline-block; }
tbody {
width: 300px;
overflow-y: hidden;
overflow-x: auto;
}
<table>
<thead>
<tr>
<th>name</th>
<th>number</th>
</tr>
</thead>
<tbody>
<tr>
<td>James Bond</td>
<td>007</td>
</tr>
<tr>
<td>Lucipher</td>
<td>666</td>
</tr>
<tr>
<td>Jon Snow</td>
<td>998</td>
</tr>
</tbody>
</table>
table shouldn't be displayed as block, if it not necessary for your other layout
I've added white-space: nowrap to prevent line wrapping
I've displayed tbody and thead as inline-block so now you can manage them like inline elements.
If you would like to scroll the tbody only without thead, it might looks like this:
table { border-collapse: collapse; white-space: nowrap; }
tr { display: inline-block; }
th, td { display: block; border: 1px solid black; }
tbody, thead { display: inline-block; vertical-align: top; }
tbody {
width: 150px;
overflow-y: hidden;
overflow-x: auto;
white-space: nowrap;
}
tbody tr { margin-right: -5px;}
<table>
<thead>
<tr>
<th>name</th>
<th>number</th>
</tr>
</thead>
<tbody>
<tr>
<td>James Bond</td>
<td>007</td>
</tr>
<tr>
<td>Lucipher</td>
<td>666</td>
</tr>
<tr>
<td>Jon Snow</td>
<td>998</td>
</tr>
</tbody>
</table>
You can think about blocks layout instead. In fact you've already implemented itexcept the HTML what is a bad pattern.
This won't be valid HTML, but you could wrap tbody with a div, then give the div the width and overflow-x property.

HTML CSS Table displays rows at bottom instead of the top of the table

I am trying to finish formatting a table that is built dynamically. On the last page, when the table is sparse because there are fewer than the rows needed to fill the table, the rows are displayed at the bottom of the table space instead of the top. I've tried to correct this unsuccessfully. How can I display these rows at the top?
It doesn't seem to matter, but the table is built by the will_paginate Ruby gem. I say it doesn't matter because when I look at the HTML, it's just a table. There is nothing in there that is making this happen. The table size is formatted to display 10 rows. If there are only 3, they are just listed as 3 rows as you would expect. So, I think it is just an HTML/CSS formatting question.
The Table as it displays:
The SCSS:
.feeds {
list-style: none;
margin: 0;
text-align: left;
table-layout: fixed;
width: 700px;
height: 250px;
overflow: auto;
vertical-align: top;
li {
overflow: auto;
padding: 10px 0;
border-top: 1px solid $grayLighter;
&:last-child {
border-bottom: 1px solid $grayLighter;
}
}
table, thead, th, tr, tbody, tfoot {
vertical-align: top;
}
td {
vertical-align:top;
height: 1ex;
overflow-x: auto
}
}
The HTML:
<table class="feeds">
<tbody><tr>
<th id="url_cell"><a class="sortfield asc" href="/feeds?commit=Search&direction=desc&search=&sort=feed_url&utf8=%E2%9C%93">URL</a></th>
<th>Etag</th>
<th>Update date</th>
</tr>
<tr>
<td id="url_cell">http://www.skalith.net/rss</td>
<td id="etag_cell">RZWAFDMVHPXHWECK</td>
<td id="update_cell">August 5, 2013</td>
</tr>
<tr>
<td id="url_cell">http://www.viva.name/rss</td>
<td id="etag_cell">KFIEQYAUWMUHUJNY</td>
<td id="update_cell">August 5, 2013</td>
</tr>
</tbody></table>
The header row is filling out the space vertically (this is what it should do because of your table-layout. If you wrap it with <thead> and then only wrap the body of the table with <tbody> it will align it correctly. However, because you have table-layout: fixed, with height: 250px, the remaining rows will grow to make up the difference.
See: http://codepen.io/chrisrockwell/pen/gGmFq
Can you add a class to the table if it doesn't have a full set of rows? This way you could remove the height declaration.
Other options:
I'm guessing you need to have a set height but, if not, you could remove it.
Wrap the table in a <div> and assign your height and overflow to the div:
<div class="wrap">
<table class="feeds"></table>
</div>
.wrap {
height: 250px;
overflow: auto;
}
table {
/* just remove the height and overflow */
}
Here is an example of Option 2: http://codepen.io/chrisrockwell/pen/wpyfI

CSS table td width - fixed, not flexible

I have a table and I want to set a fixed width of 30px on the td's. the problem is that when the text in the td is too long, the td is stretched out wider than 30px. Overflow:hidden doesn't work either on the td's, I need some way of hiding the overflowing text and keeping the td width 30px.
<table cellpadding="0" cellspacing="0">
<tr>
<td>first</td><td>second</td><td>third</td><td>forth</td>
</tr>
<tr>
<td>first</td><td>this is really long</td><td>third</td><td>forth</td>
</tr>
</table>
It's not the prettiest CSS, but I got this to work:
table td {
width: 30px;
overflow: hidden;
display: inline-block;
white-space: nowrap;
}
Examples, with and without ellipses:
body {
font-size: 12px;
font-family: Tahoma, Helvetica, sans-serif;
}
table {
border: 1px solid #555;
border-width: 0 0 1px 1px;
}
table td {
border: 1px solid #555;
border-width: 1px 1px 0 0;
}
/* What you need: */
table td {
width: 30px;
overflow: hidden;
display: inline-block;
white-space: nowrap;
}
table.with-ellipsis td {
text-overflow: ellipsis;
}
<table cellpadding="2" cellspacing="0">
<tr>
<td>first</td><td>second</td><td>third</td><td>forth</td>
</tr>
<tr>
<td>first</td><td>this is really long</td><td>third</td><td>forth</td>
</tr>
</table>
<br />
<table class="with-ellipsis" cellpadding="2" cellspacing="0">
<tr>
<td>first</td><td>second</td><td>third</td><td>forth</td>
</tr>
<tr>
<td>first</td><td>this is really long</td><td>third</td><td>forth</td>
</tr>
</table>
you also can try to use that:
table {
table-layout:fixed;
}
table td {
width: 30px;
overflow: hidden;
text-overflow: ellipsis;
}
http://www.w3schools.com/cssref/pr_tab_table-layout.asp
It is not only the table cell which is growing, the table itself can grow, too.
To avoid this you can assign a fixed width to the table which in return forces the cell width to be respected:
table {
table-layout: fixed;
width: 120px; /* Important */
}
td {
width: 30px;
}
(Using overflow: hidden and/or text-overflow: ellipsis is optional but highly recommended for a better visual experience)
So if your situation allows you to assign a fixed width to your table, this solution might be a better alternative to the other given answers (which do work with or without a fixed width)
The above suggestions trashed the layout of my table so I ended up using:
td {
min-width: 30px;
max-width: 30px;
overflow: hidden;
}
This is horrible to maintain but was easier than re-doing all the existing css for the site. Hope it helps someone else.
This workaround worked for me...
<td style="white-space: normal; width:300px;">
Put a div inside td and give following style width:50px;overflow: hidden; to the div
Jsfiddle link
<td>
<div style="width:50px;overflow: hidden;">
<span>A long string more than 50px wide</span>
</div>
</td>
Chrome 37.
for non fixed table:
td {
width: 30px;
max-width: 30px;
overflow: hidden;
}
first two important! else - its flow away!
Just divide the number of td to 100%. Example, you have 4 td's:
<html>
<table>
<tr>
<td style="width:25%">This is a text</td>
<td style="width:25%">This is some text, this is some text</td>
<td style="width:25%">This is another text, this is another text</td>
<td style="width:25%">This is the last text, this is the last text</td>
</tr>
</table>
</html>
We use 25% in each td to maximize the 100% space of the entire table