I would like to know how to vertically align two tables with horizontal scroll at bottom.
The following is the HTML code:
<div class="cntnr">
<div class="tabtwocntnr">
<table class="tabtwo">
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
<th>Header 5</th>
<th>Header 6</th>
<th>Header 7</th>
<th>Header 8</th>
<th>Header 9</th>
<th>Header 10</th>
<th>Header 11</th>
<th>Header 12</th>
<th>Header 13</th>
<th>Header 14</th>
<th>Header 15</th>
<th>Header 16</th>
<th>Header 17</th>
<th>Header 18</th>
<th>Header 19</th>
<th>Header 20</th>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
<th>Header 5</th>
<th>Header 6</th>
<th>Header 7</th>
<th>Header 8</th>
<th>Header 9</th>
<th>Header 10</th>
<th>Header 11</th>
<th>Header 12</th>
<th>Header 13</th>
<th>Header 14</th>
<th>Header 15</th>
<th>Header 16</th>
<th>Header 17</th>
<th>Header 18</th>
<th>Header 19</th>
<th>Header 20</th>
</tr>
</thead>
</table>
</div>
<div class="tabthreecntnr">
<table class="tabthree">
<tbody>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
<td>Content 6</td>
<td>Content 7</td>
<td>Content 8</td>
<td>Content 9</td>
<td>Content 10</td>
<td>Content 11</td>
<td>Content 12</td>
<td>Content 13</td>
<td>Content 14</td>
<td>Content 15</td>
<td>Content 16</td>
<td>Content 17</td>
<td>Content 18</td>
<td>Content 19</td>
<td>Content 20</td>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
<td>Content 6</td>
<td>Content 7</td>
<td>Content 8</td>
<td>Content 9</td>
<td>Content 10</td>
<td>Content 11</td>
<td>Content 12</td>
<td>Content 13</td>
<td>Content 14</td>
<td>Content 15</td>
<td>Content 16</td>
<td>Content 17</td>
<td>Content 18</td>
<td>Content 19</td>
<td>Content 20</td>
</tr>
</tbody>
</table>
</div>
</div>
The corresponding css is:
.tabtwo,.tabthree{
border-collapse: collapse;
table-layout:fixed;
}
.tabtwo th, .tabthree td{
border-right: 1px solid black;
}
.tabthree td{
text-align: center;
border-top: 1px solid #000;
}
.tabtwocntnr,.tabthreecntnr{
width:100%;
}
.cntnr{
overflow-x:auto;
width: 100%;
border: 1px solid black;
}
The fiddle is :
http://jsfiddle.net/8H5Ek/
As we can see in this fiddle the two tables are not aligned properly(with respect to each other).There is also some white space at the right corner of the bottom table. I have tried resolving it by applying table-layout: fixed; to both the tables.
I am unable to achieve the desired result.
Please help me by contributing.
Without giving explicit widths, controlling tables can be very tricky.
See this fiddle: http://jsfiddle.net/8H5Ek/3/
Add these styles to your css:
table { width: 100%; }
th, td { width: 84px; }
You have to estimate the width of the td carefully.
Edit:
Here is another fiddle: http://jsfiddle.net/8H5Ek/12/
You can control the content length by using ellipsis. Moreover, you do not need those two inner divs (tabtwocntnr and tabthreecntnr). You can rid of them.
th, td {
width: 84px;
overflow:hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
try changing width:100% to width:auto on the table css and then see how the content re-sizes the first header cell
for .tabtwo,.tabthree you have to set width:100%; and set the width for parent...add
html, body {
width:100%;/* added */
height:100%;/* added */
margin:0;/* added */
padding:0;/* added */
}
then you have to use colspan to fix the width of th
demo
EDIT
okay...solution was there in front of eyes...bit of your bad markup played foul....
thead and tbody is used for marking things in single table...you have used 2 different tables for it, that's why alignment issue is there..
delete this and you are done:
</table>
</div>
<div class="tabthreecntnr">
<table class="tabthree">
final demo
Related
I have a dynamic table which data's are coming from an API. I'm generating the table and inserting rows using JavaScript.
Everything is working properly but the problem is in styling.
My table row colors are stripped (just like we use bootstrap table-striped class) and cells are not same in every row.
Some has 3, some has 6, some 4 etc. Where cells are less they are showing some blank space.
Is there any way i can color the entire row. Here is an example. Here is an example of my code:
.table-striped th {
height: 45px;
background-color: #bfff00 !important;
color: #191919;
}
.table-striped td {
padding: 8px;
border: 2px solid #F6F6F6;
font-weight: bold;
}
.table-striped>tr:nth-child(n+1)>td {
background-color: #bababa;
}
.table-striped>tr:nth-child(n+2)>td {
background-color: #e8e7e6;
}
<div>
<table class="table-striped">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
<th>Header 5</th>
<th>Header 6</th>
<th>Header 7</th>
<th>Header 8</th>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<td>Cell 4</td>
<td>Cell 5</td>
<td>Cell 6</td>
<td>Cell 7</td>
<td>Cell 8</td>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<td>Cell 4</td>
<td>Cell 5</td>
</tr>
</table>
</div>
Here is jsfiddle
The table row is only as wide as the number of cells.
Add a final cell when you don't have a full row and use colspan as appropriate.
.table-striped th {
height: 45px;
background-color: #bfff00 !important;
color: #191919;
}
.table-striped td {
padding: 8px;
border: 2px solid #F6F6F6;
font-weight: bold;
}
.table-striped tr:nth-child(odd) {
background-color: #bababa;
}
.table-striped tr:nth-child(even) {
background-color: #e8e7e6;
}
<div>
<table class="table-striped">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
<th>Header 5</th>
<th>Header 6</th>
<th>Header 7</th>
<th>Header 8</th>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<td colspan="5"></td>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<td>Cell 4</td>
<td>Cell 5</td>
<td>Cell 6</td>
<td>Cell 7</td>
<td>Cell 8</td>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<td>Cell 4</td>
<td>Cell 5</td>
<td colspan="3"></td>
</tr>
</table>
</div>
Note from MDN that :
Any styles applied to the <tr> element will affect the cells within
the row unless overridden by styles applied to those cells.
Pay particular attention to the fact it styles the cells, not the row itself
EDIT
We know from your question and comments that you are using javascript to generate the table. We can't infer much on how you are doing this, but if you are generating the table row by row you should be able to see if you have generated the amount of cell for that row equal to the amount of th cells. If not, add a cell with the difference as a colspan. If you can't do this you can do it after the table is created.
This is a rough way of doing it
function addColspan(table) {
//Assume number of th are our max cells per row
var maxCells = table.querySelectorAll("th").length;
//Get all rows but the first
var rows = table.querySelectorAll("tr:nth-child(n+2)");
for(var i = 0; i < rows.length; i++){
//Get how many cells we have
var cellCount = rows[i].querySelectorAll("td").length;
//If we don't have enough cells
if(cellCount < maxCells) {
//Add one with the appropriate colspan
rows[i].innerHTML += "<td colspan='" + (maxCells - cellCount) + "'></td>";
}
}
}
//Run after table is generated
addColspan(document.querySelector(".table-striped"));
.table-striped th {
height: 45px;
background-color: #bfff00 !important;
color: #191919;
}
.table-striped td {
padding: 8px;
border: 2px solid #F6F6F6;
font-weight: bold;
}
.table-striped tr:nth-child(odd) {
background-color: #bababa;
}
.table-striped tr:nth-child(even) {
background-color: #e8e7e6;
}
<div>
<table class="table-striped">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
<th>Header 5</th>
<th>Header 6</th>
<th>Header 7</th>
<th>Header 8</th>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<td>Cell 4</td>
<td>Cell 5</td>
<td>Cell 6</td>
<td>Cell 7</td>
<td>Cell 8</td>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<td>Cell 4</td>
<td>Cell 5</td>
</tr>
</table>
</div>
You can use even and odd properties in css.
Check this out
.table-striped th {
height: 45px;
background-color: #bfff00 !important;
color: #191919;
}
.table-striped td {
padding: 8px;
border: 2px solid #F6F6F6;
font-weight: bold;
}
.table-striped tr:nth-child(odd) td {
background-color: #bababa;
}
.table-striped tr:nth-child(even) td {
background-color: #e8e7e6;
}
<div>
<table class="table-striped">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
<th>Header 5</th>
<th>Header 6</th>
<th>Header 7</th>
<th>Header 8</th>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<td>Cell 4</td>
<td>Cell 5</td>
<td>Cell 6</td>
<td>Cell 7</td>
<td>Cell 8</td>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<td>Cell 4</td>
<td>Cell 5</td>
</tr>
</table>
</div>
Updated answer after the comment. You need to have alteast empty in order to highlight the complete rows.
.table-striped th {
height: 45px;
background-color: #bfff00 !important;
color: #191919;
}
.table-striped td {
padding: 8px;
border: 2px solid #F6F6F6;
font-weight: bold;
}
.table-striped tr:nth-child(odd) td {
background-color: #bababa;
}
.table-striped tr:nth-child(even) td {
background-color: #e8e7e6;
}
<div>
<table class="table-striped">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
<th>Header 5</th>
<th>Header 6</th>
<th>Header 7</th>
<th>Header 8</th>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<td>Cell 4</td>
<td>Cell 5</td>
<td>Cell 6</td>
<td>Cell 7</td>
<td>Cell 8</td>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<td>Cell 4</td>
<td>Cell 5</td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</div>
Consider the following html
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>data 1.1</td>
<td>data 1.2</td>
<td>data 1.3</td>
</tr>
<tr>
<td>data 2.1</td>
<td>data 2.2</td>
<td>data 2.3</td>
</tr>
</tbody>
It renders as the following image:
Is it possible by only using css to have the previous html render like the following image? (please note the indexes)
If you have to do this with css and you can't change your HTML you can do it with Flexbox.
table {
display: flex;
}
thead tr, tbody tr {
display: inline-flex;
flex-direction: column;
}
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>data 1.1</td>
<td>data 1.2</td>
<td>data 1.3</td>
</tr>
<tr>
<td>data 2.1</td>
<td>data 2.2</td>
<td>data 2.3</td>
</tr>
</tbody>
I am trying to have a table which can have a range of columns, with this css below, I am able to get it scrollable horizontally with large number of columns:
.myTable {
overflow:auto;
display: block;
width: 100%
}
But with 2-3 columns for example, the table shows up in partial page (horizontally) - doesn't span. Changing display to "table" makes the table with large number of columns overflow horizontally (scrolling is gone).
Any ideas how both cases can work?
I believe you want to move this table into a wrapper that itself has overflow: scroll, like so:
.wrapper {
overflow: auto;
}
.myTable {
width: 100%;
text-align: center;
}
<div class="wrapper">
<table class="myTable">
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
<th>Col 5</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
<td>Data 5</td>
</tr>
</table>
</div>
<div class="wrapper">
<table class="myTable">
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
<th>Col 5</th>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
<th>Col 5</th>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
<th>Col 5</th>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
<th>Col 5</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
<td>Data 5</td>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
<td>Data 5</td>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
<td>Data 5</td>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
<td>Data 5</td>
</tr>
</table>
</div>
Is it possible to add a text-box inside a 6 columned table?
It would look something like this
<table id="table" class="table">
<thead id="search-result">
<tr>
<th>table 1</th>
<th>table 2</th>
<th>table 3</th>
<th>table 4</th>
<th>table 5</th>
<th>table 6</th>
</tr>
</thead>
<tbody id="result">
<tr>
<td>content 1</td>
<td>content 2</td>
<td>content 3</td>
<td>content 4</td>
<td>content 5</td>
<td>content 6</td>
</tr>
</tbody>
</table>
under the content tags there would be a 100% textbox. is that possible?
This question has been answered. thanks to Mr. Alvaro Montoro.
I've just added a new row with colspan="6" and the textbox inside it.
Is it possible to put an HTML element in between a table header and table rows?
Something like the code I have in this plunker:
<table style="width:100%">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<div style="background-color: #848484; width: 100%; height:1px; margin:0px; padding: 0px;"></div>
<tr>
<td>Value 1</td>
<td>Value 2</td>
<td>Value 3</td>
</tr>
</table>
My current way of achieving this is putting the divider line div after the table and using position relative to place it in between the table elements. But if the height of the table header or body changes, my position relative line may not appear in between the header and body anymore.
What is the best way to insert this divider line so that it is always between the table header and body?
Thank you very much for your time. Let me know if you need more information or I am being unclear.
You don't need additional markup to put a line in your table. Just use CSS:
th {
border-bottom: 2px solid #848484;
}
table {
border-collapse: collapse; /* remove space between cells */
}
<table style="width:100%">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
<td>Value 1</td>
<td>Value 2</td>
<td>Value 3</td>
</tr>
</table>
I think a better way to do it is by using THEAD and TBODY and then write some css that does the same as your DIV
HTML
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
</thead>
CSS
thead {border-bottom:1px solid #848484;}
Use border-bottom to add the separator, and use the :last-child selector to remove the separator for the last row.
jsFiddle
HTML
<table style="width:100%">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
<td>Value 1</td>
<td>Value 2</td>
<td>Value 3</td>
</tr>
<tr>
<td>Value 1</td>
<td>Value 2</td>
<td>Value 3</td>
</tr>
</table>
CSS
tr {
display: block;
border-bottom: 11px solid grey;
}
tr:last-child {
border: none;
}