HTML wide tables should scroll, narrow tables should stretch to fill parent - html

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%; .

Related

tr tag from table doesn't start on new line

Can somebody please explain to me why the second row is not on a new line?
$(document).ready(function() {
$('.rotate').css('height', $('.rotate').width());
});
td {
border-collapse: collapse;
border: 1px black solid;
}
th {
border: 1px black solid;
border-collapse: collapse;
}
tr {
text-align: center;
}
.rotate {
-webkit-transform: rotate(-90.0deg);
transform: rotate(-90.0deg);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table cellpadding="10" cellspacing="0" align="center">
<caption>SCARA GEOCRONOLOGICĂ</caption>
<tr>
<th rowspan="2">EON</th>
<th colspan="2" rowspan="2">ERA</th>
<th colspan="3" rowspan="2">PERIOADA</th>
<th colspan="2" rowspan="2">Cicluri orogenice</th>
<th rowspan="2">Mil. ani în urmă</th>
<th colspan="4" rowspan="2">Viețuitoare tipice</th>
</tr>
<tr>
<td rowspan="13">FANEROZOIC</td>
<td colspan="2" rowspan="4">CAINOZOIC(NEOZOIC) Cz</td>
<td colspan="3">Cuaternar Q</td>
<td colspan="2" rowspan="3">Ciclul alpin</td>
<td rowspan="15">0,01</td>
<td colspan="4">Omul primitiv, mamut, ren, bou moscat, rinocer</td>
</tr>
</table>
(fiddle)
rowspan="2" in the th elements (even in all of them) doesn't really make sense. If you remove it, you get a regular table:
table {
border-collapse: collapse;
}
td, th {
border: 1px dotted #aaa;
}
<table cellpadding="10" cellspacing="0" align="center">
<caption>SCARA GEOCRONOLOGICĂ</caption>
<tr>
<th>EON</th>
<th colspan="2">ERA</th>
<th colspan="3">PERIOADA</th>
<th colspan="2">Cicluri orogenice</th>
<th>Mil. ani în urmă</th>
<th colspan="4">Viețuitoare tipice</th>
</tr>
<tr>
<td rowspan="13">FANEROZOIC</td>
<td colspan="2" rowspan="4">CAINOZOIC(NEOZOIC) Cz</td>
<td colspan="3">Cuaternar Q</td>
<td colspan="2" rowspan="3">Ciclul alpin</td>
<td rowspan="15">0,01</td>
<td colspan="4">Omul primitiv, mamut, ren, bou moscat, rinocer</td>
</tr>
</table>
Use thead and tbody tag
<table cellpadding="10" cellspacing="0" align="center">
<caption>SCARA GEOCRONOLOGICĂ</caption>
<thead>
<tr>
<th rowspan="2">EON</th>
<th colspan="2" rowspan="2">ERA</th>
<th colspan="3" rowspan="2">PERIOADA</th>
<th colspan="2" rowspan="2">Cicluri orogenice</th>
<th rowspan="2">Mil. ani în urmă</th>
<th colspan="4" rowspan="2">Viețuitoare tipice</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="13">FANEROZOIC</td>
<td colspan="2" rowspan="4">CAINOZOIC(NEOZOIC) Cz</td>
<td colspan="3">Cuaternar Q</td>
<td colspan="2" rowspan="3">Ciclul alpin</td>
<td rowspan="15">0,01</td>
<td colspan="4">Omul primitiv, mamut, ren, bou moscat, rinocer</td>
</tr>
</tbody>
</table>
Just include row of head part (<th>) to the <thead> tag and the other part of the row inlude in <tbody>. Hope it will help work as you expect.
td {
border-collapse: collapse;
border: 1px black solid;
}
th {
border: 1px black solid;
border-collapse: collapse;
}
tr {
text-align: center;
}
<table cellpadding="10" cellspacing="0" align="center">
<caption>SCARA GEOCRONOLOGICĂ</caption>
<thead>
<tr>
<th rowspan="2">EON</th>
<th colspan="2" rowspan="2">ERA</th>
<th colspan="3" rowspan="2">PERIOADA</th>
<th colspan="2" rowspan="2">Cicluri orogenice</th>
<th rowspan="2">Mil. ani în urmă</th>
<th colspan="4" rowspan="2">Viețuitoare tipice</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="13">FANEROZOIC</td>
<td colspan="2" rowspan="4">CAINOZOIC(NEOZOIC) Cz</td>
<td colspan="3">Cuaternar Q</td>
<td colspan="2" rowspan="3">Ciclul alpin</td>
<td rowspan="15">0,01</td>
<td colspan="4">Omul primitiv, mamut, ren, bou moscat, rinocer</td>
</tr>
</tbody>
</table>

table with horizontal scroll wont extend to end of headers

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>

How do I arrange content in thead

I wan to arrange columns in my thead to look like the below image
This my code
<thead>
<tr>
<td style="text-align: center;">SUBJECT</td>
<td style="text-align: center;" colspan="3">CA<br> T1 T2 T3</td>
<td style="text-align: center;">CA TOTAL (40)</td>
<td style="text-align: center;">SIGN</td>
</tr>
</thead>
But it displays like this, how can I split the 'CA' column
You need take 2 rows for thead for display.
Here is the complete code
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<table style="width:100%">
<thead>
<tr>
<td style="text-align: center;" rowspan='2'>SUBJECT</td>
<td style="text-align: center;" colspan="3">CA</td>
<td style="text-align: center;" rowspan='2'>CA TOTAL (40)</td>
<td style="text-align: center;" rowspan='2'>SIGN</td>
</tr>
<tr>
<td style="text-align: center;">T1</td>
<td style="text-align: center;">T2</td>
<td style="text-align: center;">T3</td>
</tr>
</thead>
</table>
</body>
</html>
You can't split a cell into more cells, you can only merge existing cells together. That said, if you want to have five columns you need to create five columns, and rather than using a <br> to force content to a new line in one cell, and then fake separate cells, use two rows of cells. This, of course, requires the appropriate use of the colspan and rowspan attributes, such as the following:
table {
border-collapse: collapse;
}
th,
td {
text-align: center;
border: 1px solid #000;
min-width: 4em;
min-height: 2em;
padding: 0.2em 0.5em;
}
th {
vertical-align: top;
}
<table>
<thead>
<tr>
<!-- rowspan="2" forces the following <th> element
to span two rows in its containing element: -->
<th rowspan="2">SUBJECT</th>
<th colspan="3">CA</th>
<!-- as it does here also: -->
<th rowspan="2">CA TOTAL (40)</th>
</tr>
<tr>
<!-- this <tr> contains only three <th> elements,
unlike the previous <tr> which contains five,
because two <th> elements of the previous row
will occupy space in this <tr> also -->
<th>T1</th>
<th>T2</th>
<th>T3</th>
</tr>
</thead>
<tbody>
<tr>
<td>English Language</td>
<td>3</td>
<td>6</td>
<td>10</td>
<td></td>
</tr>
<tr>
<td>Mathematics</td>
<td>3</td>
<td>5</td>
<td>7</td>
<td></td>
</tr>
</tbody>
</table>
JS Fiddle demo.
Incidentally, where possible try to style your content via the use of stylesheets; this makes it far easier to update your style and presentation in future.
<thead>
<tr>
<td style="text-align: center;" rowspan='2'>SUBJECT</td>
<td style="text-align: center;" colspan="3">CA</td>
<td style="text-align: center;" rowspan='2'>CA TOTAL (40)</td>
<td style="text-align: center;" rowspan='2'>SIGN</td>
</tr>
<tr>
<td style="text-align: center;">T1</td>
<td style="text-align: center;">T2</td>
<td style="text-align: center;">T3</td>
</tr>
</thead>
Use rowspan and 2 trs
th { vertical-align: top; border: 1px solid black;}
<table>
<thead>
<tr>
<th rowspan="2" style="text-align: center;">SUBJECT</th>
<th style="text-align: center;" colspan="3">CA</th>
<th rowspan="2" style="text-align: center;">CA TOTAL (40)</th>
</tr>
<tr>
<th>T1</th>
<th>T2</th>
<th>T3</th>
</tr>
</thead>
</table>
You can use this. Working perfectly:
<style>
table, th, td{
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
border: 1px solid #000;
}
</style>
<table>
<thead>
<tr>
<th rowspan="2" >SUBJECT</th>
<th colspan="3">CA</th>
<th rowspan="2">CA TOTAL (40)</th>
<th rowspan="2">SIGN</th>
</tr>
<tr>
<th>T1</th>
<th>T2</th>
<th>T3</th>
</tr>
</thead>
<tbody>
<tr>
<td>English Language</td>
<td>3</td>
<td>6</td>
<td>10</td>
<td></td>
<td></td>
</tr>
<tr>
<td>Mathematics</td>
<td>3</td>
<td>5</td>
<td>7</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>

Bootstrap Table - Move cell to the right to properly display the total

Good evening, I'm trying to align a table to make it like the picture, but failed to do so, I could help?
Now I have it like this:
and I'd like it to look like this:
This is my html code:
<table class="table table-hover table-bordered">
<thead>
<tr>
<th class="text-left">Codigo</th>
<th class="text-left">Descripcion</th>
<th class="text-left">Precio</th>
<th class="text-left">Cantidad</th>
<th class="text-left">Importe</th>
</tr>
</thead>
<tbody>
<tr>
<td class="col-md-1">1001</td>
<td class="col-md-8">Producto de ejemplo 1</td>
<td class="col-md-1 text-right">$10,000</td>
<td class="col-md-1 text-center">100</td>
<td class="col-md-1 text-right">$1,000,000</td>
</tr>
<tr>
<th class="text-right" scope="row">TOTAL</th>
<td class="text-right">$1,000,000</td>
</tr>
</tbody>
</table>
Specifying the colspan for the cells in the summary row will shift the final cell right.
<th colspan="4" class="text-right" scope="row">TOTAL</th>
Alternatively, you could start the row with a <td> with colspan="3", and using a class that would have no borders (controlled by your css).
<td colspan="3" class="noborders"></td>
<th class="text-right" scope="row">TOTAL</th>
Without that class, direct manipulation of style may work.
<td style="border:0" colspan="3"></td>
<th class="text-right" scope="row">TOTAL</th>
Updated snippet:
table {
border-collapse: collapse;
font-family: arial;
}
td,
th {
width: auto;
margin: 2px;
padding: 3px;
text-align: left;
border: 1px solid grey;
}
.noborders {
border: 0;
}
.text-left {
text-align: left;
}
.text-center {
text-align: center;
}
.text-right {
text-align: right;
}
<table class="table table-hover table-bordered">
<thead>
<tr>
<th class="text-left">Codigo</th>
<th class="text-left">Descripcion</th>
<th class="text-left">Precio</th>
<th class="text-left">Cantidad</th>
<th class="text-left">Importe</th>
</tr>
</thead>
<tbody>
<tr>
<td class="col-md-1">1001</td>
<td class="col-md-8">Producto de ejemplo 1</td>
<td class="col-md-1 text-right">$10,000</td>
<td class="col-md-1 text-center">100</td>
<td class="col-md-1 text-right">$1,000,000</td>
</tr>
<tr>
<td colspan="3" class="noborders"></td>
<th class="text-right" scope="row">TOTAL</th>
<td class="text-right">$1,000,000</td>
</tr>
</tbody>
</table>

scroll bar is currently clipped underneath final table column

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