I have a table on my layout that has 5 columns, 3 of them should be fixed width in px and the other 2 should be fluid.
It sounded simple at first, but the problem is the two fluid columns should behave differently.
The last column should stretch as much as it can to fit its contents, so they are never hidden, but shouldn't ever leave empty space. And the middle column should occupy all the free space it can find, but also overflow to hidden in case the last one needs to grow larger.
I tried to make this work with css, but I couldn't manage to make it work... Is there a way to do this with pure css or I need js?
EDIT
That's what I got so far:
HTML
<table>
<tr>
<td class="fixed">fixed</td>
<td class="fixed">fixed</td>
<td class="fluid hidden">fluid</td>
<td class="fixed">fixed</td>
<td class="fluid visible">this content should always be visible</td>
</tr>
</table>
CSS
table{
width: 100%;
table-layout: fixed;
}
td{
padding: 10px;
white-space: nowrap;
}
.fixed{
background-color: #ddd;
width: 60px;
}
.fluid{
background-color: #aaa;
}
.visible{
}
.hidden{
overflow:hidden;
}
http://jsfiddle.net/KzVbX/
It works almost as expected. Except for the last column.
Maybe I can help, maybe not.
First, I would use divs instead of tr/td. I honestly don't have a need for using tables since CSS was introduced, and I'm rather surprised that some people still do. But there could be a reason, so please do not take that as criticism.
If you use divs, then edit this section of your code:
.visible {
overflow:visible;
min-width: 210px;
}
That will make sure that the div is at least 210 pixels wide no matter what. It should work.
BTW, if this is the only table on the page and that div or td is unique in the sense that it has a minimum height, then you may want to use an id instead of a class. That will make your code cleaner and more elegant.
Hope this helps.
If you don't need wrapping do this:
td{
padding: 10px;
}
If wrap is desired, you need to change width of table to auto and add min-width parameter.
table{
width: auto;
min-width: 100%;
table-layout: fixed;
}
Try this and see if it is close to what you are looking for:
DEMO - http://jsfiddle.net/WGpB3/
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td style="width:60px;"> </td>
<td style="width:60px;"> </td>
<td style="overflow:hidden;"> </td>
<td style="width:60px;"> </td>
<td style="overflow:visible;"> </td>
</tr>
Made changes to CSS file
*DEMO - http://jsfiddle.net/KzVbX/2/
table{
width: 100%;
table-layout:fixed;
}
td{
padding: 10px;
}
.fixed{
background-color: #ddd;
width: 60px;
}
.fluid{
background-color: #aaa;
}
.visible{
overflow:visible;
}
.hidden{
overflow:hidden;
max-width:20%;
white-space:nowrap;
}
Related
THIS IS THE FIDDLE
HTML:
<input id="APP" type="button" value="Append"/>
<div id="wrapper">
<table cellpadding="0" cellspacing="0">
<tr>
<td style="width: 100%; height: 30px; background-color: rgb(230,230,230)">
</td>
</tr>
<tr>
<td style="height: 100%;" align="center">
<div id="ContentWrapper">
</div>
</td>
</tr>
</table>
</div>
CSS:
#wrapper{
width: 80%;
height: 300px;
background-color: rgb(25,25,25);
}
#wrapper table{
width: 100%;
height: 100%;
}
#wrapper table td{
vertical-align: middle;
}
#ContentWrapper{
width: 98%;
height: 95%;
border: 1px solid blue;
color: rgb(255,255,255);
text-align: left;
overflow-y: auto;
}
jQuery:
$("#APP").on("click",function(){
$("#ContentWrapper").append("Test<br/>Test<br/>Test<br/>Test<br/>Test<br/>Test<br/>Test<br/>Test<br/>Test<br/>Test<br/>");
});
If you test this in Chrome or any other browser you'll see that it works as it should. But in Firefox, if you keep pressing the "Append" button, the div's height will change with the content despite of the fact that the div's overflow is set to auto.
I know this would work if I would set the div's dimensions in px instead of percentage, but I don't want to do that. I made that fiddle as an example to pinpoint the issue but in my original code the wrapper that holds the table is responsive and I have to keep the dimensions in percentage.
Interesting find. It smacks of a bug in Firefox.
However, there is a workaround: assign an explicit height to the tbody in the table as well.
#wrapper table, #wrapper tbody {
width: 100%; height:100%;
}
See updated fiddle. Note that I also changed some other properties, because the browsers got confused by the top td being 30px high and the bottom one 100%. I changed that to 10% and 90%; you may have to use other values in your situation. (Maybe use calc(...) for one of them.)
I have a basic table in a container. The table will have about 25 columns. I am trying to add a horizontal scroll bar on overflow of the table and am having a really tough time.
What is happening now, is the table cells are accommodating the cells contents by automatically adjusting the height of the cell and maintaining a fixed table width.
I appreciate any suggestions on why my method is not working on how to fix this.
CSS
.search-table-outter {margin-bottom:30px; }
.search-table{table-layout: fixed; margin:40px auto 0px auto; overflow-x:scroll; }
.search-table, td, th{border-collapse:collapse; border:1px solid #777;}
th{padding:20px 7px; font-size:15px; color:#444; background:#66C2E0;}
td{padding:5px 10px; height:35px;}
tr:nth-child(even) {background: #f5f5f5;}
tr:nth-child(odd) {background: #FFF;}
HTML
<div class="search-table-outter wrapper">
<table class="search-table inner">
<tr>
<th>Col1</th>
<th>col2</th>
<th>col3</th>
<th>col4</th>
<th>col5</th>
<th>col5</th>
</tr>
<?php echo $rows; ?>
</table>
</div>
JS fiddle (Note: if possible, I would like the horizontal scroll bar to be in the container with the red border):
http://jsfiddle.net/ZXnqM/3/
I think your overflow should be on the outer container. You can also explicitly set a min width for the columns. Like this:
.search-table-outter { overflow-x: scroll; }
th, td { min-width: 200px; }
Fiddle: http://jsfiddle.net/5WsEt/
The solution for those who cannot or do not want to wrap the table in a div (e.g. if the HTML is generated from Markdown) but still want to have scrollbars:
table {
display: block;
max-width: -moz-fit-content;
max-width: fit-content;
margin: 0 auto;
overflow-x: auto;
white-space: nowrap;
}
<table>
<tr>
<td>Especially on mobile, a table can easily become wider than the viewport.</td>
<td>Using the right CSS, you can get scrollbars on the table without wrapping it.</td>
</tr>
</table>
<table>
<tr>
<td>A centered table.</td>
</tr>
</table>
Explanation: display: block; makes it possible to have scrollbars. By default (and unlike tables), blocks span the full width of the parent element. This can be prevented with max-width: fit-content;, which allows you to still horizontally center tables with less content using margin: 0 auto;. white-space: nowrap; is optional (but useful for this demonstration).
A solution that nobody mentioned is use white-space: nowrap for the table and add overflow-x to the wrapper.
(http://jsfiddle.net/xc7jLuyx/11/)
CSS
.wrapper { overflow-x: auto; }
.wrapper table { white-space: nowrap; }
HTML
<div class="wrapper">
<table></table>
</div>
This is an ideal scenario if you don't want rows with multiple lines.
To add break lines you need to use <br/>.
Unless I grossly misunderstood your question, move overflow-x:scroll from .search-table to .search-table-outter.
http://jsfiddle.net/ZXnqM/4/
.search-table-outter {border:2px solid red; overflow-x:scroll;}
.search-table{table-layout: fixed; margin:40px auto 0px auto; }
As far as I know you can't give scrollbars to tables themselves.
.search-table-outter {border:2px solid red; overflow-x:scroll;}
.search-table{table-layout: fixed; margin:40px auto 0px auto; }
.search-table, td, th{border-collapse:collapse; border:1px solid #777;}
th{padding:20px 7px; font-size:15px; color:#444; background:#66C2E0;}
td{padding:5px 10px; height:35px;}
You should provide scroll in div.
On a responsive site for mobiles the whole thing has to be positioned absolute on a relative div. And fixed height. Media Query set for relevance.
#media only screen and (max-width: 480px){
.scroll-wrapper{
position:absolute;
overflow-x:scroll;
}
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
I have a table cell with an anchor tag and I want the width (and height) of the anchor tag to fill the width of the containing table cell. Intuition doesn't seem to work on this. Please help.
HTML:
<table>
<tr>
<td class="width_is_100px">
<span class="make_width_100px">Some Text</span>
</td>
</tr>
</table>
CSS: (doesn't work)
.make_width_100px {
width:100px !important;
}
The anchor tag is only as wide as the text "Some Text". I want the user to be able to click anywhere inside the table cell and trigger the link. No javascript please.
Try it:
a {
display: block;
height: 100%;
width: 100%;
}
Make your <a> element a block element (it's inline by default):
.width_is_100px a {
display:block;
}
None of these answers work properly to fill the width and height when the text wraps.
The only answer I found that produces a proper result filling the height and width of the cell right to the edges is this one:
td {
overflow: hidden;
}
td > a {
display: block;
margin: -10em;
padding: 10em;
}
<table>
<tr>
<td class="width_is_100px">
<div style="width:100%;">Some Text</div>
</td>
</tr>
</table>
this is probably too late but, this worked for me in tables with different content height.
html
<table class="example">
<tr>
<td>small content</td>
<td>multiline<br>content</td>
<tr>
</table>
css
table{
padding:10px; /*for example*/
}
table.example td{
overflow:hidden;
padding:0px; /*you can need !important*/
}
table.example a{
display: block;
height: 100%;
width: 100%;
padding: 10px 10px 500px 10px; /*table default padding except on bottom 500px for example*/
margin-bottom: -490px; /*same as bottom padding - table padding*/
}
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