Horizontal scroll on overflow of table - html

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;
}

Related

Display table with 'display: block;' at the center of a page

I have a very large table that doesn't fit a page and I added horizontal scrollbar like this:
.table-x-scroll {
display: block;
overflow-x: auto;
white-space: nowrap;
}
And <table class="table-x-scroll">...</table>
But I also want my table to be in the center of a page. I used to use align="center" but now, when I added display: block it doesn't work. Table is always at the left. How can I do these two things at the same time?
Would be better to use a wrapper for the table and add overflow to that wrapper not the table it self. This way you can control the behavior better.
And also use margin:0 auto on table to center it horizontally. No need for display:block
See below or better in jsFIddle
table {
border: 2px solid red;
width: 300px;
margin: 0 auto;
}
td {
border: 1px solid red;
}
.wrapper {
width: 100%;
overflow-x: auto;
}
<div class="wrapper">
<table>
<tr>
<td>a</td>
<td>a</td>
</tr>
<tr>
<td>a</td>
<td>a</td>
</tr>
<tr>
<td>a</td>
<td>a</td>
</tr>
</table>
</div>
Try to add margin-left:auto; margin-right:auto; in your style and specify the width
.table-x-scroll {
display: block;
overflow-x: auto;
white-space: nowrap;
margin-left:auto;
margin-right:auto;
width:50%;
}
<table class="table-x-scroll" border="1">
<tbody>
<row>
<td>AA</td>
<td>BB</td>
</row>
</tbody>
</table>
Try adding Margin : 0 auto
.table-x-scroll {
display: block;
overflow-x: auto;
white-space: nowrap;
margin : 0 auto
}
I can see that others have answered the aligning problem. But i couldn't see the "Doesn't fit to page" error so here is a solution for that:
In the css you specified overflow-x: auto;
If you change that to overflow-x scroll; it will make it scale to whatever size you choose to specify. and make it scrollable. The scrollbar within is possible to edit/remove, but it will still scroll ^^
I personally recommend the vw,vh values
example:
.bunnydiv {font-size: 1vw, 1vh;} This will make it scale to 1% of the width and of the height of the monitor. This can be specified with either vh, vw, vh and vw. Not both paramaters are needed, and it will scale accordingly :-)
I found some code I made myself some time ago, use this as a reference :-)
.div_scroll
{overflow-y: scroll;
display: block;
top:0px;
right: 0px;
position: absolute;
transform: translate(-23.6vw, 15vh);
border: 3px solid #dddddd;
max-height: 75vh;
width: 76vw;
margin: 0px;
background-color: white;}
It's copy-pasted, and setup this way for readability. Don't format you code like this unless it works better for you :-)

Responsive table column with CSS

There's a table with three columns, the first and the last are fixed and the middle one should be fluid.
The problem - inside of the middle column there's text with nowrap and it prevents it from being fluid.
How that could be done?
How it looks on wide page (correct behaviour):
How it should look on narrow page:
How it actually looks on narrow page (incorrect behaviour, see scrollbar):
The code https://jsfiddle.net/8c9msa71/
td {
border: 1px solid black;
}
.fluid {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<table>
<tr>
<td>first</td>
<td class="fluid">very-very-very-very-very-very-long-second-text</td>
<td>third</td>
</tr>
</table>
you can do it by adding a div
td { border: 1px solid black; }
.fluid { position: relative; width: 70%;}
.fluid div {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
position: absolute;
left: 5px;
right: 5px;
top: 0;
}
<table width="100%">
<tr>
<td>first</td>
<td class="fluid">
<div>very-very-very-very-very-very-long-second-text</div>
</td>
<td>third</td>
</tr>
</table>
I noticed the solution to add a div.
If you don't want to or need to add a div, you could use max-width.
Explanation:
Your first problem is that none of your elements has a width attribute set. To start with I would set a max-width of your "very-very-long-second-text". For example you can add max-width: 60vw; to your .fluid. If you're not familiar with the vw syntax, read more here: https://www.w3.org/TR/css3-values/#vw
By only adding this, you'll be almost there. You'll still have one problem left: On very small devices / resolutions, you notice that your third table-data, <td></td> will disappear out of visible area.
Instead of collapsing ALL the content, I recommend using display: inline-block; on your table data <td></td>. What this does is that it will display your table data inline as long as they have enough space to be inline. In addition a small part of the information will be visible, instead of the result of NO information visible at all. When the available area becomes smaller (i.e. resizing the window), they will start jumping down one by one.
Full CSS:
td {
border: 1px solid black;
display: inline-block;
}
.fluid {
max-width: 60vw;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

Fluid and Fixed Columns Table

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;
}

Overflow: auto does not work in Firefox

I have a table. Its <td> have overflow: auto.
The width is set to be 100px. In Firefox only, text that exceeds 100px is not hidden and replaced with a scrollbar.
How can I hide content and have a scrollbar when it exceeds the width of its container?
http://jsfiddle.net/be6tM/10/
this question from here maybe solve your problem
nickb answer: "Try wrapping it in a <div>. I'm pretty sure the overflow attribute is not defined for a <td> element, at least in HTML4 it's not."
try to put your overflow:auto to the wrapper hope this can help you
pre, div {
width:100%;
overflow: auto !important;
}
working demo
The easier way to do this would be to add this to the Html
<td class="first">
<div>Don ovonMrLongNameIsMe!!!</div>
</td>
and this to the CSS
div {
overflow:auto;
}
td {
border: 1px solid rgb(0,0,0);
min-width: 100px;
max-width: 100px;
}
Working Example:
div {
overflow:auto;
}
td {
border: 1px solid rgb(0,0,0);
min-width: 100px;
max-width: 100px;
}
<table>
<tr>
<td class="first">
<div>Don ovonMrLongNameIsMe!!!</div>
</td>
</tr>
</table>

How to do I get an anchor tag to fill the width of a table cell?

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*/
}