What I'm trying to achieve is that when hovering over the td, that the background changes to black and the text color to white, but I only get the black background. I believe this happens because there is an interference with the "blueColor" and "redColor" classes with the "hover:hover" class.
td {
text-align: center;
}
table {
width: 100%;
font-size: 35px;
font-family: Arial;
font-weight: bold;
}
.blackBorder {
border: 1px solid black;
}
p.redColor {
color: red;
}
p.blueColor {
color: blue;
}
.lightGreenBackground {
background-color: #b5f79e;
}
.hover:hover {
background-color: black;
color: white;
}
<table>
<tr>
<td class="lightGreenBackground blackBorder hover">
<p class="redColor">1</p>
<p class="blueColor">H</p>
</td>
</tr>
</table>
I have tried using the !important property without any success and I'm not allowed to use id's.
Because you already have a color defined on p and not td.hover means you will have to change the color of <p> when td.hover is in a hovered state.
You'll notice if you remove both colors from your CSS, the text will turn white as expected on hover.
You can target both p's and change the color using .hover:hover > p.
td {
text-align: center;
}
table {
width: 100%;
font-size: 35px;
font-family: Arial;
font-weight: bold;
}
.blackBorder {
border: 1px solid black;
}
p.redColor {
color: red;
}
p.blueColor {
color: blue;
}
.lightGreenBackground {
background-color: #b5f79e;
}
.hover:hover {
background-color: black;
}
.hover:hover > p {
color: #fff;
}
<table>
<tr>
<td class="lightGreenBackground blackBorder hover">
<p class="redColor">1</p>
<p class="blueColor">H</p>
</td>
</tr>
</table>
I have two tables, side by side, surrounded by a shared border. The last row in the first table contains a list with dynamically generated data (usually one word each). The second cell in the second table contains a list with dynamically generated data (usually 31 words).
In
Firefox, the table row color (light blue) for the first table extends down to the shared border.
Whereas, in Edge/Chrome, the table row color (light blue) for the first table does not extend down to the shared border.
Is this a difference in how Firefox styles tables? Or am I missing a property in my code? I used normalize.css, if that makes a difference.
<style>
/* general styles */
#generate {
margin-top: 15%;
height: 100%;
text-transform: capitalize;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
align-content: center;
}
/* button styles */
#button-generate {
background-color: #76cef2;
font-size: 8rem;
text-align: center;
text-shadow: 5px 5px 8px #93aef9;
border: 6px solid #58efec;
border-radius: 10px;
padding: 20px 100px 20px 100px;
}
#button-generate:active {
background-color: #a29dfc;
text-shadow: 5px 5px 8px #76cef2;
}
/* table styles */
.table {
margin: 5%;
font-size: 1.8rem;
text-align: left;
text-shadow: 1px 1px 2px #b18dff;
border: 4px solid #000000;
border-collapse: collapse;
border-spacing: 0;
border-radius: 10px;
display: flex;
flex-direction: row;
table-layout: auto;
width: auto;
}
.table tr:nth-child(even) {
background-color: #93aef9;
}
.table tr:nth-child(odd) {
background-color: #85bef6;
}
.table td {
border: 4px solid #a29dfc;
padding: 2%;
}
.table td:focus {
font-weight: bold;
}
#purple-background-bold {
background-color: #93aef9;
font-weight: bold;
}
/* list styles */
.list {
list-style-type: none;
padding: 0;
line-height: 1.6;
}
.list li:focus {
font-weight: bold;
}
#two-column-list {
columns: 3;
-webkit-columns: 3;
-moz-columns: 3;
}
#bold {
font-weight: bold;
}
</style>
<div class="table">
<table v-show="isGenerated">
<tr>
<td id="bold">Cat1:</td>
<td>{{ stuff1 }}</td>
</tr>
<tr>
<td id="bold">Cat2:</td>
<td >{{ stuff2 }}</td>
</tr>
<tr>
<td id="bold">Cat3:</td>
<td>{{ stuff3 }}</td>
<ul class="list">
<li>Cat4: {{ stuff4 }}</li>
<li>Cat5: {{ stuff5 }}</li>
<li>Cat6: {{ stuff6 }}</li>
</ul>
</td>
</tr>
</table>
<table v-show="isGenerated">
<tr>
<td id="purple-background-bold">Generated List</td>
<td>
<ul class="list" id="two-column-list">
<li v-for="(item, index) in listItems" :key="index">{{ index + 1 }}. {{ item }}</li>
</ul>
</td>
</tr>
</table>
</div>
I'm trying to make a responsive table, but I have no idea about how can I achieve this. After googling, could not find a solution.
So, I have this table in desktop view:
1 2 3
I B C D
II C D E
III D E F
IV E F G
And I'm trying to get this in responsive view:
1
I B
II C
III D
IV E
2
I C
II D
III E
IV F
etc..
Any kind of help would be appreciated
There are several available, responsive table solutions including this simple, CSS-only pattern:
data-label attribute :
First, we’ll add a data-label attribute to each data cell with a value that represents that column’s name. That will be used for labeling purposes in the responsive layout.
In smaller viewports the and elements will display as block-level and not as table rows and cells. And the ::before pseudo-class now serves as a label.
body {
font-family: "Open Sans", sans-serif;
line-height: 1.25;
}
table {
border: 1px solid #ccc;
border-collapse: collapse;
margin: 0;
padding: 0;
width: 100%;
table-layout: fixed;
}
table caption {
font-size: 1.5em;
margin: .5em 0 .75em;
}
table tr {
background-color: #f8f8f8;
border: 1px solid #ddd;
padding: .35em;
}
table th,
table td {
padding: .625em;
text-align: center;
}
table th {
font-size: .85em;
letter-spacing: .1em;
text-transform: uppercase;
}
#media screen and (max-width: 600px) {
table {
border: 0;
}
table caption {
font-size: 1.3em;
}
table thead {
border: none;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
table tr {
border-bottom: 3px solid #ddd;
display: block;
margin-bottom: .625em;
}
table td {
border-bottom: 1px solid #ddd;
display: block;
font-size: .8em;
text-align: right;
}
table td::before {
/*
* aria-label has no advantage, it won't be read inside a table
content: attr(aria-label);
*/
content: attr(data-label);
float: left;
font-weight: bold;
text-transform: uppercase;
}
table td:last-child {
border-bottom: 0;
}
}
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="ID">1</td>
<td data-label="Name">Jake</td>
<td data-label="Age">23</td>
</tr>
<tr>
<td data-label="ID">2</td>
<td data-label="Name">Dave</td>
<td data-label="Age">45</td>
</tr>
</tbody>
</table>
the URL is http://action.news
there are 3 columns in a fixed width table)
(i removed the script that loads on demand) to make things easier.
thank you kindly
what i have done:
added this code below
https://gist.github.com/hkirsman/3002480
added this per google instructions
<meta name=viewport content="width=device-width, initial-scale=1">
i have read HTML table with auto-fit for some columns, fixed width for others
but still cant get the page to be mobile friendly, the top part seems to realign
I've found this example on the Codepen so you can try it and adjust it to your needs:
Here's some code
HTML:
<table class="js-table participants-table">
<caption>
<p>Responsive Table</p>
</caption>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Gender</th>
<th>Picture</th>
</tr>
</thead>
<tbody>
<!--filled from javascript-->
</tbody>
</table>
CSS:
HTML {
font-size: 100%;
line-height: 1.5;
font-family: 'Raleway', sans-serif;
}
BODY {
margin: 0;
}
*, *:before, *:after {
box-sizing: border-box;
}
IMG {
max-width: 100%;
}
/*Style For Table*/
.participants-table {
table-layout: fixed;
border-collapse: collapse;
margin: 0 auto;
width: 90%;
}
.participants-table TD,
.participants-table TH {
border: 1px solid #b4adad;
}
.participants-table IMG {
width: 150px;
}
.participants-table THEAD {
display: none;
}
.participants-table TD {
display: block;
position: relative;
overflow-x: hidden;
text-overflow: ellipsis;
}
#media only screen and (min-width: 450px) {
.participants-table {
width: auto;
}
.participants-table TD,
.participants-table TH {
padding: .2em 1em;
}
.participants-table THEAD {
display: table-header-group;
}
.participants-table TD {
display: table-cell;
position: static;
}
.participants-table TD:before {
display: none;
}
}
link to the example
I have a table that looks like this:
It is built using a standard table structure:
<table id="dashboard" class="table table-condensed table-hover table-striped table-bordered sortableTable responsive-table table-header-rotated">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Date Field</th>
<th class="rotate"><div><span>Attribute 1</span></div></th>
<th class="rotate"><div><span>Attribute 2</span></div></th>
<th class="rotate"><div><span>Attribute 3</span></div></th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="Col 1">Data 1</td>
<td data-label="Col 2">Data 2</td>
<td data-label="Date Field" class="success">2014-07-03</td>
<td data-label="Attribute 1" class="success"> </td>
<td data-label="Attribute 2" class="success"> </td>
<td data-label="Attribute 3" class="success"> </td>
</tr>
<tr>
<td data-label="Col 1">Data 3</td>
<td data-label="Col 2">Data 4</td>
<td data-label="Date Field" class="warning">2014-06-03</td>
<td data-label="Attribute 1" class="success"> </td>
<td data-label="Attribute 2" class="warning"> </td>
<td data-label="Attribute 3" class="success"> </td>
</tr>
</tbody>
</table>
And the table-header-rotated looks like this to get the vertical column headers
.table-header-rotated th.row-header{
width: auto;
}
.table-header-rotated td{
width: 10px;
vertical-align: middle;
text-align: center;
}
.table-header-rotated th.rotate{
height: 80px;
width: 10px;
position: relative;
vertical-align: bottom;
padding: 0;
font-size: 12px;
line-height: 0.8;
}
.table-header-rotated th.rotate > div{
position: relative;
top: 0px;
height: 100%;
-ms-transform:skew(0deg,0deg);
-moz-transform:skew(0deg,0deg);
-webkit-transform:skew(0deg,0deg);
-o-transform:skew(0deg,0deg);
transform:skew(0deg,0deg);
overflow: hidden;
}
.table-header-rotated th.rotate span {
-ms-transform:skew(0deg,0deg) rotate(270deg);
-moz-transform:skew(0deg,0deg) rotate(270deg);
-webkit-transform:skew(0deg,0deg) rotate(270deg);
-o-transform:skew(0deg,0deg) rotate(270deg);
transform:skew(0deg,0deg) rotate(270deg);
position: absolute;
bottom: 30px;
left: -20px;
display: inline-block;
text-align: left;
text-align: center;
}
When the page is less than a certain width, it collapses to look like this:
This is done using this CSS:
#media
only screen and (max-width: 900px),
(min-device-width: 768px) and (max-device-width: 1024px) {
/* Force table to not be like tables anymore */
#dashboard table,
#dashboard thead,
#dashboard tbody,
#dashboard th,
#dashboard td,
#dashboard tr {
display: block;
}
/* Hide table headers (but not display: none;, for accessibility) */
#dashboard thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}
#dashboard tr { border: 1px solid #ccc; }
#dashboard td {
/* Behave like a "row" */
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 50%;
vertical-align: left;
text-align: left;
}
#dashboard 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;
/* Pull label from the data-label attribute */
content: attr(data-label);
}
}
/* Smartphones (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
body {
padding: 0;
margin: 0;
width: 320px; }
}
/* iPads (portrait and landscape) ----------- */
#media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
body {
width: 495px;
}
}
Now, my problem is, when I remove the table-header-rotated class from my table, and use a smaller screen, the collapsed table looks how I want it to look:
My problem appears to be this block of CSS:
.table-header-rotated td{
width: 10px;
vertical-align: middle;
text-align: center;
}
A fiddle demonstrating the problem:
http://jsfiddle.net/38vXF/
JSFiddle of it working (and non vertical headers)
http://jsfiddle.net/zzUqL/1/
Remeber, for both fiddles, the table layout changes based on the size of the window.
How can I completely remove this CSS class (table-header-rotated) when the page is smaller and the media CSS is rendering my table? At that point, obviously, I don't need the vertical headers.
This can be achieved by dynamically adding/removing the class on loading and also while resizing the screen using Javascript/jQuery. The below sample uses jQuery but it can be ported back to vanilla JavaScript if you prefer not to use jQuery.
Note: There is a difference between JS/jQuery width and CSS media queries width as mentioned in this post. Hence we would also have to use the work-around provided there.
Updated Demo
Width workaround code:
function viewport() {
var e = window, a = 'inner';
if (!('innerWidth' in window )) {
a = 'client';
e = document.documentElement || document.body;
}
return { width : e[ a+'Width' ] , height : e[ a+'Height' ] };
}
Sample 1 - jQuery:
$(document).ready(function () {
res = viewport();
if (res.width<900) $('#dashboard').removeClass('table-header-rotated');
$(window).on('resize', function () {
res = viewport();
console.log(res.width);
if (res.width < 900) $('#dashboard').removeClass('table-header-rotated');
else $('#dashboard').addClass('table-header-rotated');
});
});
Sample 2 - JavaScript:
window.onload = function (){
res = viewport();
if (res.width<900) {
var elm = document.getElementById('dashboard');
elm.className = elm.className.replace('table-header-rotated','');
}
window.onresize = function(){
elm = document.getElementById('dashboard');
res = viewport();
if (res.width<900) {
elm.className = elm.className.replace('table-header-rotated','');
}
else{
if(elm.className.indexOf('table-header-rotated') == -1)
elm.className += ' table-header-rotated';
}
};
};
You can simply use matchMedia to remove dynamicaly the class using javascript.
Like this:
if (matchMedia) {
var mq = window.matchMedia("(min-width: 900px)");
mq.addListener(WidthChange);
WidthChange(mq);
}
function WidthChange(mq) {
if (mq.matches) {
document.querySelector("table").classList.add("table-header-rotated");
}
else {
document.querySelector("table").classList.remove("table-header-rotated");
}
}
I have updated the JS Fiddle
There is no need to remove the class "table-header-rotated".
You can use this class, but you just need to reset it's styling, as below mentioned in media queries.
#media only screen and (max-width: 900px), (min-device-width: 768px) and (max-device-width: 1024px) {
.table-header-rotated td {
width: auto;
}
}
With your new requirements, I have changed the code in CSS only, with reference to your comment
This can also be achieved using JS, but its preferable to use CSS changes due to cross browser and different mobile devices.
Please refer to this JS Fiddle Updated
Below I have added the CSS style code.
.table-header-rotated th.row-header{
width: auto;
}
.table-header-rotated td{
width: 10px;
vertical-align: middle;
text-align: center;
}
#media
only screen and (min-width: 901px) {
.table-header-rotated th.rotate{
height: 80px;
width: 10px;
position: relative;
vertical-align: bottom;
padding: 0;
font-size: 12px;
line-height: 0.8;
}
.table-header-rotated th.rotate > div{
position: relative;
top: 0px;
height: 100%;
-ms-transform:skew(0deg,0deg);
-moz-transform:skew(0deg,0deg);
-webkit-transform:skew(0deg,0deg);
-o-transform:skew(0deg,0deg);
transform:skew(0deg,0deg);
overflow: hidden;
}
.table-header-rotated th.rotate span {
-ms-transform:skew(0deg,0deg) rotate(270deg);
-moz-transform:skew(0deg,0deg) rotate(270deg);
-webkit-transform:skew(0deg,0deg) rotate(270deg);
-o-transform:skew(0deg,0deg) rotate(270deg);
transform:skew(0deg,0deg) rotate(270deg);
position: absolute;
bottom: 30px;
left: -20px;
display: inline-block;
text-align: left;
text-align: center;
}
}
#media
only screen and (max-width: 900px),
(min-device-width: 768px) and (max-device-width: 1024px) {
/* Force table to not be like tables anymore */
/*
#dashboard table,
#dashboard thead,
#dashboard tbody,
#dashboard th,
#dashboard td,
#dashboard tr {
display: block;
}
*/
/* Hide table headers (but not display: none;, for accessibility) */
/* #dashboard thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}
*/
#dashboard tr { border: 1px solid #ccc; }
#dashboard td {
/* Behave like a "row" */
border: none;
border-bottom: 1px solid #eee;
position: relative;
/*padding-left: 50%;*/
vertical-align: left;
text-align: left;
}
#dashboard 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;
/* Pull label from the data-label attribute */
/*content: attr(data-label);*/
}
}
/* Smartphones (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
body {
padding: 0;
margin: 0;
width: 320px; }
}
/* iPads (portrait and landscape) ----------- */
#media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
body {
width: 495px;
}
}
If you have any other query or comment, then you can modify the fiddle and resend it again.
Regards D.