I have a table with n rows. On these n rows I need to set some CSS style, following this rules:
the CSS should apply only on odd rows
the CSS should NOT apply on the first and the last row (or on those rows with .excludeme class)
the CSS should apply only on visible rows
HTML
<table>
<tr class="excludeme"><td>first</td></tr>
<tr><td>Hello</td></tr>
<tr><td>Hello</td></tr>
<tr class="showhide"><td>show/hide</td></tr>
<tr><td>Hello</td></tr>
<tr><td>Hello</td></tr>
<tr><td>Hello</td></tr>
<tr><td>Hello</td></tr>
<tr class="showhide"><td>show/hide</td></tr>
<tr class="showhide"><td>show/hide</td></tr>
<tr><td>Hello</td></tr>
<tr class="excludeme"><td>last</td></tr>
</table>
CSS
.showhide{
display: none;
}
table tr:nth-child(odd):not(.excludeme){
background: orange;
}
JS
//--- #test is a button
$("#test").on("click", function(){
$(".showhide").toggle();
});
you can find a fiddle HERE
As you can see, when some rows are hidde, the even/odd colouring is not respected. I tried this selector
table tr:nth-child(odd):not(.excludeme):visible{
....
}
but is not working.. Is it possible to accomplish this only using CSS?
Thank you
$(document).ready(function(){
$("#test").on("click", function(){
$(".showhide").toggle();
if($(element).is(":visible")){
// color odd rows
}
});
});
You have to verify the visibility of elements before coloring them. Only color the visible odd elements, not all elements in DOM (visible or not).
I hope that helps, have a good day :)
you can use following CSS(use of gradient CSS) without changing any other code:
* {
margin: 0;
padding: 0;
}
table {
padding: 0;
margin: 0;
border: none;
width: 100%;
background-image:
repeating-linear-gradient(white, white 1.2em, hotpink 1.2em, hotpink 2.4em);
border-collapse: collapse;
}
td,tr {
border: none;
padding: 0 5px;
line-height: 1.2em;
margin: 0;
border-collapse: collapse;
}
.showhide {
display: none;
}
.excludeme {
background: white;
}
Related
I have a table, with a header row and numerous data rows. I have a checkbox in the first column. In the th cell, I would like to add a top and bottom margin, on the td cells, I don't want this.
I have LESS(css) that is the same for both the th and td elements with class .col-checkbox, as well as a label element in both cells with shared css. I'd like to add the margin top/bottom to the label if it is in the th cell.
.html file
<table>
<tr>
<th class="col-checkbox">
<div>Column Label</div>
<label class="custom-checkbox">
<input type="checkbox" />
<span class="checkbox"></span>
</label>
</th>
<th>
Unimportant, there are more columns as well
</th>
</tr>
<tr>
<td class="col-checkbox">
<label class="custom-checkbox">
<input type="checkbox" />
<span class="checkbox"></span>
</label>
</td>
<td>
Unimportant, there are more columns as well
</td>
</tr>
</table>
.less file
.col-checkbox {
width: 30px;
// more css here
label.custom-checkbox {
height: 24px;
// more css here
// I know I can do the following, but I'd like to not have to add
// more classes if I can someone make this dependent on whether it
// is in the th or td element
//&.header {
// margin: 6px auto;
//}
//
//&.data {
// margin: 0 auto;
//}
}
}
I know I can make it work the way I have it above, but I was curious if I can do it just by referencing the td and th elements without duplicating the other css. I don't think so, but I figured I'd ask anyway.
It looks like you are already familiar with the & operator. Well it doesn't have to go before the selector. Instead, you can use it after the selector, such as th&, to get what you want.
So this:
.col-checkbox {
width: 30px;
// more css here
label.custom-checkbox {
height: 24px;
// more css here
}
th& {
margin: 10px 0;
}
}
Outputs this:
.col-checkbox {
width: 30px;
}
.col-checkbox label.custom-checkbox {
height: 24px;
}
th.col-checkbox {
margin: 10px 0;
}
However, a word of caution that this pattern may not work as you expect if you have more than one level of nesting.
Consider this code:
.col-checkbox {
width: 30px;
// more css here
label.custom-checkbox {
height: 24px;
// more css here
.checkbox& {
color: navy;
}
}
}
You may expect to get this out of it:
.col-checkbox {
width: 30px;
}
.col-checkbox label.custom-checkbox {
height: 24px;
}
.col-checkbox label.custom-checkbox.checkbox {
color: navy;
}
But in reality you'll get this:
.col-checkbox {
width: 30px;
}
.col-checkbox label.custom-checkbox {
height: 24px;
}
.checkbox.col-checkbox label.custom-checkbox {
color: navy;
}
I am trying to create a table with the following two functionalities:
Some kind of 'bring-to-front with shadow' or '3d effect' that I see in some websites and find very pleasant.
Mouse cursor should have the 'hand' icon indicating the row is clickable.
I need these functionalities to appear whenever the user hover over the rows of the table. I tried using Bootstrap 4's table-hover class's functionality but couldn't achieve any of the two functionalities.
For the first functionality, I have an idea of adding a class with shadow to the <tr> being hovered. Don't know however, if this is the best approach. Is there some already defined class that could achieve such behavior?
And for the second functionality, I have no idea. Any suggestions?
Here's my code:
<div class="container">
<div class="table-wrapper">
<table class="table">
<thead id="thead_st" class="thead">
<tr>
<th class="thead-row" scope="col"></th>
<th class="thead-row" scope="col">1</th>
<th class="thead-row" scope="col">2</th>
<th class="thead-row" scope="col">3</th>
<th class="thead-row" scope="col">4</th>
<th class="thead-row" scope="col"></th>
</tr>
</thead>
<tbody>
<tr class="trow">
<th scope="row"></th>
<td class="score">4.7</td>
<td>Bla bla</td>
<td>2</td>
</tr>
</tbody>
</table>
</div>
</div>
CSS:
.table-wrapper {
border-radius: 8px;
overflow: hidden;
}
.container {
margin-top: 50px;
margin-bottom: 50px;
background: white;
border-radius: 8px;
padding: 0px;
}
body {
background: #ECEEF1;
font-family: 'Roboto', sans-serif;
color: #2C3A56;
}
tr {
font-size: 16.5px;
line-height: 50px;
padding: 0px;
font-weight: 100;
}
td, th {
display: table-cell;
text-align: center;
}
#thead_st {
background-color: #F6CE52;
border: 3px solid #F6CE52;
color: white;
}
.thead-row {
line-height: 20px;
font-weight: 100;
}
.trow:hover {
cursor:pointer; //set the cursor to pointer (hand)
background-color: blue; //sets hovered row's background color to blue
box-shadow: 5px 10px #888888;
}
Please Check Below Fiddle. Your two requirement completed.
Some kind of 'bring-to-front with shadow' or '3d effect' that I see in some websites and find very pleasant.
Mouse cursor should have the 'hand' icon indicating the row is clickable.
Fiddle
.trow
{
transition: transform .2s;
}
.trow:hover {
cursor:pointer;
transform: scale(1.03);
background:#ccc;
color:#fff;
}
In css, assuming your <tr> has .row class :
.row:hover{
cursor:pointer; //set the cursor to pointer (hand)
background-color:blue; //sets hovered row's background color to blue
box-shadow: 5px 10px #888888; //this is a box shadowing effect that you can tweak at your choice.
}
If further more you just want to make that "3D" effect you can play with width and height properties of the <tr> element to make it bigger over the others on hover event.
When I used to play with 3D effects I usually implemented 2D Transforms to adjust positioning with translation properties.
I have a table which has a peculiar style issue when I insert an input element in the header. It does not center the input exactly; instead it is off by 2 pixels on the right side. I have some Twitter Bootstrap styles applied to the table, but I can't find one that is causing the issue so I don't think that has anything to do with it. Here is the markup:
<thead>
<tr>
<th scope="col" style="width: 5%;">
ID
<input type="text" value="" class="grid-filter" id="id-filter">
</th>
...
Here is a picture of the issue (zoomed in considerably):
Here are the styles applied:
th {
width: 15%;
a { display: block; }
input {
height: 15px;
line-height: 15px;
margin: 0;
padding: 5px 0;
width: 100%;
}
}
In the image above, I'm using Firebug and have focused on the "ID" anchor. As you can see, the anchor is correctly centered in the th, but the input box has an extra 2 pixels on the right for some reason. Why is this? The weird thing is that this does NOT affect select elements, only input elements.
Update: When I set the border and outline, Bootstrap's focus glow also has a border. Not sure which style to override...
edit: looks like the problem is your width attribute. Check this JSFiddle
HTML:
<table>
<thead>
<tr>
<th scope="col" style="width: 5%;">
ID
<input type="text" value="" class="grid-filter" id="id-filter"/>
</th>
</tr>
</thead>
</table>
CSS:
a, input {
padding: 0;
margin 0;
}
a {
display: block;
background: red;
}
input {
height: 15px;
line-height: 15px;
/* width: 100%; */
}
table { width: 3em }
The width attribute on the input defaults to auto, which does what you want in this case.
I need to change td's text color based on odd/even, like this http://jsfiddle.net/N9gEG/
Actually I have a class which do this, but, I want to do from css
<table>
<tr>
<td>RED</td>
<td class="foo">BLUE</td>
<td>RED</td>
<td class="foo">BLUE</td>
</tr>
</table>
For tr odd/even I have the follow code: table tr:nth-child(even).
td {
color: blue;
}
td:nth-child(even) {
color: red;
}
This works because of rule specificity. The more specific CSS rule wins. td without anything else is less specific than td:nth-child(even), so it applies to the odd <td>s automatically.
If your jsFiddle correctly illustrates what you want, you can simply use the :nth-child selectors on the tds rather than the tr:
td { color: blue; }
td:nth-child(odd) { color: red; }
http://jsfiddle.net/N9gEG/2/
Given my limited understanding of your question, I'd suggest:
td:nth-child(even) {
color: blue;
}
td:nth-child(odd) {
color: red;
}
JS Fiddle demo.
Use jquery
.hover {background-color:green !important;}
.odd {background-color:blue}
$("#MyTable tr:odd").addClass("odd");
$("#MyTable tr").mouseover(function() { $(this).addClass("hover"); });
$("#MyTable tr").mouseout(function() { $(this).removeClass("hover"); });
I created a table without borders styling and I want it to underline a row on hover. However, I am getting pretty odd behaviour. When I move mouse over from upside down, nothing happens. In opposite direction, all touched rows get underlined and stay that way until I move mouse over in some other direction. I am pretty confused by this. I wanted to stay out of jquery for simplicity, but with jquery I get the same result. Here is the code..
<div class="information" >
<table id="summary" >
<%
foreach (KeyValuePair<long, float> pair in sums)
{ %>
<tr>
<td class="left" >Automat id: <%= pair.Key%></td>
<td class="right" ><%= pair.Value%></td>
</tr>
<% } %>
</table>
</div>
And the css applied to this div:
table
{
border-collapse: collapse;
border-spacing: 0;
text-align: center;
margin-top:.5em;
}
div.information
{
margin:1em 0;
padding:1em;
font-weight:bold;
text-align: center;
color:#C80;
background-color:#FF9;
border:1px solid #C80;
}
#summary
{
width: 715px;
margin-bottom: 5px;
}
.left
{
text-align: left;
}
.right
{
text-align: right;
}
And the faulty part:
#summary tr:hover
{
border-bottom: dotted 1px gray;
}
Anyone sees an error? Another way? And sorry for the long post.
I know this is old, but it showed up in a Google Search of mine. Browser support for styling tr is pathetic at best. Your styles need to be on the tds, like:
#summary tr:hover td {
border-bottom:solid 1px #FF9;
}
Try using JavaScript onmouseover and onmouseout events. In this events just apply and un-apply your css class.
This looks like a bug in chrome. It works well on Firefox, and not at all in IE.
(IE doesn't support :hover, border-spacing, border for collapsed <tr>s, it goes on and on...)
I've found a simple workaround for Chrome, however - simply add a bottom border for all <tr>s:
#summary tr {
border-bottom:solid 1px #FF9;
}
This will also keep your rows from changing heights and jiggle.
Ok, there seems to be a bug in the way the :hover pseudoselector is being applied to "tr". Change selector to "#summary td:hover {}". It should work.
One note to add, that does not seem too widely known, unlike every other browser tested, chrome redraws the ENTIRE table when applying styles on tr:hover. This may have been fixed in newer versions of chrome
This is VERY critical when you have a large table, chrome lags very badly.
Also, I would highly recommend having a border of the same width set on the TDs before hover, and simply match the BG color, vertical shifts like that are horrible UX. I tried reducing the padding inside the TDs by 1px to account for the border, but had some weird results (yet another reason to hate tables)
This is the way to define Script using JQuery to change row style (you should define your css).
CSS:
.hovercs {
cursor: pointer;
background-color: #70C9C4;
}
JavaScript:
$(function () {
$(document).on({
mouseenter: function () {
$(this).addClass('hovercs');
},
mouseleave: function () {
$(this).removeClass('hovercs');
}
}, 'tbody tr');
});
$(function () {
$(document).on({
mouseenter: function () {
$(this).addClass('hovercs');
},
mouseleave: function () {
$(this).removeClass('hovercs');
}
}, 'tbody tr');
});
.hovercs {
cursor: pointer;
background-color: #70C9C4;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<style>
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
</style>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>City</th>
<th>Country</th>
</tr>
<tr>
<td>Vipin Yadav</td>
<td>Frankfrut</td>
<td>Germany</td>
</tr>
<tr>
<td>Ramesh</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Reetika</td>
<td>Melbourne</td>
<td>Austria</td>
</tr>
</table>
</body>
</html>
$(function () {
$(document).on({
mouseenter: function () {
$(this).addClass('hovercs');
},
mouseleave: function () {
$(this).removeClass('hovercs');
}
}, 'tbody tr');
});