I have a simple html table where i am trying to get the body to extend to the end of the headers/table. I need it to have horizontal scroll only for the body and have fixed headers.
I have a fiddle here that shows the issue.
http://jsfiddle.net/ZbdZe/54/
<table border="1" width="100%">
<thead >
<tr>
<th id="th1" width="50%">first</th>
<th id="th2" width="50%" >last</th>
</tr>
</thead>
<tbody style="display: block; border: 1px solid green; height: 530px; overflow-y: scroll">
<tr>
<td headers="th1" >Hello</td>
<td headers="th2" >World</td>
</tr>
</tbody>
</table>
Right now it extendes only the first heading. I need it to extend the full width of the table with scroll. Thank you
This is to have fixed table header created by another table
Second table for the body of the table with scroll
table{
border-collapse:collapse;
width:100%
}
tbody{
border: 1px solid green;
table-layout: fixed;
}
div{
height: 66px; /*adjust table body*/
overflow-y: scroll
}
<table border="1">
<thead>
<tr>
<th id="th1" width="39%">first</th>
<th id="th2" width="50%" >last</th>
</tr>
</thead>
</table>
<div>
<table border=1>
<tbody>
<tr>
<td headers="th1" >Hello</td>
<td headers="th2" >World</td>
</tr>
<tr>
<td headers="th1" >Hello</td>
<td headers="th2" >World</td>
</tr>
<tr>
<td headers="th1" >Hello</td>
<td headers="th2" >World</td>
</tr>
<tr>
<td headers="th1" >Hello</td>
<td headers="th2" >World</td>
</tr>
</tbody>
</table>
</div>
Related
In my app there's an option to add auto-generated tables and I need to make them scroll-able (overflow-x) when the table is wider than the containing parent. My solution works for wider tables, however not for tables that are narrower than the parent, I need them to stretch across the parent container.
CodePen here.
HTML:
<div id="parent">
<table cellspacing="0" width="100%" class="scroll">
<tbody>
<tr class="ms-rteTableHeaderRow-default">
<th rowspan="1" colspan="3">dasdasdas</th>
<th rowspan="1" colspan="3">dasdasdas</th>
<th rowspan="1" colspan="3">dasdasdas</th>
<th rowspan="1" colspan="3">dasdasdas</th>
<th rowspan="1" colspan="3">dasdasdas</th>
<th rowspan="1" colspan="3">dasdasdas</th>
<th rowspan="1" colspan="3">dasdasdas</th>
</tr>
<tr class="ms-rteTableOddRow-default">
<th class="ms-rteTableFirstCol-default" rowspan="1" colspan="1">dasdasdas<br>dasdasdas<br>dasdasdas</th>
<td class="ms-rteTableOddCol-default">dasdasdas dasdasdas </td>
<td class="ms-rteTableEvenCol-default">dasdasdas</td>
<td class="ms-rteTableOddCol-default"> </td>
</tr>
<tr class="ms-rteTableEvenRow-default">
<th class="ms-rteTableFirstCol-default" rowspan="1" colspan="4">dasdasdas</th>
<td class="ms-rteTableOddCol-default">dasdasdas</td>
<td class="ms-rteTableEvenCol-default">dasdasdas</td>
<td class="ms-rteTableEvenCol-default">dasdasdas</td>
</tr>
<tr class="ms-rteTableOddRow-default">
<th class="ms-rteTableFirstCol-default" rowspan="1" colspan="1">dasdasdas</th>
<td class="ms-rteTableOddCol-default">dasdasdas<br>dasdasdas</td>
<td class="ms-rteTableEvenCol-default">dasdasdas<br>dasdasdas</td>
</tr>
<tr class="ms-rteTableFooterRow-default">
<th class="ms-rteTableFooterFirstCol-default" rowspan="1" colspan="4">dasdasdas</th>
<td class="ms-rteTableFooterOddCol-default" rowspan="1">dasdasdas</td>
<td class="ms-rteTableFooterEvenCol-default" rowspan="1">dasdasdas</td>
<td class="ms-rteTableFooterOddCol-default" rowspan="1">dasdasdas</td>
<td class="ms-rteTableFooterEvenCol-default" rowspan="1">dasdasdas</td>
<td class="ms-rteTableFooterOddCol-default" rowspan="1">dasdasdas</td>
<td class="ms-rteTableFooterEvenCol-default" rowspan="1">dasdasdas</td>
</tr>
</tbody>
</table>
<table cellspacing="0" width="100%" class="ms-rteTable-default scroll" style="font-size:1em">
<tbody>
<tr>
<th rowspan="1" colspan="1">title 1</th>
<th rowspan="1" colspan="1">this table should span across the parent</th>
</tr>
<tr>
<th rowspan="1" colspan="1">content</th>
<td >contentcontentcontentcontentcontentcontentcontentcontentcontentcontent</td>
</tr>
</tbody>
</table>
</div>
CSS:
td,th{
border:1px solid black;
}
#parent{
width: 700px;
}
table{
border:1px solid blue;
padding: 10px;
}
.scroll{
display: block;
overflow-x: auto;
}
tr {
display: table-row;
vertical-align: inherit;
border-color: inherit;
}
tbody {
display: table-row-group;
vertical-align: middle;
border-color: inherit;
}
I took a look at your CodePen and everything looks fine to me. What I do see happening is that your cells aren't taking up the entire width. The tables (blue borders) are spanning the entire width, but your cells width fit to it's content. if you add the following to you cells
td, th {
width:100%;
}
Your table cells will span to the full width of the table. That being said, maybe you only want the second cell to span to the full width. Then just add a custom class with width: 100%; .
I have a table structured like the one below and it is a on page with a gray background. The problem I am having is removing the cell borders.
<table width="510">
<tbody>
<tr>
<td style="background-color: #fff;" colspan="2" width="510"></td>
</tr>
<tr>
<td style="background-color: #fff;"></td>
<tdstyle="background-color: #fff;"></td>
</tr>
<tr>
<td style="background-color: #fff;"></td>
<td style="background-color: #fff;"></td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
</tbody>
</table>
This is what it looks like no matter what I have tried such as adding a .no-border class border:0; to the <tr> or adding inline css to the <td>. It still comes out like this...
How can I remove the border around all the cells?
You need to remove the cellspacing like this:
table {
border-spacing: 0;
border-collapse: collapse;
}
This is equivalent for the cellspacing="0" HTML attribute. See this example:
body {
background: grey;
}
table {
border-spacing: 0;
border-collapse: collapse;
}
<table width="510">
<tbody>
<tr>
<td style="background-color: #fff;" colspan="2" width="510">t</td>
</tr>
<tr>
<td style="background-color: #fff;">t</td>
<td style="background-color: #fff;">t</td>
</tr>
<tr>
<td style="background-color: #fff;">t</td>
<td style="background-color: #fff;">t</td>
</tr>
<tr>
<td colspan="2">t</td>
</tr>
</tbody>
</table>
I have a table from n columns. Each cell has a class column-n.
I want to add on table class hide-column-n and hide all cells with class column-n.
Is this possible do it through css?
Example:
.table {
border: 1px solid black;
}
.table thead{
background-color: lightgray;
}
.table td{
border: 1px solid gray;
width: 50px;
}
<table class="table">
<thead>
<tr>
<td class="column-1">One</td>
<td class="column-2">Two</td>
<td class="column-3">Three</td>
<td class="column-x">...</td>
<td class="column-n">N</td>
</tr>
</thead>
<tbody>
<tr>
<td class="column-1">One</td>
<td class="column-2">Two</td>
<td class="column-3">Three</td>
<td class="column-x">...</td>
<td class="column-n">N</td>
</tr>
<tr>
<td class="column-1">One</td>
<td class="column-2">Two</td>
<td class="column-3">Three</td>
<td class="column-x">...</td>
<td class="column-n">N</td>
</tr>
<tr>
<td>...</td>
</tr>
</tbody>
</table>
Here is example https://jsfiddle.net/xr21kwrh/
<table class="table">
<thead>
<tr>
<th class=column-1">One</th>
<th class"column-2">Two</th>
...
<th class="column-n">N</th>
</tr>
</thead>
<tbody>
<tr>
<td class=column-1">One</t>
<td class"column-2">Two</td>
...
<td class="column-n">N</td>
</tr>
<tr>
<td class=column-1">One</t>
<td class"column-2">Two</td>
...
<td class="column-n">N</td>
</tr>
...
</tbody>
</table>
<style>
th:nth-child(2){display: none;}
td:nth-child(2){display: none;}
</style>
<table class="table">
<thead>
<tr>
<th class=column-1">One</th>
<th class"column-2">Two</th>
...
<th class="column-n">N</th>
</tr>
</thead>
<tbody>
<tr>
<td class=column-1">One</t>
<td class"column-2">Two</td>
...
<td class="column-n">N</td>
</tr>
<tr>
<td class=column-1">One</t>
<td class"column-2">Two</td>
...
<td class="column-n">N</td>
</tr>
...
</tbody>
</table>
<style>
.column-n{ display-none !important; }
</style>
If you use sass/scss you can do so (in SCSS):
#for $i from 1 through 10 {
.hide-column-#{$i} .column-#{$i} {
display: none;
}
}
it will compile to:
.hide-column-1 .column-1 {
display: none;
}
.hide-column-2 .column-2 {
display: none;
}
....
.hide-column-n .column-n {
display: none;
}
then use it like expected:
<table class="table hide-column-1 hide-column-2">
codepen demo here
The following HTML code displays the value of Heading9 and Heading10 in a separate row instead of displaying right after Heading8 (see ActualOutput image)
Code 1:
<html><body bgcolor="#E6E6FA">
<TABLE cellpadding="5" style="border: 1px solid #000000; border-collapse: collapse;" border="2">
<TH>Heading1</TH>
<TH>Heading2</TH>
<TH>Heading3</TH>
<TH>Heading4</TH>
<TH>Heading5</TH>
<TH>Heading6</TH>
<TH>Heading7</TH>
<TH>Heading8</TH>
<TH>Heading9</TH>
<TH>Heading10</TH>
<TH colspan="20">Heading11</TH>
</TR>
<tbody>
<TR>
<TD rowspan="2">knnjkn</TD>
<TD>ceecev</TD>
<TD>lnlnlkn</TD>
<TD>lknlkn</TD>
<TD>lknkn</TD>
<TD>kjnkljnk</TD>
<TD>lknlkn</TD>
<TD>kjnlkn</TD>
<TD>kjbkn</TD>
</TR>
<TR>
<TD>jknklnk</TD>
<TD>kjnlknknm</TD>
<TD>jnkj n</TD>
<TD>lnnkkl</TD>
<TD>kjnknkj</TD>
<TD>sdwewcw</TD>
<TD>qwdcwcwc</TD>
<TD>csdcs</TD>
</TR>
<TD rowspan="2" bgcolor=#00BFFF>SUCCESS</TD>
<TD rowspan="2" bgcolor=#00BFFF>SUCCESS</TD>
<TD rowspan="2" bgcolor=#00BFFF>SUCCESS</TD>
</tbody>
<tbody>
<TR>
<TD rowspan="2">ddccs</TD>
<TD>csdcs</TD>
<TD>csdcs</TD>
<TD>cswewc</TD>
<TD>csdcsdc</TD>
<TD>cdwdc</TD>
<TD>cdsc</TD>
<TD>zcxascsac</TD>
<TD>csdcdc</TD>
</TR>
<TR>
<TD>dadcc</TD>
<TD>csdc</TD>
<TD>cacsc</TD>
<TD>cdwcc</TD>
<TD>csdd</TD>
<TD>csdc</TD>
<TD>sdcsdc</TD>
<TD>cdscsd</TD>
</TR>
<TD rowspan="2" bgcolor=#00BFFF>SUCCESS</TD>
<TD rowspan="2" bgcolor=#00BFFF>SUCCESS</TD>
<TD rowspan="2" bgcolor=#00BFFF>SUCCESS</TD>
</tbody>
</TABLE>
</html>
Actual Output:
I can get the desired output by changing the code 1 to code 2, however, my bash script gathers Heading9 and Heading10 data only after Heading8 is framed for each body, so I cannot inject Heading9 and 10 in between.
Appreciate if experts can provide a better solution to get the expected output by restructuring the code 1. Unfortunately, code 2 doesn't work in my scenario.
Code 2:
<html><body bgcolor="#E6E6FA">
<TABLE cellpadding="5" style="border: 1px solid #000000; border-collapse: collapse;" border="2">
<TH>Heading1</TH>
<TH>Heading2</TH>
<TH>Heading3</TH>
<TH>Heading4</TH>
<TH>Heading5</TH>
<TH>Heading6</TH>
<TH>Heading7</TH>
<TH>Heading8</TH>
<TH>Heading9</TH>
<TH>Heading10</TH>
<TH colspan="20">Heading11</TH>
</TR>
<tbody>
<TR>
<TD rowspan="2">knnjkn</TD>
<TD>ceecev</TD>
<TD>lnlnlkn</TD>
<TD>lknlkn</TD>
<TD>lknkn</TD>
<TD>kjnkljnk</TD>
<TD>lknlkn</TD>
<TD>kjnlkn</TD>
<TD>kjbkn</TD>
<TD rowspan="2" bgcolor=#00BFFF>SUCCESS</TD>
<TD rowspan="2" bgcolor=#00BFFF>SUCCESS</TD>
<TD rowspan="2" bgcolor=#00BFFF>SUCCESS</TD>
</TR>
<TR>
<TD>jknklnk</TD>
<TD>kjnlknknm</TD>
<TD>jnkj n</TD>
<TD>lnnkkl</TD>
<TD>kjnknkj</TD>
<TD>sdwewcw</TD>
<TD>qwdcwcwc</TD>
<TD>csdcs</TD>
</TR>
</tbody>
<tbody>
<TR>
<TD rowspan="2">ddccs</TD>
<TD>csdcs</TD>
<TD>csdcs</TD>
<TD>cswewc</TD>
<TD>csdcsdc</TD>
<TD>cdwdc</TD>
<TD>cdsc</TD>
<TD>zcxascsac</TD>
<TD>csdcdc</TD>
<TD rowspan="2" bgcolor=#00BFFF>SUCCESS</TD>
<TD rowspan="2" bgcolor=#00BFFF>SUCCESS</TD>
<TD rowspan="2" bgcolor=#00BFFF>SUCCESS</TD>
</TR>
<TR>
<TD>dadcc</TD>
<TD>csdc</TD>
<TD>cacsc</TD>
<TD>cdwcc</TD>
<TD>csdd</TD>
<TD>csdc</TD>
<TD>sdcsdc</TD>
<TD>cdscsd</TD>
</TR>
</tbody>
</TABLE>
</html>
Expected Output:
Add Following may this help...
<style>
table
{
border: 1px solid #000000;
border-collapse: collapse;
display:table;
position:relative;
width:100%;
}
table tr
{
display: table-row;
height: 35px;
line-height: 30px;
width: 100%;
}
table td
{
direction: ltr;
display: table-cell;
margin: 0;
padding: 5px;
table-layout: fixed;
}
</style>
What is causing scroll bar to not be clipped on side of table in this fiddle: http://jsfiddle.net/m4HB3/8/ At the moment it is underneath the last column, meaning it is taking up some of that columns space and thus causing alignment issues with the table.
The table headers are fixed as I want a scrolling table with fixed headers.
Could the problem be the width set for the table and individual columns?
Can somebody please also text their answer on a script outside jsfiddle and put it straight on a browser because I have seen examples where something is working in jsifddle but then not working in main application on browser.
HTML:
<table id="tableqanda" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th width="5%" class="questionno">Question No.</th>
<th width="27%" class="question">Question</th>
<th width="7%" class="option">Option Type</th>
<th width="11%" class="image">Image</th>
</tr>
</thead>
</table>
<div id="tableqanda_onthefly_container">
<table id="tableqanda_onthefly" cellpadding="0" cellspacing="0">
<tbody>
<tr class="tableqandarow">
<td width="5%" class="questionno">1</td>
<td width="27%" class="question">What is a RAM?</td>
<td width="7%" class="option">A-E</td>
<td width="11%" class="imagetd"><ul class="qandaul"><li>Lighthouse_4.jpg</li></ul></td>
</tr>
<tr class="tableqandarow">
<td width="5%" class="questionno">2</td>
<td width="27%" class="question">Name 3 car maneuvers you may do in a test?</td>
<td width="7%" class="option">A-F</td>
<td width="11%" class="imagetd"><ul class="qandaul"><li>Tulips_3.jpg</li></ul></td>
</tr>
</tbody>
</table>
</div>
CSS:
#tableqanda_onthefly_container
{
width:100%;
overflow-y: auto;
overflow-x: hidden;
max-height:25px;
}
#tableqanda_onthefly
{
width:100%;
overflow-y: auto;
overflow-x: hidden;
clear:both;
}
#tableqanda_onthefly td
{
border:1px black solid;
border-collapse:collapse;
}
#tableqanda, #tableqanda_onthefly{
border:1px black solid;
border-collapse:collapse;
table-layout:fixed;
word-wrap:break-word;
}
#tableqanda{
width:100%;
margin-left:0;
float:left;
}
#tableqanda td {
vertical-align: middle;
border:1px black solid;
border-collapse:collapse;
}
#tableqanda th{
border:1px black solid;
border-collapse:collapse;
text-align:center;
}
.tableqandarow{
border:1px black solid;
border-collapse:collapse;
}
UPDATE:
http://jsfiddle.net/m4HB3/37/
I have a js fiddle which works perfectly. But if you copy code into a standard html, the columns are not aligned. If you can get the columns aligned correctly with their headings with the scroll bar on the side of the table, then that will be perfect answer
Is that something what could work for you?
Using here script/jQuery within your div to scroll, slightly modified css to give side scrolling.
Please see: http://jsfiddle.net/m4HB3/177
Here's standalone version: http://webapper.pl/demo.html
I didn't spend much time on css but i'm sure you could make it look pretty.
var ele = $('.scroll');
var scroll = 25;
$('.up').on('click',function() {
ele.scrollTop( ele.scrollTop() - scroll );
});
$('.down').on('click',function() {
ele.scrollTop( ele.scrollTop() + scroll );
});
Or you can use static table width (that would also fix your problem):
http://jsfiddle.net/m4HB3/178/
Here is a fiddle based on the linked question by Sara:
DEMO
html
<div id="tableContainer" class="tableContainer">
<table width="100%" cellspacing="0" cellpadding="0" border="0" class="scrollTable">
<col width="10%" />
<col width="54%" />
<col width="14%" />
<col width="22%" />
<thead class="fixedHeader">
<tr>
<th class="questionno">Question No.</th>
<th class="question">Question</th>
<th class="option">Option Type</th>
<th class="image">Image</th>
</tr>
</thead>
<tbody class="scrollContent">
<tr>
<td class="questionno">1</td>
<td class="question">What is a RAM?</td>
<td class="option">A-E</td>
<td class="imagetd"><ul class="qandaul"><li>Lighthouse_4.jpg</li></ul></td>
</tr>
<tr>
<td class="questionno">2</td>
<td class="question">Name 3 car maneuvers you may do in a test?</td>
<td class="option">A-F</td>
<td class="imagetd"><ul class="qandaul"><li>Tulips_3.jpg</li></ul></td>
</tr>
<tr>
<td class="questionno">3</td>
<td class="question">What is a RAM?</td>
<td class="option">A-E</td>
<td class="imagetd"><ul class="qandaul"><li>Lighthouse_4.jpg</li></ul></td>
</tr>
<tr>
<td class="questionno">4</td>
<td class="question">Name 3 car maneuvers you may do in a test?</td>
<td class="option">A-F</td>
<td class="imagetd"><ul class="qandaul"><li>Tulips_3.jpg</li></ul></td>
</tr>
<tr>
<td class="questionno">5</td>
<td class="question">What is a RAM?</td>
<td class="option">A-E</td>
<td class="imagetd"><ul class="qandaul"><li>Lighthouse_4.jpg</li></ul></td>
</tr>
<tr>
<td class="questionno">6</td>
<td class="question">Name 3 car maneuvers you may do in a test?</td>
<td class="option">A-F</td>
<td class="imagetd"><ul class="qandaul"><li>Tulips_3.jpg</li></ul></td>
</tr>
<tr>
<td class="questionno">7</td>
<td class="question">What is a RAM?</td>
<td class="option">A-E</td>
<td class="imagetd"><ul class="qandaul"><li>Lighthouse_4.jpg</li></ul></td>
</tr>
<tr>
<td class="questionno">8</td>
<td class="question">Name 3 car maneuvers you may do in a test?</td>
<td class="option">A-F</td>
<td class="imagetd"><ul class="qandaul"><li>Tulips_3.jpg</li></ul></td>
</tr>
</tbody>
</table>
</div>
css
div.tableContainer {
clear: both;
border: 1px solid #963;
overflow: auto;
}
html>body tbody.scrollContent {
display: block;
height: 100px;
overflow: auto;
width: 100%
}
html>body thead.fixedHeader tr {
display: block
}
html>body td {
box-sizing: border-box;
border: 1px solid grey;
}
table .questionno {
width: 10%;
}
table .question {
width: 54%;
}
table .option {
width: 14%;
}
table .image {
width: 22%;
}