Which is the best practices to create a responsive table? [closed] - html

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have this sample:
link
CODE HTML:
<table class="table table-striped">
<thead>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Email</td>
<td>Phone</td>
<td>Adresss</td>
<td>Message</td>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Smith</td>
<td>jhs#yahoo.com</td>
<td>0766323123</td>
<td>Test</td>
<td>Test</td>
</tr>
<tr>
<td>Ronny</td>
<td>Stuard</td>
<td>ros#yahoo.com</td>
<td>0877223534</td>
<td>Test2</td>
<td>Test2</td>
</tr>
</tbody>
</table>
I found this example and want to do something like
sample responsive table
What I do not understand is how CSS code is written.
I need a short sample code CSS to turn my table to be like over there.Also if you know better examples to make a table responsive please put here
After all that is the best way to make a table responsive?
Thanks in advance!

Use this code of css for your table:
#media
only screen and (max-width: 500px),
(min-device-width: 500px) and (max-device-width: 500px) {
/* 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";
text-align: left;
font-size: 12px;
}
td:nth-of-type(2):before { content: "Last Name";
text-align: left;
font-size: 12px;
}
td:nth-of-type(2):before { content: "Email";
text-align: left;
font-size: 12px;
}
td:nth-of-type(2):before { content: "Phone";
text-align: left;
font-size: 12px;
}
td:nth-of-type(2):before { content: "Address";
text-align: left;
font-size: 12px;
}
td:nth-of-type(2):before { content: "Message";
text-align: left;
font-size: 12px;
}
}
#media (max-width: 500px)
{
td
{
font-size: 12px!important;
text-align: right!important;
padding-right: 5px;
}
}
Add the size of the screen as you want the table to be converted in responsive like the example you gave. In this case is for 500px and less.

Related

How to text ellipsis with width auto / dynamically?

I have a table where I group all the orders of a user. The product name row is often very large, so the text comes out of the table and breaks the layout of the entire page.
To solve this I am using text ellipsis with a max-width: 200px;. This is fine on desktop, but when I switch to tablet or mobile the 200px width is no longer fine.
So I would have to change max-width every time according to the browser display width (mobile, tablet, portrait etc ..), this is insane.
So is there any way to make the width of text ellipsis work automatically? Or determine the width of the text ellipsis based on the row width percentage ?
To better explain the problem
I used a wide range of media queries for the convenience of the question. .product_name is 100% wide and that's what I want.
.product_name> a on the other hand has a fixed width of 200px, so even if the screen gets bigger or smaller, the product name is always displayed with the same width.
To solve this problem I would have to use different media queries and change the max-width of .product_name> a every time, which I want to avoid.
Is there a way to tell the max-width to automatically fit the width of td.product name ?
/*Global Container For Table*/
.global_container.order {
gap: 12px;
}
/* Table Width */
.product_number { width: 10%; }
.product_name { width: 35%; }
.product_data { width: 15%; }
.product_price { width: 10%; }
.product_status { width: 15%; }
.product_action { width: 15%; }
/* Table Heading */
.table_orders.heading > tbody > tr > td {
color: #4B525F;
font-weight: 600;
}
.table_orders.heading {
border: solid #e0e0e0;
border-width: 0px 0px 1px 0px;
padding: 0px 0px 12px 0px;
width: 100%;
}
/* Table Orders */
.table_orders {
text-align: left;
border: 0;
font-weight: 400;
color: #737477;
padding-top: 12px;
width: 100%;
}
td {
border: 0;
font-size: 13px;
}
/* Text Ellipsis */
.product_name {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 200px;
}
/*** Mobile Device Range ***/
#media only screen and (min-width: 280px) and (max-width: 1024px){
.table_orders.heading { display: none; }
/* Name Before Value */
.product_number::before { content: 'Ordine'; }
.product_name::before { content: 'Prodotto'; }
.product_data::before { content: 'Data'; }
.product_price::before{ content: 'Prezzo'; }
.product_status::before { content: 'Stato'; }
.product_action::before { content: 'File'; }
/* Items */
.product_name {
white-space: unset;
overflow: unset;
text-overflow: unset;
max-width: unset;
}
.product_name > a {
text-align: right;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
max-width: 200px;
}
td {
display: flex;
width: 100%!important;
flex-direction: row;
justify-content: space-between;
}
}
<table class="table_orders heading">
<tr class="table_row">
<td class="product_number">Order ID</td>
<td class="product_name">Product Name</td>
<td class="product_data">Date</td>
<td class="product_price">Total</td>
<td class="product_status">Status</td>
<td class="product_action">File</td>
</tr>
</table>
<table class="table_orders">
<tr class="table_row_items">
<td class="product_number">
<span>#12345</span>
</td>
<td class="product_name">
Product Name is very long long long long long long long long long long long
</td>
<td class="product_data">
<span>02/08/2022</span>
</td>
<td class="product_price">
<span>Order Total</span>
</td>
<td class="product_status">
<span>Purchuased</span>
</td>
<td class="product_action">
Show
</td>
</tr>
</table>
Use #media to change css based on screen size.
Example:
#media only screen and (max-width: 600px) {
.product_name {
max-width: 100px;
}
}
Code:
/*Global Container For Table*/
.global_container.order {
gap: 12px;
}
/* Table Width */
.product_number { width: 10%; }
.product_name { width: 35%; }
.product_data { width: 15%; }
.product_price { width: 10%; }
.product_status { width: 15%; }
.product_action { width: 15%; }
/* Table Heading */
.table_orders.heading > tbody > tr > td {
color: #4B525F;
font-weight: 600;
}
.table_orders.heading {
border: solid #e0e0e0;
border-width: 0px 0px 1px 0px;
padding: 0px 0px 12px 0px;
width: 100%;
}
/* Table Orders */
.table_orders {
text-align: left;
border: 0;
font-weight: 400;
color: #737477;
padding-top: 12px;
width: 100%;
}
td {
border: 0;
font-size: 13px;
}
/* Text Ellipsis */
.product_name {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 200px;
}
#media only screen and (max-width: 600px) {
.product_name {
max-width: 100px
}
}
<table class="table_orders heading">
<tr class="table_row">
<td class="product_number">Order ID</td>
<td class="product_name">Product Name</td>
<td class="product_data">Date</td>
<td class="product_price">Total</td>
<td class="product_status">Status</td>
<td class="product_action">File</td>
</tr>
</table>
<table class="table_orders">
<tr class="table_row_items">
<td class="product_number">
<span>#12345</span>
</td>
<td class="product_name">
Product Name is very long long long long long long long long long long long
</td>
<td class="product_data">
<span>02/08/2022</span>
</td>
<td class="product_price">
<span>Order Total</span>
</td>
<td class="product_status">
<span>Purchuased</span>
</td>
<td class="product_action">
Show
</td>
</tr>
</table>

