I have a table with several columns that can be switched into mobile view by clicking a button. Some fields contain hyperlinks.
<style>
.show-thin {
width: 930px; /* complete width of alternative table view */
}
/* Force table to not be like tables anymore */
.show-thin table, .show-thin thead, .show-thin tbody, .show-thin th, .show-thin td, .show-thin tr {
display: block;
}
/* Hide table headers (but not display: none;, for accessibility) */
.show-thin thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}
.show-thin tr { border: 1px solid #ccc; }
.show-thin td {
/* Behave like a "row" */
border: none;
border-bottom: 1px solid #eef;
position: relative;
padding-left: 30%; /* distance of table-values from left margin 30px */
}
.show-thin td:before {
/* Now like a table header */
position: absolute; /* puts field-names at left margin */
/* Top/left values mimic padding */
top: 6px;
left: 6px;
width: 45%;
padding-right: 10px;
white-space: nowrap;
}
/*
Label the data
*/
.show-thin td:before { content: attr(data-label); }
</style>
<script>
function toggle() {
var table = document.querySelector('.myTable');
table.classList.toggle('show-thin');
}
</script>
<button onclick="toggle()">Toggle</button>
<hr/>
<table class="myTable">
<thead>
<tr class="tr thin-hide">
<th data-label='Nr'>Nr</th>
<th>Estimated arrival date</th>
<th>Amount</th>
<th>Period</th>
<th>Period2</th>
<th>Period3</th>
<th>Period4</th>
<th>Period5</th>
<th>Period6</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="Nr">1234</td>
<td data-label="Estimated Arrival Date">03/15/2001</td>
<td data-label="Amount">$1.00</td>
<td data-label="Period"><a href='https://www.startpage.com' target='_blank'>Startpage</a></td>
<td data-label="Period2"><a href='https://www.wikipedia.com' target='_blank'>Wikipedia</a></td>
<td data-label="Period3"><a href='https://www.google.com' target='_blank'>Google</a></td>
<td data-label="Period4">3rd</td>
<td data-label="Period5">3rd</td>
<td data-label="Period6">3rd</td>
</tr>
<tr class="tr">
<td data-label="Account">1235</td>
<td data-label="Estimated Arrival Date">04/21/2002</td>
<td data-label="Amount">$12.00</td>
<td data-label="Period">4th</td>
<td data-label="Period2">4th</td>
<td data-label="Period3">4th</td>
<td data-label="Period4">4th</td>
<td data-label="Period5">4th</td>
<td data-label="Period6">4th</td>
</tr>
<tr>
<td data-label="Account">4594</td>
<td data-label="Estimated Arrival Date">11/11/2011</td>
<td data-label="Amount">$45.50</td>
<td data-label="Period">2nd</td>
<td data-label="Period2">2nd</td>
<td data-label="Period3">2nd</td>
<td data-label="Period4">2nd</td>
<td data-label="Period5">2nd</td>
<td data-label="Period6">2nd</td>
</tr>
<tr>
<td data-label="Account">4594</td>
<td data-label="Estimated Arrival Date">11/11/2011</td>
<td data-label="Amount">$45.50</td>
<td data-label="Period">2nd</td>
<td data-label="Period2">2nd</td>
<td data-label="Period3">2nd</td>
<td data-label="Period4">2nd</td>
<td data-label="Period5">2nd</td>
<td data-label="Period6">2nd</td>
</tr>
</tbody>
</table>
In desktop mode the hover effect of the hyperlinks works as it should.
After switching into mobile view the hover effect of the hyperlinks works only in the very upper margin of the respective words.
I think the problem is in the CSS, however, I didn´t get it to work properly.
In mobile mode the the first column overlaps the second column. Add background-color: green to .show-thin td:before {...} and you will see it happen. Ergo, the first column is too wide.
Change width in .show-thin td:before {...} to 28% or less and your problem is solved....
I have a table like this:
<table>
<thead>
<tr>
<th>Name</th>
<th>Phone no.</th>
<th>Address</th>
<th>Wealth</th>
</tr>
</thead>
<tbody>
<tr>
<th>John Doe</th>
<td>00123456789</td>
<td>Morgue St. 21</td>
<td>$100,000</td>
</tr><tr>
<th>Mary Sue</th>
<td>00987654321</td>
<td>Impossible St. 12</td>
<td>$999,999,999,999,999</td>
</tr><tr>
<th>Cpt. Kirk</th>
<td>00999999999</td>
<td>Enterprise St. 22</td>
<td>$100,000,000</td>
</tr>
</tbody>
</table>
Well, this table is pretty wide. And now I have to make a stylesheet that would make the site appropriate for viewing on a narrow screen, like a mobile phone. So I have to do sth with this wide table.
I was thinking if this table could display vertically, not horizontally. More specifically, I was thinking if it could display more like this:
<ul>
<li><strong>John Doe:</strong>
<ul>
<li><em>Phone no.:</em> 00123456789</li>
<li><em>Address:</em> Morgue St. 21</li>
<li><em>Wealth:</em> $100,000</li>
</ul>
</li><li><strong>Mary Sue:</strong>
<ul>
<li><em>Phone no.:</em> 00987654321</li>
<li><em>Address:</em> Impossible St. 12</li>
<li><em>Wealth:</em> $999,999,999,999,999</li>
</ul>
</li><li><strong>Cpt. Kirk:</strong>
<ul>
<li><em>Phone no.:</em> 00999999999</li>
<li><em>Address:</em> Enterprise St. 22</li>
<li><em>Wealth:</em> $100,000,000 </li>
</ul>
</li>
</ul>
Now, I surely could make a Javascript that would transform the table code from the first snippet to the list code from the second snippet. But I wonder, if this is necessary? Is it possible to make a CSS stylesheet that, when attached to the table code from the first snippet would make it look like the second snippet?
Sure, you can play with media queries and change the display of the table :
See this fiddle
#media(max-width: 640px){
table, table td, table tr, table th { display: block; text-align: left; }
table th, table td { margin: 0; padding-left: 25px; }
table td { margin-left: 40px;list-style: square; display: list-item; padding-left: 0; }
table thead { display: none; }
}
td, th {
text-align: left;
display: block;
float: left;
width: 100%;
}
thead {
display: none;
}
<table>
<thead>
<tr>
<th>Name</th>
<th>Phone no.</th>
<th>Address</th>
<th>Wealth</th>
</tr>
</thead>
<tbody>
<tr>
<th>John Doe</th>
<td>00123456789</td>
<td>Morgue St. 21</td>
<td>$100,000</td>
</tr><tr>
<th>Mary Sue</th>
<td>00987654321</td>
<td>Impossible St. 12</td>
<td>$999,999,999,999,999</td>
</tr><tr>
<th>Cpt. Kirk</th>
<td>00999999999</td>
<td>Enterprise St. 22</td>
<td>$100,000,000</td>
</tr>
</tbody>
</table>
Here's a possible way without the <thead> elements, but you could create hidden elements before each person, e.g. <span class="hidden">Name:</span> Cpt. Kirk and then enable all the hidden elements with media queries. Not the most elegant solution, I'd probably prefer JS for this.
There might be better ways to do this, but here is a solution to reach the goal:
table thead{
display:none;
}
table tbody tr th{
display:block;
text-align: left;
}
table tbody tr td{
display:block;
margin-left:20px;
}
table tbody tr th::before{
content:"• ";
}
table tbody tr td::before{
content:"◊ ";
}
Find a working example: https://jsfiddle.net/ktnurvfr/
You can change the table elements to display block and force them to act like block elements just add this class to your table.
.vertical-table thead{
display:none;
}
.vertical-table tr, .vertical-table th, .vertical-table td{
display:block;
width:100%;
}
.vertical-table thead{
display:none;
}
.vertical-table tr, .vertical-table th, .vertical-table td{
display:block;
width:100%;
}
<table class="vertical-table">
<thead>
<tr>
<th>Name</th>
<th>Phone no.</th>
<th>Address</th>
<th>Wealth</th>
</tr>
</thead>
<tbody>
<tr>
<th>John Doe</th>
<td>00123456789</td>
<td>Morgue St. 21</td>
<td>$100,000</td>
</tr><tr>
<th>Mary Sue</th>
<td>00987654321</td>
<td>Impossible St. 12</td>
<td>$999,999,999,999,999</td>
</tr><tr>
<th>Cpt. Kirk</th>
<td>00999999999</td>
<td>Enterprise St. 22</td>
<td>$100,000,000</td>
</tr>
</tbody>
</table>
you can use this code :
#media(max-width: 640px){
table, table td, table tr, table th { display: block; text-align: left; }
table th, table td { margin: 0; padding-left: 25px; }
table td { margin-left: 40px;list-style: square; display: list-item; padding-left: 0; }
table thead { display: none; }
}
i will be work.
I had the same problem. During my search I encountered this elegant solution by sergiopinnaprato: http://bootsnipp.com/snippets/featured/no-more-tables-respsonsive-table
On smaller screens it changes the table to a single column. Very readable solution using only html and css code:
HTML:
<div id="no-more-tables">
<table class="col-md-12 table-bordered table-striped table-condensed cf">
<thead class="cf">
<tr>
<th>Code</th>
<th>Company</th>
<th class="numeric">Price</th>
<th class="numeric">Change</th>
<th class="numeric">Change %</th>
<th class="numeric">Open</th>
<th class="numeric">High</th>
<th class="numeric">Low</th>
<th class="numeric">Volume</th>
</tr>
</thead>
<tbody>
<tr>
<td data-title="Code">AAC</td>
<td data-title="Company">AUSTRALIAN AGRICULTURAL COMPANY LIMITED.</td>
<td data-title="Price" class="numeric">$1.38</td>
<td data-title="Change" class="numeric">-0.01</td>
<td data-title="Change %" class="numeric">-0.36%</td>
<td data-title="Open" class="numeric">$1.39</td>
<td data-title="High" class="numeric">$1.39</td>
<td data-title="Low" class="numeric">$1.38</td>
<td data-title="Volume" class="numeric">9,395</td>
</tr>
<tr>
<td data-title="Code">AAD</td>
<td data-title="Company">ARDENT LEISURE GROUP</td>
<td data-title="Price" class="numeric">$1.15</td>
<td data-title="Change" class="numeric">+0.02</td>
<td data-title="Change %" class="numeric">1.32%</td>
<td data-title="Open" class="numeric">$1.14</td>
<td data-title="High" class="numeric">$1.15</td>
<td data-title="Low" class="numeric">$1.13</td>
<td data-title="Volume" class="numeric">56,431</td>
</tr>
</tbody>
</table>
</div>
CSS:
#media only screen and (max-width: 800px) {
/* Force table to not be like tables anymore */
#no-more-tables table,
#no-more-tables thead,
#no-more-tables tbody,
#no-more-tables th,
#no-more-tables td,
#no-more-tables tr {
display: block;
}
/* Hide table headers (but not display: none;, for accessibility) */
#no-more-tables thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}
#no-more-tables tr { border: 1px solid #ccc; }
#no-more-tables td {
/* Behave like a "row" */
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 50%;
white-space: normal;
text-align:left;
}
#no-more-tables td:before {
/* Now like a table header */
position: absolute;
/* Top/left values mimic padding */
top: 6px;
left: 6px;
width: 45%;
padding-right: 10px;
white-space: nowrap;
text-align:left;
font-weight: bold;
}
/*
Label the data
*/
#no-more-tables td:before { content: attr(data-title); }
}
Hope this helps!
I have this css style for responsive table. it works as expected in firefox but not in chrome.
In chrome all the text mixed, and it seems that their is a layers.
what is the problem in the code that it works on firefox but not in chrome?
#media only screen and (max-width: 800px) {
/* Force table to not be like tables anymore */
table,
thead,
tbody,
th,
td,
tr {
display: block;
}
/* Hide table headers (but not display: none;, for accessibility) */
thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}
tr {
border: 1px solid #ccc;
}
td {
/* Behave like a "row" */
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-right: 50%;
white-space: normal;
text-align: right;
}
td:before {
/* Now like a table header */
position: absolute;
/* Top/left values mimic padding */
top: 6px;
right: 6px;
width: 45%;
padding-right: 10px;
white-space: nowrap;
text-align: right;
font-weight: bold;
}
/*
Label the data
*/
td:before {
content: attr(data-title);
};
}
<table>
<thead>
<tr>
<th>title 1</th>
<th>title 2</th>
<th>title 3</th>
<th>title 4</th>
<th>title 5</th>
</tr>
</thead>
<tbody>
<tr>
<td data-title="title1">
text
</td>
<td data-title="title2">
text
</td>
<td data-title="title3">
text
</td>
<td data-title="title4">
text
</td>
<td data-title="title5">
text
</td>
</tr>
</tbody>
</table>
In the preview it seems ok in chrome byt if I create new simple html file with the table in the body it look very bad.
I understand that I need add something to the css or to the body but I dont know what.
This is how it looks like when I make the screen width small then 800px
From the comments it was found you were missing a doctype. A doctype is required of all modern web pages. Without one, you are in "quirks mode" and things like margin and padding and all kinds of things do not follow the W3C box model.
Adding this to the very first line keeps all browsers in "standards mode"
<!DOCTYPE html>
Activating Browser Modes with Doctype
I am trying to get with following data with table.. on mobile view it is not working perfectly: I am trying to achieve to achieve it through bootstrap.
It includes colspan and rowspan too.
HTML as follow:
<div id="no-more-tables">
<table border="1" id="pricing" width="100%" class="table col-sm-12 table-bordered table-striped table-condensed cf">
<tr>
<th colspan="6" scope="col" style="background-color:#CD3E27; color:#FFFFFF;">Baner Packages - Per Month</th>
</tr>
<tr>
<td>The Mesh Premium</td>
<td>The Mesh Eco</td>
<td>The Mesh Ladies<br></td>
<td>The Mesh Impact</td>
<td>The Mesh Moonlighters</td>
<td>The Mesh 9-9</td>
</tr>
<tr>
<td rowspan="2">₹ 7,500/-</td>
<td rowspan="2">₹ 6,000/-</td>
<td rowspan="2">₹ 5,500/-</td>
<td rowspan="2">₹ 5,500/-</td>
<td rowspan="2">₹ 3,500/-</td>
<td>₹ 500/- Non-AC</td>
</tr>
<tr>
<td>₹ 750/- AC</td>
</tr>
<tr>
<td>Includes 24*7 AC</td>
<td>Non-AC</td>
<td>Non-AC</td>
<td>Non-AC</td>
<td>Non-AC</td>
<td>One Day Pass</td>
</tr>
</table>
<div>
CSS as follows:
#media only screen and (max-width: 800px) {
/* Force table to not be like tables anymore */
#no-more-tables table,
#no-more-tables thead,
#no-more-tables tbody,
#no-more-tables th,
#no-more-tables td,
#no-more-tables tr {
display: block;
}
/* Hide table headers (but not display: none;, for accessibility) */
#no-more-tables thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}
#no-more-tables tr { border: 1px solid #ccc; }
#no-more-tables td {
/* Behave like a "row" */
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 50%;
white-space: normal;
text-align:left;
}
#no-more-tables td:before {
/* Now like a table header */
position: absolute;
/* Top/left values mimic padding */
top: 6px;
left: 6px;
width: 45%;
padding-right: 10px;
white-space: nowrap;
text-align:left;
font-weight: bold;
}
/*
Label the data
*/
#no-more-tables td:before { content: attr(data-title); }
}
JS fiddle link as above: https://jsfiddle.net/anujoshi10/n0gL4y1g/
http://jsfiddle.net/anujoshi10/5t2syp13/
Does anybody have a solution for this?
For Bootstrap Responsive Table i use this Trick: Work Perfect for me.
Visit Link and try to follow Examples http://elvery.net/demo/responsive-tables/
Hope This Helps!
Thanks
My aim is to create responsive tables design that look good when viewing on mobiles (width under 480px).
I have the following markup for my table:
<table class="table eventlist">
<thead>
<tr>
<th>Date</th>
<th>Time</th>
<th>Duration</th>
<th>Location</th>
<th>Cost</th>
</tr>
</thead>
<tr>
<td data-title="Code">AAC</td>
<td data-title="Company">AUSTRALIAN AGRICULTURAL COMPANY LIMITED.</td>
<td data-title="Price" class="numeric">$1.38</td>
<td data-title="Change" class="numeric">-0.01</td>
<td data-title="Change %" class="numeric">-0.36%</td>
<td data-title="Open" class="numeric">$1.39</td>
</tr>
</table>
And the following CSS:
/* Landscape phones and down */
#media (max-width: 480px) {
/* Force table to not be like tables anymore */
table, thead, tbody, th, td, tr {
display: block;
}
/* Hide table headers (but not display: none;, for accessibility) */
thead tr {
position: absolute;
top: -9999px;
left: -9999px;
display:none;
}
tr {
border: 1px solid #ccc;
}
td {
/* Behave like a "row" */
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 50%;
white-space: normal;
text-align:right;
}
td:before {
padding-right: 10px;
white-space: nowrap;
text-align:left;
font-weight: bold;
}
/*
Label the data
*/
td:before {
content: attr(data-title);
}
}
My table ends up looking like this:
Would you say this is a good user friendly responsive design?
How can I edit the line:
td:before {
content: attr(data-title);
}
So that it reads the <th> for the column?
Here is a live example: JS Fiddle
Re: Would you say this is a good user friendly responsive design?
The layout of it looks great for a single symbol, and still looks quite good for multiple rows when not in "mobile-mode", however if you plan on having multiple quotes, I think it's going to wind up looking quite cluttered on small screens. You might consider removing the borders between rows and only having lines between quotes to keep it visually cleaner.
Just my .02 from a design perspective.