Cell Background in html - html

I want to provide backgroud color to table cell based on the value this is what so far i have done:
<style type="text/css">
.Scheduled {
background-color: lime;
}
.Completed {
background-color: lawngreen;
}
.Completed with error {
background-color:red ;
}
.Pending {
background-color: #ffbf00 ;
}
</style>
<td class="#item.Status" >
#Html.DisplayFor(modelItem => item.Status)
</td>
i want Complete with error cell to be in Red how can i do it? what i am doing wrong?
expected output:

The spaces in .Completed with error make it an invalid css class name.
If the class name was changed to .Completed-with-error, then it would become valid.
Let's fix this:
<td class="#item.Status.Trim().Replace(' ', '-')" >
#Html.DisplayFor(modelItem => item.Status)
</td>
Now change your CSS as well:
<style type="text/css">
.Scheduled {
background-color: lime;
}
.Completed {
background-color: lawngreen;
}
.Completed-with-error {
background-color:red ;
}
.Pending {
background-color: #ffbf00 ;
}
</style>
Voila!
Updated:
Add .Trim(), to clean up any trailing spaces.

So the first thing that I see is .Completed with error which is not a valid css class. You can't have spaces in a css class.
You'll need to amend the code so the class is .Completed-with-error. Both on the HTML and in the styles.

you may find this useful, notice i dont have border outside table
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid #888;
}
td{
color: #fff;
padding: 6px
}
.red{
background-color: red;
}
.green{
background-color: green;
}
.blue{
background-color: blue;
}
.pink{
background-color: pink;
}
<table border="1">
<tr>
<td class="red">fjdfk fdfdf</td>
<td class="green">fjdfk fdfdf</td>
</tr>
<tr>
<td class="blue">fjdfk fdfdf</td>
<td class="pink">fjdfk fdfdf</td>
</tr>
<tr>
<td class="red">fjdfk fdfdf</td>
<td class="green">fjdfk fdfdf</td>
</tr>
</table>

Related

Table CSS is not showing up at all