WYSIWYG Table Making it responsive

So I am getting a table from a content management system called ButterCMS. And they have tables in the WYSIWYG components but I am trying to make it responsive without using the bootstrap scrollable horizon.
My code looks like this:
#import '../../../../styles/_variables.scss';
#import '../../../../../node_modules/bootstrap/scss/functions';
#import '../../../../../node_modules/bootstrap/scss/mixins';
#import '../../../../../node_modules/bootstrap/scss/variables';
#import '../../../../../node_modules/bootstrap/scss/_tables.scss';
table {
#extend .table;
text-align: center;
display: block;
max-width: -moz-fit-content;
max-width: 80%;
margin: 0 auto;
overflow-x: auto;
}
table,
tr,
td,
tbody {
border: none !important;
text-align: center !important;
}
#media only screen and (max-width: 991px) {
td,
tr {
display: block;
border: none
}
tr {
width: 100%;
border-bottom: 1px solid;
}
tr:nth-child(even)>td {
position: relative;
line-height: 22px;
}
td {
position: relative;
}
td:before {
position: absolute;
left: 6px;
width: 45%;
padding-right: 10px;
white-space: nowrap;
color: #000;
font-weight: bold;
text-align: left;
}
}
<table border="1" style="width: 98.4261%; height: 292px;">
<tbody>
<tr>
<td>
<h3>Header</h3>
</td>
<td>
<h3>Header</h3>
</td>
<td>
<h3>Header</h3>
</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
</tbody>
</table>
The issue is that from the CMS the table comes as is. I am able to get the <tr></tr> stacked on top of each other which is just how a table works. So, when I am on mobile screens how could I get the first <tr></tr> <td></td> to act as a header for the columns? The CMS regular implementation of the table doesn't give headers. And I don't want users editing the source code for tables inside the WYSIWYG. I can change the td under the media query to position: absolute; but then would have to give fixed positions to align it for the headers.
The behavior I am trying to get would be:
Header
content
Header
content
Header
content

