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.
Related
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 would like to create a responsive table like this
Now, I have some large content of data(which is filled dynamically) for each cell, and if you notice this table overlaps the contents on least screen size(300 px and less). Though this can be managed, but I would like to have any optimal solution for this, if possible.
Any help would be appreciated.
It may be your answer please see result in full screen
table tr td,table tr td{
text-align: center;
}
#media only screen and (max-width: 500px) {
table,head,tbody,th,td,tr {
display: block;
}
thead tr {
display: none;
}
tr {
border: 1px solid #ccc;
}
td {
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 50%;
white-space: normal;
text-align:left;
min-height: 30px;
overflow: hidden;
word-break:break-all;
}
td:before {
position: absolute;
top: 6px;
left: 6px;
width: 45%;
padding-right: 10px;
text-align:left;
font-weight: bold;
}
td:before { content: attr(data-title); }
}
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table width="100%">
<thead>
<tr>
<th>Name</th>
<th>ID</th>
<th>designation</th>
</tr>
</thead>
<tbody>
<tr>
<td data-title="Name">John</td>
<td data-title="ID">100</td>
<td data-title="designation">Software Engineer</td>
</tr>
<tr>
<td data-title="Name">Shyam</td>
<td data-title="ID">101</td>
<td data-title="designation">Quality Analyst</td>
</tr>
<tr>
<td data-title="Name">Mark</td>
<td data-title="ID">102</td>
<td data-title="designation">Software Engineer</td>
</tr>
<tr>
<td data-title="Name">Bill</td>
<td data-title="ID">104</td>
<td data-title="designation">Quality Analyst</td>
</tr>
<tr>
<td data-title="Name">Arjun</td>
<td data-title="ID">105</td>
<td data-title="designation">Human Resources</td>
</tr>
<tr>
<td data-title="Name">Pratyush</td>
<td data-title="ID">106</td>
<td data-title="designation">Software Engineer</td>
</tr>
<tr>
<td data-title="Name">Anant</td>
<td data-title="ID">101</td>
<td data-title="designation">Administrative Dept</td>
</tr>
</tbody>
</table>
</body>
</html>
In above snippet you can use media query which screen size you want to stack td of table eg:300px
Kailash Chandra's answer gave me the clue for something I was trying to do. I wanted a 2 column table that was very compact in most cases, and Bootstrap always forces a minimum width on the first column. I just wanted a table that would act like a table until I got down to phone size, and then have it turn into a single row alternating a heading and the value below it. That turned out to be pretty trivial with this bit of css, which basically just uses the same trick to make it quit acting like a table. In this case I'm not changing anything else about it as it meets my need, but it would be easy enough to add some styling.
#media only screen and (max-width: 400px) {
.flyout-table table,
.flyout-table th,
.flyout-table td,
.flyout-table tr {
display: block;
}
}
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
I am working on responsive css for a table. I read a tutorial from this link http://css-tricks.com/responsive-data-tables/ (How to make responsive table on css). It worked on some sample page but in my HTML page I have some problem on re-sizing of window and the table does not respond correctly. Here's my HTML code:
<style>
/*
Generic Styling, for Desktops/Laptops
*/
table
{
width: 100%;
border-collapse: collapse;
}
/* Zebra striping */
tr:nth-of-type(odd)
{
background: #eee;
}
th
{
background: #333;
color: white;
font-weight: bold;
}
td, th
{
padding: 6px;
border: 1px solid #ccc;
text-align: left;
}
/*
Max width before this PARTICULAR table gets nasty
This query will take effect for any screen smaller than 760px
and also iPads specifically.
*/
#media only screen and (max-width: 760px), (min-device-width: 768px) and (max-device-width: 1024px)
{
/* 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-left: 50%;
}
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;
}
/*
Label the data
*/
td:nth-of-type(1):before
{
content: "First Name";
}
td:nth-of-type(2):before
{
content: "Last Name";
}
td:nth-of-type(3):before
{
content: "Job Title";
}
td:nth-of-type(4):before
{
content: "Favorite Color";
}
td:nth-of-type(5):before
{
content: "Wars of Trek?";
}
td:nth-of-type(6):before
{
content: "Porn Name";
}
td:nth-of-type(7):before
{
content: "Date of Birth";
}
td:nth-of-type(8):before
{
content: "Dream Vacation City";
}
td:nth-of-type(9):before
{
content: "GPA";
}
td:nth-of-type(10):before
{
content: "Arbitrary Data";
}
}
</style>
<body>
<table>
<thead>
<tr>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
Job Title
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
James
</td>
<td>
Matman
</td>
<td>
Chief Sandwich Eater
</td>
</tr>
<tr>
<td>
The
</td>
<td>
Tick
</td>
<td>
Crimefighter Sorta
</td>
</tr>
</tbody>
</table>
</body>
</html>
It shows the output on re-size window like the figure below:
and it worked well on js-fiddle like the figure below:
Link: http://jsfiddle.net/Cd9Yp/
What's wrong in my code? Can any one guide me, thanks!
Try to define a min-width between your specified viewport. To the parent element table and the child tr if wanted / needed.
table {
width: 100%;
min-width: 320px; // this should do it
border-collapse: collapse;
}
And.
tr {
min-width: 200px; // specify if additionally needed or wanted
}
What resolution are you optimizing for -- I see your #media is tablet but your shooting for smaller obviously -- you shouldn't need to go below 320px; in most cases, so if you minimize below that it shouldn't matter.
Also, why not use nested divs?
It's small mistake i don't specify top of html document type as html5
after it does work perfectly on existing code
<!DOCTYPE html>