I have this simple html code. I am simply trying to format the colors but none of the CSS is actually formatting it.
I've tried changing the variables names, changing the table class to id and vice-versa.
<head>
<style>
.cool-table {
width: 500px;
border: 1px solid #000;
background-color: blue;
color: purple;
}
.cool-table tr:first-child td {
font-size: 30px;
background-color: red;
color: green;
}
#cell-style {
font-size: 8px;
text-align: left;
}
</style>
</head>
<html>
<body>
<table class="cool-table">
<tr>
<th id="cell-style">Fruit</th>
<th id="cell-style">Price</th>
</tr>
<tr>
<th id="cell-style">Apples</th>
<th id="cell-style">$10</th>
</tr>
<tr>
<th id="cell-style">Banana</th>
<th id="cell-style">$50</th>
</tr>
<tr>
<th id="cell-style">Mango</th>
<th id="cell-style">$20</th>
</tr>
</table>
</body>
</html>
It should show the entire table background as blue and the text should be purple. The first row's text should be large with a red background and green text. The rest of the cells should have a blue background with purple text and size 8px font.
Change your header cells to th and the normal cells to td. That way you do not need a id, class or tr:first-child to separate the header row from the rest. Note that if you use id, you should only use it on a single HTML tag. For multiple tags use class instead.
<html>
<head>
<style>
.cool-table {
width: 500px;
border: 1px solid #000;
background-color: blue;
color: purple;
}
.cool-table th { /* Changed to th, no need for tr:first-child */
font-size: 30px;
background-color: red;
color: green;
}
.cool-table td { /* Styling td-tags (table cells) */
font-size: 8px;
text-align: left;
}
</style>
</head>
<body>
<table class="cool-table">
<tr>
<th>Fruit</th> <!-- Keep as th -->
<th>Price</th>
</tr>
<tr>
<td>Apples</td> <!-- Changed to td -->
<td>$10</td>
</tr>
<tr>
<td>Banana</td>
<td>$50</td>
</tr>
<tr>
<td>Mango</td>
<td>$20</td>
</tr>
</table>
</body>
</html>
You can simply do it this way :
.cool-table {
width: 500px;
border: 1px solid #000;
background-color: blue;
color: purple;
}
.cool-table tr:first-child {
background-color: red;
color: green !important;
}
.cool-table tr:not(:first-child) {
font-size: 8px;
text-align: left;
}
<table class="cool-table">
<tr>
<th>Fruit</th>
<th>Price</th>
</tr>
<tr>
<th>Apples</th>
<th>$10</th>
</tr>
<tr>
<th>Banana</th>
<th>$50</th>
</tr>
<tr>
<th>Mango</th>
<th>$20</th>
</tr>
</table>
The first-child has a red background and green color, and everything that is NOT the first child gets a font-size of 8 and is aligned to the left.
There are several issues to look at.
style tag belongs in head tag which belongs in the html tag.
You can't use multiple ids in the same document - they're supposed to be unique. Try using a class like below.
the second css block doesn't do anything. Maybe you want to remove the td from the selector like below?
Several of your styles are not being applied because they override each other. Try making the selectors more specific to give them higher precedence.
You really want to understand the structure of html documents. You can verify them using the w3 validator
You can also learn more about CSS from Mozilla.
<!doctype html>
<html><head>
<style>
.cool-table {
width: 500px;
border: 1px solid #000;
background-color: blue;
color: purple;
}
.cool-table tr:first-child { /* removed 'td' */
font-size: 30px;
background-color: red;
color: green;
}
.cell-style { /* changed to class */
font-size: 8px;
text-align: left;
color: yellow;
}
</style>
</head><body>
<table class="cool-table">
<tr>
<th class="cell-style">Fruit</th>
<th class="cell-style">Price</th>
</tr>
<tr>
<th class="cell-style">Apples</th>
<th class="cell-style">$10</th>
</tr>
<tr>
<th class="cell-style">Banana</th>
<th class="cell-style">$50</th>
</tr>
<tr>
<th class="cell-style">Mango</th>
<th class="cell-style">$20</th>
</tr>
</table>
</body>
</html>

CSS - Apply conditional style to table row

I have a requirement where i have to apply conditional style for my table rows but the style never gets applied.
.test1 {
background-color: '#ffbf00';
}
.test2 {
background-color: '#92D050';
}
.test3 {
background-color: ' ';
}
<table id="testTable">
<tr styleClass="test1">
<td></td>
<td></td>
</tr>
</table>
But the styling will never be applied. something i am missing?
You have like 10 mistakes in this HTML code. Please learn HTML and CSS. Here is what you might want to achieve in a correct manner.
.test1 {
background-color: #ffbf00;
}
.test2 {
background-color: #92D050;
}
.test3 {
background-color: transparent;
}
<table id="testTable">
<tr class="test1">
<td>a</td>
<td>b</td>
</tr>
</table>
Do not put quotes on colors. It means class, not styleClass. ' ' is not a valid color value.
Use class for the definition in HTML and don't use quotes around the hex value.
.test1 {
background-color: #ffbf00;
}
.test2 {
background-color: #92D050;
}
.test3 {
background-color: ' ';
}
<table id="testTable">
<tr class="test1">
<td>Test</td>
<td>Test</td>
</tr>
</table>
target to td
.test1 td
{
background-color: #ffbf00;
}
.test2 td
{
background-color: #92D050;
}
.test3 td
{
background-color: none;
}
you need to add class name and styleClass="test1" not proper syntax
so update
styleClass="test1"
to
class="test1"
and one more thing your style code is not perfect
so also update
background-color: '#92D050';
to
background-color: #92D050;
cause this is also syntax error
other code is ok
.test1 {
background-color: #ffbf00;
}
.test2 {
background-color: #92D050;
}
.test3 {
background-color: rebeccapurple;
}
<table id="testTable">
<tr class="test1">
<td>test1</td>
<td>test1</td>
</tr>
<tr class="test2">
<td>test2</td>
<td>test3</td>
</tr>
<tr class="test3">
<td>test4</td>
<td>test5</td>
</tr>
</table>