CSS responsive code not working when given under class element

New to CSS! I have a css code written to make my tables responsive. The same code works perfectly when put inside directly in <style> tag but doesnt work when i put a class and refer that class in div. Below are the codes:
WORKING:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#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: "Secret Alias"; }
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>
</head>
<body>
<div>
<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>
</div>
</body>
</html>
NOT WORKING:
<!DOCTYPE html>
<html>
<head>
<style>
.custom-accordiontable{
/*
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 {
margin: 0 0 1rem 0;
}
tr:nth-child(odd) {
background: #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: 0;
left: 6px;
width: 45%;
padding-right: 10px;
white-space: nowrap;
}
/*
Label the data
You could also use a data-* attribute and content for this. That way "bloats" the HTML, this way means you need to keep HTML and CSS in sync. Lea Verou has a clever way to handle with text-shadow.
*/
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: "Secret Alias"; }
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>
</head>
<body>
<div class="custom-accordiontable">
<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>
</tbody>
</table>
</div>
</body>
</html>
Since you are adding a class in your parent div element which is custom-accordionable
You have to pass that parent element class every table,tr,th,td when you are doing css.
Here's a best example: (Also when you are doing media queries the css class should be inside the media query tag for it to work.)
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#media
only screen and (max-width: 760px),
(min-device-width: 768px) and (max-device-width: 1024px) {
/* Force table to not be like tables anymore */
.custom-accordiontable table, .custom-accordiontable thead, .custom-accordiontable tbody, .custom-accordiontable th, .custom-accordiontable td, .custom-accordiontable tr {
display: block;
}
/* Hide table headers (but not display: none;, for accessibility) */
.custom-accordiontable thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}
.custom-accordiontable tr { border: 1px solid #ccc; }
.custom-accordiontable td {
/* Behave like a "row" */
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 50%;
}
.custom-accordiontable 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
*/
.custom-accordiontable td:nth-of-type(1):before { content: "First Name"; }
.custom-accordiontable td:nth-of-type(2):before { content: "Last Name"; }
.custom-accordiontable td:nth-of-type(3):before { content: "Job Title"; }
.custom-accordiontable td:nth-of-type(4):before { content: "Favorite Color"; }
.custom-accordiontable td:nth-of-type(5):before { content: "Wars of Trek?"; }
.custom-accordiontable td:nth-of-type(6):before { content: "Secret Alias"; }
.custom-accordiontable td:nth-of-type(7):before { content: "Date of Birth"; }
.custom-accordiontable td:nth-of-type(8):before { content: "Dream Vacation City"; }
.custom-accordiontable td:nth-of-type(9):before { content: "GPA"; }
.custom-accordiontable td:nth-of-type(10):before { content: "Arbitrary Data"; }
}
</style>
</head>
<body>
<div class="custom-accordiontable">
<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>
</div>
</body>
</html>
Quick Hints
[1] When you use css without responsive you should have a file called styles.css and use link tag to link that style inside your head tag.
<head>
<link rel="stylesheet" href="styles.css">
</head>
2 When you use media queries to do some modification in responsive mode add another file called media.css to separate your styles in media queries only.
<head>
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="media.css">
</head>
And inside your media file this should only call media queries and certain width you want to modify.
Here are some few examples:
#media only screen and (max-width: 1024px) {
// do some css modification here...
}
#media only screen and (max-width: 600px) {
// do some css modification here...
}
That way you can have a cleaner code and not putting all the styles inside the style tag in your html document.

incorrect horizontal lineup for table

