CSS table class is affecting all tables outside of class - html

I'm trying to get a new page up containing a responsive table, using the code below. I've put the code in its own class 'resptable' but for some reason its affecting all tables (even ones not in any class) throughout my site. I can't seem to find why. Any help would be appreciated, thank you!
The CSS -
/*
Generic Styling, for Desktops/Laptops
*/
.resptable table {
width: 100%;
border-collapse: collapse;
}
/* Zebra striping */
.resptable tr:nth-of-type(odd) {
background: #eee;
}
.resptable th {
background: #333;
color: white;
font-weight: bold;
}
.resptable 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 */
.resptable table, thead, tbody, th, td, tr {
display: block;
}
/* Hide table headers (but not display: none;, for accessibility) */
.resptable thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}
.resptable tr { border: 1px solid #ccc; }
.resptable td {
/* Behave like a "row" */
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 50%;
}
.resptable 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
*/
.resptable td:nth-of-type(1):before { content: "First Name"; }
.resptable td:nth-of-type(2):before { content: "Last Name"; }
.resptable td:nth-of-type(3):before { content: "Job Title"; }
.resptable td:nth-of-type(4):before { content: "Favorite Color"; }
.resptable td:nth-of-type(5):before { content: "Wars of Trek?"; }
.resptable td:nth-of-type(6):before { content: "Porn Name"; }
.resptable td:nth-of-type(7):before { content: "Date of Birth"; }
.resptable td:nth-of-type(8):before { content: "Dream Vacation City"; }
.resptable td:nth-of-type(9):before { content: "GPA"; }
.resptable td:nth-of-type(10):before { content: "Arbitrary Data"; }
}
And here is the HTML:
<table class="resptable">
<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>

Such selector:
.resptable table, thead, tbody, th, td, tr
points to:
.resptable table,
thead,
tbody,
th,
td,
tr
So, just first selector in a list has your class resptable. Others are points to every thead, tbody, th, td, tr elements (without your class resptable).
And other point is that you table has resptable class. So, table is not in .resptable but table is .resptable:
table.resptable
I believe you was trying to write another one:
table.resptable,
.resptable thead,
.resptable tbody,
.resptable th,
.resptable td,
.resptable tr

Related

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.

Tfoot width misaligned when fixed to bottom in a responsive table

I want to fix the tfoot to the bottom of the page with it being as responsive as the rows of the table.
My code: https://codepen.io/adu8/pen/MWWXqGY
The problem I face now is once I add the class="fixed-bottom" to the tr of tfoot, it goes to bottom and gets misaligned.
I tried fixing it with having a workaround by adding this css:
tfoot td {
display: inline-block;
width: calc((100%/8) - 10px);
}
As in this codepen: https://codepen.io/adu8/pen/dyyKqOv. However, it's still not able to be as responsive.
I would appreciate if someone helps fixing this.
Please use this CSS
body {
background-color: midnightblue;
color: white;
}
table {
max-width: 100%;
font-size: 2vw;
}
thead, tr, tfoot {
text-align: center;
background-color: midnightblue;
}
.table-striped>tbody>tr:nth-child(odd)>td,
.table-striped>tbody>tr:nth-child(odd)>th {
background-color: darkslateblue;
}
.table-hover tbody tr:hover > th {
/*background-color: #06765d;*/
background-color: #009999;
}
.table td {
padding: 0.05rem;
margin: 0px;
}
.fixed-bottom {
width: 100%;
}
table tr {
display: flex; display: -webkit-flex;
width: 100%;
}
tr th,
tr td {
width: calc(100% / 8);
}
tr th:first-child,
tr td:first-child {
width: 22%;
flex-shrink: 0;
-webkit-flex-shrink: 0;
}

Vertical allign middle with fixed header and table responsive bootstrap