How to make different elements under <tr> behave differently when hover over <tr> in CSS?

I want to make different changes to different elements under <tr>.
For example, I want to:
change background color of <tr> to black
change black <p> text color to white
change red <p> text color to blue
Here is the HTML code :
<tr class="trhl">
<td class = "td1">
<p class="p1">Hello</p>
<p class="p2">Enrique!</p>
</td>
<td class = "td2">
<p class="p3">Hi</p>
<p class="p4">Madonna</p>
</td>
</tr>
Following is the CSS code for the above :
p.p2 {
color:#c04848;
}
p.p3{
color:#c04848;
}
And I want to implement the changes on <tr> hover as following:
tr.trh1:hover {
background-color: black;
}
tr.trh1:hover .p1, .p4{
color: white;
}
tr.trh1:hover .p2, .p3{
color: blue;
}
How to do the above changes using CSS?
Seems like your big issue is that the class on the table row is .trhl and you have put .trh1 (tr-h-One).
p.p2 {
color: #c04848;
}
p.p3 {
color: #c04848;
}
.trhl:hover {
background-color: black;
}
.trhl:hover .p1,
.trhl:hover .p4 {
color: white;
}
.trhl:hover .p2,
.trhl:hover .p3 {
color: blue;
}
<table>
<tr class="trhl">
<td class="td1">
<p class="p1">Hello</p>
<p class="p2">Enrique!</p>
</td>
<td class="td2">
<p class="p3">CSS3 is the ticket</p>
<p class="p4">Madonna</p>
</td>
</tr>
</table>
.trh1:hover {
background-color: black;
}
.trh1:hover .p1,
.trh1:hover .p4 {
color: white;
}
.trh1:hover .p2,
.trh1:hover .p3 {
color: blue;
}
This will work for you
Why set tr.trh1:hover? You should set hover on the <p> element by class
tr .p1:hover,.p4:hover { color:white; }
tr .p2:hover, .p3:hover { color: blue; }

How to change color in first td in my table using class

I have this table CSS:
table.show-my-request-table {
border: 1px solid black;
margin-left:auto;
margin-right:auto;
border-collapse: collapse;
}
tr.show-my-request-table-header{
border: 1px solid black;
}
tr.show-my-request-table{
border: 1px solid black;
}
tr.show-my-request-table:hover{
background-color: #D3D3D3;
}
And table HTML:
<table class="show-my-request-table center">
<tr class="show-my-request-table-header">
<th>Date</th>
<th>Priority</th>
<th>Question</th>
</tr>
<tr >
<td class="show-my-request-table"> 11.8.2016 15:27:13
</td>
<td>
<img src="/resources/img/priority-flag/priority-LOW-icon.png" class="priority-icon">
</td>
<td>
</td>
</tr>
<tr >
<td class="show-my-request-table">
11.8.2016 14:45:41
</td>
<td>
<img src="/resources/img/priority-flag/priority-LOW-icon.png" class="priority-icon">
</td>
<td>
Jak se máš?
</td>
</tr>
</table>
I want set up a red background for the first td tag.
My problem is, that I don't know how to do it for only one table.
When I try:
td:first-child {
background-color: #ff0000;
}
it works for all tables.
I think that this code is good, but not working:
table.show-my-request-table > td:first-child {
background-color: #ff0000;
}
Why? How can I do this?
Try this:
table.show-my-request-table tr > td:first-child {
background-color: #ff0000;
}
You don't need to use > (immediate children selector) just put a space
Try this:
table.show-my-request-table td:first-child {
background-color: #ff0000;
}
table.show-my-request-table > td:first-child {
background-color: #ff0000;
}
This selector tries to target a td that is a direct child of the table element. As your own code shows:
<table class="show-my-request-table center">
<!-- snip -->
<tr >
<td class="show-my-request-table">
There is (and has to be) a tr element between them. But that's not all: the HTML parser will also silently insert a tbody element as a parent for the tr (unless you have explicitly included a <thead> or <tbody> tag). The <tbody> tag is optional in HTML, but the element is not, so the parser will simply add the element if the tag is missing.
The solution is to use the descendant selector:
table.show-my-request-table td:first-child {
background-color: #ff0000;
}
A keen observer will notice that the > combinator has been replaced by a (space) combinator.
You just need to target the element inside the table
Try this
table.show-my-request-table tr td:first-child {
background-color: #ff0000;
}
Here's a code pen http://codepen.io/anon/pen/LkrmqK
Cheers and happy coding.
table.show-my-request-table > td:nth-child(1) {
background: #25a3c2;
}
/* grab 2nd row, then color the first cell */
tr:nth-child(2) td:first-child {
background-color: #ff0000;
}
See it in action below
table.show-my-request-table {
border: 1px solid black;
margin-left: auto;
margin-right: auto;
border-collapse: collapse;
}
tr.show-my-request-table-header {
border: 1px solid black;
}
tr.show-my-request-table {
border: 1px solid black;
}
tr.show-my-request-table:hover {
background-color: #D3D3D3;
}
/* grab 2nd row, then color the first cell */
tr:nth-child(2) td:first-child {
background-color: #ff0000;
}
<table class="show-my-request-table center">
<tr class="show-my-request-table-header">
<th>Date</th>
<th>Priority</th>
<th>Question</th>
</tr>
<tr>
<td class="show-my-request-table">
11.8.2016 15:27:13
</td>
<td>
<img src="/resources/img/priority-flag/priority-LOW-icon.png" class="priority-icon">
</td>
<td>
</td>
</tr>
<tr>
<td class="show-my-request-table">
11.8.2016 14:45:41
</td>
<td>
<img src="/resources/img/priority-flag/priority-LOW-icon.png" class="priority-icon">
</td>
<td>
Jak se máš?
</td>
</tr>

