How to add remove !important css with new css - css

bootstrap-next-table` and I am using this table in my project around different places, and I have to override a CSS class in this and I have given my own value.
Now the problem that I am currently facing, one component needs default CSS class. I have to override this class:
.table > thead {
display: none !important;
}
Now in one file I need this:
.table > thead {
display: block;
}
How can I achieve this?

Just use a selector with higher specificity and use !important in your style declaration. Here's a plain HTML/CSS snippet that you can adapt to Raect's className attribute syntax.
.table > thead {
display: none !important;
}
.table > thead.box {
display: block !important;
}
<table class="table">
<thead>
<tr>
<th>First</th>
<th>Second</th>
</tr>
</thead>
</table>
<table class="table">
<thead class="box">
<tr>
<th>Third</th>
<th>Fourth</th>
</tr>
</thead>
</table>

Related

Add hover on css class

I use bootstrap and datatable.
I created this class.
.nonCompliant{
background: #de5d5d;
}
It put on the tr of some row of the table.
Would like to put another color when nonCompliant class is displayed and hover event
tried
table#samplesTestsTable.dataTable tbody tr:hover > .nonCompliant{
background: #c11f1f;
}
and
tr:hover > .nonCompliant{
background: #c11f1f;
}
without good result.
Edit code of the row
<tr role="row" class="nonCompliant even"><td data-id="19475A" class="sorting_1" tabindex="0">190475A</td><td>2019-04-23</td></tr>
If the .nonCompliant class is modifying the tr itself, then
.nonCompliant:hover {
background: #c11f1f;
}
should work. Otherwise if .nonCompliant is on a direct child element of the tr, like a td,
tr:hover .nonCompliant {
background: #c11f1f;
}
or the child selector you're already using.
If you are applying the class directly to the hr tag, then you can just do this:
.someclass:hover{
background-color: yellow;
}
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td class="someclass">
Hello2
</td>
<td>Lastname</th>
<td>Age</th>
</tr>
</table>
else, if its a direct child you could to this:
tr:hover .nonCompliant {
background-color: yellow;
}

CSS N:th child with gaps

I've been trying to make a colored table with even rows with class item a different color than the odd ones.
Please, see fiddle: http://jsfiddle.net/x7XT5/
HTML:
<table>
<tr class="item">
<td>1</td>
</tr>
<tr>
<td>info</td>
</tr>
<tr class="item">
<td>2</td>
</tr>
<tr>
<td>info</td>
</tr>
<tr class="item">
<td>3</td>
</tr>
<tr>
<td>info</td>
</tr>
<tr class="item">
<td>4</td>
</tr>
<tr>
<td>info</td>
</tr>
</table>
​
CSS:
table tr.item:nth-child(2n)
{
background-color: yellow;
}
table tr.item:nth-child(2n+1)
{
background-color: red;
}
How to make it work in css?
UPD1
<tr> without class item must be on white background.
<tr class="item">'backgrounds must be red/yellow on even/odd positions.
​
table tr
{
background-color: yellow;
}
table tr.item:nth-child(2n+1)
{
background-color: red;
}
update: Here you go:
table tr {
background-color: white;
}
table tr.item:nth-child(n)
{
background-color: red;
}
table tr.item:nth-child(4n+1)
{
background-color: yellow;
}
Try this. No need to set the nth child.
http://jsfiddle.net/x7XT5/2/
You could also use odd & eeven keywords.
Hey remove the class and check in your tr and css file
and create easily
Live demo http://jsfiddle.net/x7XT5/1/
and
second method is Live Demo http://jsfiddle.net/x7XT5/3/
Updated Live demo http://jsfiddle.net/x7XT5/5/
First, I think, you should use :nth-of-type instead of :nth-child, but
unfortunately, :nth-of-type doesn't work with classes, so I dont know any pure CSS solution.
You can always use:
table tr.item:nth-of-type(4n+3)
{
background-color: yellow;
}
table tr.item:nth-of-type(4n+1)
{
background-color: red;
}
Works for this example.

When setting the back-ground color of a table row should I use the <tr> or the <td>'s in that row?

Say I have two classes row and altRow. What is the best practice when setting the backround-color of table rows? I only ask this because I was told that I shouldn't set such properties on the <tr> element. Thanks!!
You were told right, browsers generally don't deal ok with background set on tr elements.
however, you can set the classe to <tr> elements:
<tr class="row">
<td></td>
<td></td>
</tr>
<tr class="altRow">
<td></td>
<td></td>
</tr>
and then in css, use cascading face of CSS :)
.row td { background: yellow; }
.altRow td { background: blue; }
If you're looking for alternating table rows, CSS3 also allows you to do this:
tr:nth-child(odd) { background-color: #ddd; }
tr:nth-child(even) { background-color: #eee; }
With CCS3, you no longer need alternating class names in your HTML

Trouble with inheritance of CSS

I have some problem with my CSS Style. Currently, i have something like this:
table tbody tr:hover { background-color: #5A5A5A; color: #F9F9F9;}
After this, i found out that i need to have some table somewhere without the hover. So i go ahead and use this to overide:
.image-result tr:hover { background-color: #FFF; }
but unfortunately, this do nothing on the TR.
Can you suggest what should i do?
<div id="image-box">
<div>
<span>Search Image: </span>
<%= Html.TextBox("img-search") %>
<%= Html.Hidden("img-submitto", Url.Action("photopicker", "ajax"))%>
<button id="img-submit">Search</button>
</div>
<div class="image-result">
<table>
<tbody><tr>
<td>c</td>
<td>c</td>
</tr>
</tbody>
</table>
</div>
</div>
Basically the more specific the selector is, the higher the priority the browser will give in applying the rules for that style. Your first rule is more specific so has higher priority, which is why the style isn't being applied. You can do this:
.image-result tr:hover { background-color: #FFF !important; }
to increase the priority. That's not generally the recommended approach as it can (with some justification) be seen as hacking around the real problem. Probably a better solution is to make the new rule at least as specific as the other one:
table.image-result tbody tr:hover { background: #FFF; }
try to use
.image-result tr:hover td { background-color:#fff; }
Try this more specific one.
div.image-result table tbody tr:hover { background-color: #FFF;
color: theDefaultColor;}
Update: I've tried and it works on FF and IE8, not tested on others but should work. However, you'll have to add other styles especially "color" for the class, esle it'll take the original hover one.
Update 2: Modified based on the OP's code.
Here's the code I've used:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style type="text/css">
table tbody tr:hover { background-color: #5A5A5A; color: #F9F9F9;}
div.image-result table tbody tr:hover { background-color: #FFF; color: #000000;}
</style>
</head>
<body>
<div>
<table id="myTable" border="1">
<tbody>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</tbody>
</table>
</div>
<div class="image-result">
<table id="myTable2" border="1">
<tbody>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
Your second one will be recognised as a less specific selector than the first. Try this to make it more specific:
table.image-result tbody tr:hover { background-color: #FFF; }
(put image-result class wherever it is actually applied).

Conditional alternative table row styles

Is it possible to style alternate table rows without defining classes on alternate <tr> tags?
With the following table, can CSS define alternate row styles WITHOUT having to give the alternate rows the class "row1/row2"? row1 can be default, so row2 is the issue.
<style>
.altTable td { }
.altTable .row2 td { background-color: #EEE; }
</style>
<table class="altTable">
<thead><tr><td></td></tr></thead>
<tbody>
<tr><td></td></tr>
<tr class="row2"><td></td></tr>
<tr><td></td></tr>
<tr class="row2"><td></td></tr>
</tbody>
</table>
tr:nth-child(even) { background: #FFF; }
tr:nth-child(odd) { background: #EEE; }
Does not work in IE, but it's a purely presentational thing, the content will work fine anyway, so I don't think it's a huge issue -- depending on the % of regular IE users on your site.
Yes! You can do it with pure CSS and no classes on browsers that support the "+" selector of CSS:
.altTable tr td,
.altTable tr+tr+tr td,
.altTable tr+tr+tr+tr+tr td { background-color: #EEE; }
.altTable tr+tr td,
.altTable tr+tr+tr+tr td,
.altTable tr+tr+tr+tr+tr+tr td{ background-color: #fff; }
Probably not the best approach, but doable.
If you don't mind a little Javascript, jQuery gives it to you much concisely:
$('.altTable tr:odd').addClass('odd');
Give a class of row2 on tbody and then style your alternate rows with class row1. Other rows will inherit the class row2 from the tbody.
<style>
.row1 { color: red }
.row2 { color: blue }
</style>
<table class="altTable">
<thead><tr><td></td></tr></thead>
<tbody class="row2">
<tr class="row1"><td>row 1</td></tr>
<tr><td>row 2</td></tr>
<tr class="row1"><td>row 1</td></tr>
<tr><td>row 2</td></tr>
</tbody>
</table>

Resources