I am having trouble getting the text centered in the middle of the cell, the single lines are fine but the double lines do not get in the center of the cell :
I have a simpler version on JSfiddle of my code but with all the css that I use.
When I remove the plan-list-view-table class (for the FIXED header) the vertical align works, but I cant get around the display:block without breaking the fixed header.
The code that is breaking the vertical align in particular:
#plan-list-view-table tr:after { /* clearing float */
content: ' ';
display: block;
clear: both;
}
You can use flexbox properties on the td
#plan-list-view-table thead,
#plan-list-view-table tbody,
#plan-list-view-table tfoot,
#plan-list-view-table tr,
#plan-list-view-table th,
#plan-list-view-table td {
display: block;
}
#plan-list-view-table thead tr,
#plan-list-view-table tfoot tr {
/* fallback */
width: 97%;
/* minus scroll bar width */
width: -webkit-calc(100% - 16px);
width: -moz-calc(100% - 16px);
width: calc(100% - 16px);
}
#plan-list-view-table tr:after {
/* clearing float */
content: ' ';
display: block;
/*visibility: hidden;*/
clear: both;
}
.table-responsive>.table>tbody>tr>td {
vertical-align: middle;
}
#plan-list-view-table tbody {
height: 65vh;
}
#plan-list-view-table tbody td,
#plan-list-view-table thead th,
#plan-list-view-table tfoot td {
width: 16.70%;
float: left;
}
#plan-list-view-table tbody td {
/* height: 50px; */
}
#plan-list-view-table thead th {
height: 35px;
}
/* Every odd column has a light grey color */
#table-body tr {
background-color: rgba(224, 224, 224, 0.3);
}
/* Hover on plan entry */
#table-body tr:hover {
background-color: rgba(233, 233, 234, 1);
cursor: pointer;
}
/* Green checkbox for "Published" column */
#table-body tr td.green {
color: green;
}
/* Red checkbox for "Published" column (Not working)*/
#table-body tr td.fade {
color: red;
}
/* Right delimiter between plans in the plan list*/
#table-head-data tr th,
#table-body tr td {
border-right: thin solid rgb(224, 224, 224);
background: transparent;
vertical-align: middle;
}
.table-responsive>.table>tbody>tr>td {
vertical-align: middle;
}
.table-responsive {
overflow: auto;
/*margin-bottom:20px;*/
}
.table {
border-left: 0px;
width: 100%;
table-layout: fixed;
text-align: center;
font-family: 'Lato', sans-serif;
}
.planListItem td {
display: flex !important;
justify-content: center;
align-items: center;
height: 50px;
}
<div class="table-responsive" id="table-res">
<table class="table" id="plan-list-view-table">
<thead id="table-head-data">
<tr>
<th> Name <span class="glyphicon"></span></th>
<th> Description <span class="glyphicon"></span></th>
<th> Start <span class="glyphicon"></span></th>
<th> End <span class="glyphicon"></span></th>
<th> Published Date <span class="glyphicon"></span></th>
</tr>
</thead>
<tbody id="table-body" #plantable>
<tr class="planListItem">
<td style="overflow: hidden;">Test</td>
<td style="overflow: hidden;">TestDoubleLine TestDoubleLine TestDoubleLine</td>
<td style="overflow: hidden;">3 TestDoubleLine oubleLine</td>
<td style="overflow: hidden;">4</td>
<td style="overflow: hidden;">5</td>
</tr>
</tbody>
</table>
</div>
not sure of all side effects but these are working (note commented-out css):
#plan-list-view-table tbody {
/* height: 65vh; */
}
#plan-list-view-table thead, #plan-list-view-table tbody,
#plan-list-view-table tfoot, #plan-list-view-table tr,
#plan-list-view-table th, #plan-list-view-table td {
/* display: block; */
}
#plan-list-view-table tbody td, #plan-list-view-table thead th,
#plan-list-view-table tfoot td {
/* float: left; */
width: 16.7%;
}

Media query not working on iOS

I have been having problem with table not wanting to change (everything else scales as it should) responsively using media queries on iOS, it works perfectly on android phones and tablets, but not on iOS. I have tried adding viewport meta tag, but it doesn't help. Anyone know how to fix it?
Part of media query that doesn't work:
table, thead, tbody, th, td, tr {
display: block;
max-width:100%;
}
th{
display:none;
}
td {
border: none;
position: relative;
padding-left: 50%;
font-size: 1em;
background-color: #F6F7FB;
}
td:before {
position: absolute;
top: 6px;
left: 6px;
width: 45%;
padding-right: 10px;
white-space: nowrap;
line-height: 4em;
vertical-align: middle;
font-weight: bold;
}
td:nth-of-type(2):before { content: "test"; }
td:nth-of-type(3):before { content: "test"; }
td:nth-of-type(4):before { content: "test"; }
td:nth-of-type(5):before { content: "test"; }
td:nth-of-type(6):before { content: "test"; }
td:nth-of-type(7):before { content: "test"; }
I am not sure, but for me changing display of the isn't correct. Also the position relative/absolut in tables won't work in most cases. Is it possible for you, to change table structure to divs structure with display table and table-cell? I always do that, when need to make some big table responsive and there is no problem with that

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

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.