Merge CSS declarations in one, possible?

I am trying to face a little problem i have encountered with.
I am designing a map in leaflet. What i want to achieve is add a HTML label above each marker in a leaflet map, like in this photoshoped example:
Ok the code for each label to display it is:
HTML
<table>
<tr>
<td class="red"></td>
<td class="yellow"></td>
<td class="orange"></td>
<td class="blue"></td>
</tr>
</table>
CSS
table{
border-collapse: collapse;
}
td{
padding:20px;
border:5px solid black;
}
.red{
background-color:#F15E66;
}
.yellow{
background-color:#FFDB64;
}
.orange{
background-color:#F58326;
}
.blue{
background-color:#85B1DE;
}
check https://jsfiddle.net/YameenYasin/cx1ka3Ly/ for the live code.
Ok the problem comes when leaflet labels only allow to add one unique CSS entry for each label. Example:
marker.bindLabel('<table><tr><td class="red"></td><td class="yellow"></td><td class="orange"></td><td class="blue"></td></tr></table>',
{noHide: true, className: "onlyone", offset: [-10, -40] });
Notice the className: "onlyone" property. I can only put one css there and the "class" tags added in the html code are not executed (they do not work).
My conclusion is that i need to add all the CSS code in only one classname called "onlyone" so that then i can do className: "onlyone".
Is this possible?
Regards,
It's possible to add a single class to the tr and then target the children by 'number' using nth-child as follows.
table {
border-collapse: collapse;
}
td {
padding: 20px;
border: 5px solid black;
}
.multi td:nth-child(1) {
background-color: #F15E66;
}
.multi td:nth-child(2) {
background-color: #FFDB64;
}
.multi td:nth-child(3) {
background-color: #F58326;
}
.multi td:nth-child(4) {
background-color: #85B1DE;
}
<table>
<tr class="multi">
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
Here you are:
https://jsfiddle.net/cx1ka3Ly/2/
table{
border-collapse: collapse;
}
td{
padding:20px;
border:5px solid black;
}
.onlyone:nth-child(1){
background-color:#F15E66;
}
.onlyone:nth-child(2){
background-color:#FFDB64;
}
.onlyone:nth-child(3){
background-color:#F58326;
}
.onlyone:nth-child(4){
background-color:#85B1DE;
}