https://jsfiddle.net/tbnzaga9/
#smenu a {
color: black;
text-decoration: none;
}
.button:hover {
background-color: #A9A9A9;
}
.button {
position: absolute;
border: 1px solid black;
cursor: pointer;
background-color: #d3d3d3;
width: 30%;
left: 1%;
}
.right {
position: absolute;
right: 60%;
}
#smenu table tr td p {
display: inline-block;
float: left;
}
<div id='smenu'>
<table>
<tr>
<th> Title</th>
<th class='right'> Date</th>
</tr>
<tr>
<td class='button'>test</td>
<td class='right'>
<p>4/27</p>
</td>
</tr>
</table>
</div>
I was making a list of separate websites and the dates they were made, but I can't get them to line up! I tried float:left and display: inline-block, but it's just not lining up. Any suggestions?
Also - is there a better way than using a table?
try these change in CSS.
#smenu a {
color: black;
text-decoration: none;
}
.button:hover {
background-color: #A9A9A9;
}
.button {
border: 1px solid black;
cursor: pointer;
background-color: #d3d3d3;
}
td {
text-align: center;
width: 200px;
height:25px;
}
#smenu table tr td p {
//display: inline-block;
//float: left;
}
Try to replace <p></p> by <div></div> or use a css reset sheet to clear the paragraph doing a line break. Also, I think tables are ok to do things like this, but limit you more. If you really wanted to you could mimic a table with some divs where you have your rows and in each one have your columns you could have differents classes by rows and by column in different rows.
I would remove all "position:absolute".
Given that you have tabular data, it makes sense to just use a regular table tag.
I've also added the following styles:
.right {
padding-right: 60%;
}
#smenu table {
width:100%;
}
.right p {
margin:0px;
}
th {
text-align: left;
}
You can see the changes here:
https://jsfiddle.net/tbnzaga9/2/

Media query not working while using hostname in URL

I am designing a responsive html page containing images and tables.
Please see below code block of html.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='UTF-8'>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
margin: 0;
padding: 0;
}
body {
/*font: 14px/1.4 Georgia, Serif;*/
font-size: 11.0pt;
font-family: "Calibri","sans-serif";
}
#page-wrap {
margin: 5px;
}
p {
margin: 20px 0;
}
/*
Generic Styling, for Desktops/Laptops
*/
table {
width: 100%;
border-collapse: collapse;
}
/* Zebra striping */
tr:nth-of-type(odd) {
background: #eee;
}
th {
background: #5388FF;
color: white;
font-weight: bold;
}
td, th {
padding: 6px;
border: 1px solid #ccc;
text-align: left;
}
</style>
<style>
#media only screen and (max-width: 760px), (min-device-width: 768px) and (max-device-width: 1024px) {
img {
width: 100%;
}
/* 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: "Employee ID";
}
td:nth-of-type(2):before {
content: "Employee Name";
}
td:nth-of-type(3):before {
content: "Employee Address";
}
td:nth-of-type(4):before {
content: "Employee Phone";
}
}
/* Smartphones (portrait and landscape) ----------- */
#media only screen and (min-device-width : 320px) and (max-device-width : 480px) {
body {
padding: 0;
margin: 0;
width: 320px;
}
img {
width: 100%;
}
}
/* iPads (portrait and landscape) ----------- */
#media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
body {
width: 495px;
}
img {
width: 100%;
}
}
</style>
</head>
<body>
<div id="page-wrap">
<img max-width="100%" src="Images/image001.png">
<table>
<thead>
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Address</th>
<th>Employee Phone</th>
</tr>
</thead>
<tbody>
<tr>
<td>101</td>
<td>John Smith</td>
<td>US</td>
<td>45454545</td>
</tr>
<tr>
<td>102</td>
<td>Dave Furber</td>
<td>US</td>
<td>45454546</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
When I browse the html page using hostname like http://mymachine:6000/test.html, the media query does not work in IE11 but works in Chrome.
However when I browse the page using IP in the URL the media query works just fine and the page is rendered correctly in Chrome and IE 11 for all the devices.
Can someone provide me inputs on how to resolve this issue?
Thanks,
Vinayak
For some reason IE was using IE7 mode for rendering this page.
Reference : Link
Adding the below tag solved the problem !!
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
Thanks,
Vinayak
Are You using some kind of testing environment like XAMPP? It would also be helpful if we could see Your Html (cant comment, don't have enough reputation).