This question is asking the opposite of what usually is desired: a fixed-width table that exceeds the width of its parent div(s).
In fact, the following implementation works as expected in Chrome/Chromium based browsers:
#myTable {
border-collapse: collapse;
/* Collapse borders */
border: 1px solid #ddd;
/* Add a grey border */
font-size: 18px;
/* Increase font-size */
width: 1040px;
/* Full-width */
table-layout: fixed;
}
/* This is intended to be in a media query for screens less than 768 px */
#myTable {
width: 745px;
/* Full-width + 225px */
}
td:nth-child(1),
th:nth-child(1) {
width: 51.9px;
}
td:nth-child(2),
th:nth-child(2) {
width: 158.783px;
}
td:nth-child(3),
th:nth-child(3) {
width: 225px;
}
td:nth-child(4),
th:nth-child(4) {
width: 78.2667px;
}
td:nth-child(5),
th:nth-child(5) {
width: 116.15px;
}
td:nth-child(6),
th:nth-child(6) {
width: 100.833px;
}
<div style="background-color: #f0f0f0; padding: 5px; width: 540px;">
<table id="myTable" cellspacing="0" cellpadding="2" border="1">
<thead>
<tr>
<th style="text-align:right;">col1</th>
<th>col2</th>
<th>col3</th>
<th>col4</th>
<th>col5</th>
<th>col6</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">col1</td>
<td>col2</td>
<td>col3</td>
<td>col4</td>
<td>col5</td>
<td>col6</td>
</tr>
<tr>
<td align="right">col1</td>
<td>col2</td>
<td>col3</td>
<td>col4</td>
<td>col5</td>
<td>col6</td>
</tr>
<tr>
<td align="right">col1</td>
<td>col2</td>
<td>col3</td>
<td>col4</td>
<td>col5</td>
<td>col6</td>
</tr>
<tr>
</tbody>
</table>
</div>
However, when checking in Firefox and Microsoft Edge, the specified table/column widths are not honored, instead conforming to the parent div.
Most suggested solutions I've found appear to recommend applying table-layout: fixed; rule to the table, however, it's already applied in this example to no effect.
Anyone have any ideas?
Typically I use a class/ID wrapper and based on the max-width of the wrapper assign the table's width:100% which will then create the scrollbar effect with the overflow:scroll hidden property which is what I think you're looking for? If you want vertical scroll.
I never use the table-layout fixed attribute which would seem to be the problem., but then again I don't use it.
Just assign the table CSS at width:100% and use the #media with the table's wrapper based on screen size. This will work great for the column's adaptations too. You will just have to adjust accordingly.
By assigning the parent div's width inline you are creating somewhat of a roadblock for your responsive css capabilities as you cannot override inline styles. I'd change that approach.
#myTable {
border-collapse: collapse;
/* Collapse borders */
border: 1px solid #ddd;
/* Add a grey border */
font-size: 18px;
/* Increase font-size */
width: 100%;
overflow: scroll hidden;
}
.tbl_wrapper{max-width: 540px;}
#media screen and (max-width: 768px){
<!--Do some edits based on screen size //-->
.tbl_wrapper{max-width: 540px;}
}
#media screen and (max-width: 1040px){
<!--Do some edits based on screen size //-->
.tbl_wrapper{max-width: 640px;}
}
<div class="tbl_wrapper" style="background-color: #f0f0f0; padding: 5px;">
<table id="myTable" cellspacing="0" cellpadding="2" border="1">
</table>
</div>
Related
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.
I've been trying to get the column width on mobile to become 50%, but everything I've tried hasn't worked. This is the css.
#media (max-width: 768px) {
#customTable tbody { float: left; }
#customTable thead { float: left; }
#customTable thead { min-width: 120px }
#customTable td { display: block }
#customTable th { display: block }
#customTable {table-layout: fixed;}
#customTable td {width: 50%;}
#customTable td {height: 20px; overflow: hidden;}
#customTable div {height: 20px;overflow: hidden;}
}
Here is the html
<div class="table">
<table id="customTable" class="table" style="width: 100%">
<thead>
<tr>
<th><LABEL>Test 1</LABEL></td>
<th><LABEL>Test 2</LABEL></td>
<th><LABEL>Test 3</LABEL></td>
<th><LABEL>Test 4</LABEL></td>
</tr>
</thead>
<tr>
<td><LABEL>Test</LABEL></td>
<td><LABEL>Test</LABEL></td>
<td><LABEL>Test</LABEL></td>
<td><LABEL>Test</LABEL></td>
</tr>
</table>
</div>
If you want column width to adjust, using tables, you actually need to tell it NOT to behave as a table any more.
e.g. use display:block and width:50% on your #customTable td and work from there as though they are block elements.
If you want to adjust the width of columns, you should adjust the width of the th in thead.
this works on me fine.
Here is a link to a jsfiddle that works in the modern browsers I tested (Chrome, Firefox and IE11) except for Safari. It seems that Safari does not support adding a calc style containing a percentage to a col element within a table's colgroup. Below is a code excerpt from the jsfiddle, the col element with id "col1" has a width style of "calc(100% - 70px)". On Safari the Month column is hidden whereas on other modern browsers such as Chrome, Firefox and IE11 it is visible. If we remove this style then things behave as expected on Safari where the Month column takes up all the available width and the Savings column stays a static 70px width. Note that the box-sizing for all elements is border-box which is specified in the css.
<table>
<colgroup>
<col id="col1" style="width: calc(100% - 70px)" />
<col id="col2" style="width: 70px" />
</colgroup>
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$100.58</td>
</tr>
<tr>
<td>February</td>
<td>$80.67</td>
</tr>
</tbody>
</table>
Here is the related CSS:
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
table {
border-color: #dbdcde;
table-layout: fixed;
width: 100%;
margin: 0;
padding: 0;
border: 0;
vertical-align: baseline;
border-collapse: collapse;
border-spacing: 0;
}
th,
td {
text-align: left;
padding: 0 0 0 1rem;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
vertical-align: middle;
}
th {
font-weight: bold;
font-size: larger;
background-color: #dbdcde;
border: 0.1rem solid #aaa;
}
tr {
border: 0.1rem solid #dbdcde;
color: #000000;
}
td {
padding-left: 1rem;
}
tbody tr:nth-child(even) {
background-color: #f8f8f8;
}
Your problem is width: calc(100% - 70px) as this cant be done by CSS as it doesnt support calculations in general and no percentage minus pixel in specific.
Just leave this out (don't set any width) and keep setting the other column to 70px.
This will make second column 70px and the first will be screen width - 70px just as you wanted
The problem is, that all of elements (main container, table container and table itself) must have 100% height. Table itself has slightly smaller size in Chrome and IE, than in Firefox, which causes a small gap between .table-container and table borders.
Do somebody know how to fix this? I've spent almost all day for this, and can't simply find a solution. Would be grateful for any help.
Here is a link for fiddle with my current problem: fiddle link
<div class="container">
<div class="buttons">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="table-container">
<table>
<thead>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</thead>
<tbody>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
</tr>
<tr>
<td>aa</td>
<td>bb</td>
<td>cc</td>
<td>dd</td>
</tr>
<tr>
<td>aaa</td>
<td>bbb</td>
<td>ccc</td>
<td>ddd</td>
</tr>
</tbody>
</table>
</div>
</div>
and CSS:
html, body, .container, .table-container, table
{
height: 100%;
}
.container
{
display: block;
width: 500px;
margin: 0 auto;
text-align: center;
}
.table-container
{
width: 100%;
border: 1px solid red;
padding-top: 30px;
margin-top: -30px;
}
.buttons
{
height: 30px;
}
.buttons div
{
box-sizing: border-box;
-moz-box-sizing: border-box;
width: 25%;
float: left;
height: 30px;
border: 1px solid black;
}
table
{
border: 1px solid black;
width: 100%;
border-spacing: 0;
}
thead td, tbody td
{
width: 25%;
height: 25%;
}
Update: added padding and negative margin for .table-container
Update2: added border-spacing and border-collapse to an example. Still not working properly.
Update3: Now it works here, which means, that i couldn't fully reproduce my bug to show it to you :( But in general, the problem is, that the height of 5x5 table in Chrome is less from FF on 22px, which is 22px gap between table container and a table itself. Each cell has around 4px + to table height in Firefox.
It appears to be the border spacing on the table.
Apply this CSS to the table element:
border-spacing: 0;
Be aware that
width: 100%;
does not work the exactly the same way as
height: 100%;
does.
While width: 100% grabs 100% of the parent's width even if there has not been an explicit setting of that width, height: 100% works only on elements whose parents have a fixed height.
Simply speaking, %-width applies dynamically, while %-height applies to static parents container's heights.
I came here because I was having the same problems with different 100% height of table for Chrome and Firefox. The fix is to add a border the table. You can just add border-bottom as well and it won't actually show up in Chrome, but will fix the height issue.
table
{
**border-bottom: 1px solid black;**
width: 100%;
border-spacing: 0;
}
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