Give on different cells colors - html

How can I have different colors on each cell. What I did is only to make the whole red, just to the first 3 cells only yellow blue and red. How can it be this? I should refer to specific <td>? I see this question, but it wasn't exactly what I was searching.
body {
background: #000;
}
#wrap {
margin: 0 auto;
/* margin 0 auto will center that box in your document */
width: 780px;
/*size of your box*/
background: #000;
text-align: center;
/* everything will be written in that box will be centered horizontaly*/
}
td:hover {
background-color: #ff0000;
color: #000000;
}
<div id="wrap">
<table width="780">
<tr>
<td align="center">
<table border=1>
<tbody>
<!-- Results table headers -->
<tr>
<th>Messages Per Month</th>
<th>1 Month Pricing</th>
<th>3 Month Pricing</th>
<th>12 Month Pricing</th>
</tr>
<tr>
<td>500</td>
<td>$14.95/Month</td>
<td>$12.95/Month</td>
<td>$9.95/Month</td>
</tr>
<tr>
<td>1,000</td>
<td>$24.95/Month</td>
<td>$20.95/Month</td>
<td>$17.95/Month</td>
</tr>
<tr>
<td>1,500</td>
<td>$37.95/Month</td>
<td>$31.95/Month</td>
<td>$26.95/Month</td>
</tr>
<tr>
<td>2,000</td>
<td>$49.95/Month</td>
<td>$41.95/Month</td>
<td>$35.95/Month</td>
</tr>
<tr>
<td>2,500</td>
<td>$62.95/Month</td>
<td>$52.95/Month</td>
<td>$44.95/Month</td>
</tr>
<tr>
<td>5,000</td>
<td>$119.95/Month</td>
<td>Not Available</td>
<td>Not Available</td>
</tr>
<tr>
<td>7,500</td>
<td>$179.95/Month</td>
<td>Not Available</td>
<td>Not Available</td>
</tr>
<tr>
<td>10,000</td>
<td>$219.95/Month</td>
<td>Not Available</td>
<td>Not Available</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</div>

Try using nth-child on the elements, here is a quick reference to all kinds of selections.
td:nth-child(odd) {
color: green;
}
td:nth-child(even) {
color: red;
}
td {
border: 1px solid gray;
}
<table>
<tr>
<td>2,500</td>
<td>$62.95/Month</td>
<td>$41.95/Month</td>
<td>$35.95/Month</td>
</tr>
<tr>
<td>1,500</td>
<td>$52.95/Month</td>
<td>$31.95/Month</td>
<td>$25.95/Month</td>
</tr>
</table>

You can use nth-child css psuedoselectors:
td:nth-child(1) {
color: yellow;
background-color: #AAA;
}
td:nth-child(2) {
color: red;
}
td:nth-child(3) {
color: blue;
}
<table>
<tr>
<td>Yellow</td>
<td>Red</td>
<td>Blue</td>
<td>Normal</td>
</tr>
</table>

First let's create a simplify version of your table.
table tr td{
border:2px solid black;
width:70px;height:30px;
text-align: center;
}
table{
border-collapse: collapse;
}
table:hover tr td:nth-child(1){
background: yellow;
}
table:hover tr td:nth-child(2){
background:blue;
}
table:hover tr td:nth-child(3){
background:red;
}
<table>
<tr>
<td>one</td>
<td>two</td>
<td>three</td>
<td>four</td>
</tr>
<tr>
<td>five</td>
<td>six</td>
<td>seven</td>
<td>eight</td>
</tr>
<tr>
<td>nine</td>
<td>ten</td>
<td>eleven</td>
<td>twelve</td>
</tr>
</table>
Let me give you a bit of an explanation.. the table:hover tr td:nth-child(1) part, first the table:hover part, when we hover to the whole table, we want to target all the tr, inside the table, then inside the tr, we want to only select the first td ":nth-child(1)" of every tr, so this will only select and change the background color of the one,five and nine td (which is the first column of the table) color to yellow if we hover the mouse to the whole body of the table.
PS: For me, I prepare to do this on JavaScript.

Related

How to separate rows in a table with space between with HTML and Bootstrap 5

I currently have a table with my content in but it looks like
I would like it to have space between each row to make them distinctively separate and look something like this
I have tried using padding on my td elements and border spacing on my tr elements. What would be the easiest way to do this within bootstrap 5? Below is my HTML and CSS
.icon-green {
color: green;
}
table {
border-collapse: separate;
border-spacing: 0 15px;
}
th,
td {
text-align: center;
vertical-align: middle;
padding: 5px;
}
<table>
<tr>
<th>Employee ID</th>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
</tr>
<tr>
<td class="td">10001</td>
<td>Tom</td>
<td>M</td>
<td>30</td>
</tr>
<tr>
<td class="td">10002</td>
<td>Sally</td>
<td>F</td>
<td>28</td>
</tr>
<tr>
<td class="td">10003</td>
<td>Emma</td>
<td>F</td>
<td>24</td>
</tr>
</table>
I think you already fixed most of your own problem except that because everything is white, it's hard to see that there is space between the rows. Added a slight box shadow on the rows. I'm not sure how you are using bootstrap here though, but I assume it's similar if you want to override bootstrap styles
table {
border-collapse: separate;
border-spacing: 0px 15px;
}
tr{
/* only added this line */
box-shadow:0px 1px 2px 3px rgba(0, 0, 0, 0.142);
}
th,
td {
width:100%;
background-color:white;
text-align: center;
vertical-align: middle;
padding: 5px;
}
<table>
<tr>
<th>Employee ID</th>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
</tr>
<tr>
<td class="td">10001</td>
<td>Tom</td>
<td>M</td>
<td>30</td>
</tr>
<tr>
<td class="td">10002</td>
<td>Sally</td>
<td>F</td>
<td>28</td>
</tr>
<tr>
<td class="td">10003</td>
<td>Emma</td>
<td>F</td>
<td>24</td>
</tr>
</table>
There are limited things you can do with tr elements. You can use box-shadow and outline on them as below. Note, I've had to add a class to your table so I can increase the specificity of the rule that causes the table not to collapse.
.icon-green {
color: green;
}
/* note the added class, if you use just 'table' as the selector bootstrap will override it and collapse your table */
table.mytable {
border-collapse: separate;
border-spacing: 0 15px;
}
th,
td {
text-align: center;
vertical-align: middle;
padding: 10px;
}
tr {
border-radius: 0.25rem;
outline: 1px solid #cccccc;
box-shadow: 10px 10px 10px 0px #d0d0d0;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous" defer></script>
<table class='mytable'>
<tr>
<th>Employee ID</th>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
</tr>
<tr>
<td class="td">10001</td>
<td>Tom</td>
<td>M</td>
<td>30</td>
</tr>
<tr>
<td class="td">10002</td>
<td>Sally</td>
<td>F</td>
<td>28</td>
</tr>
<tr>
<td class="td">10003</td>
<td>Emma</td>
<td>F</td>
<td>24</td>
</tr>
</table>

CSS not removing overlapping / thickened borders

Line 5 in the CSS removed all of the overlapping or thickened lines but through the middle, the line is still thick and at the start of the 2nd column as you can see in the picture but it isn't in the tutorial I'm watching. Why?
/* table styles */
table {
border-spacing: 0px;
border-collapse: collapse;
width: 100%;
}
table th {
text-align: left;
background-color: darkseagreen;
color: white;
text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.4);
}
table th,
table td {
border: 1px solid black;
}
/* end table styles */
<table>
<thead>
<tr>
<th>Item Name</th>
<th>Quantity</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Oranges</td>
<td>5</td>
<td>$3.00</td>
</tr>
<tr>
<td>Celery</td>
<td>10</td>
<td>$5.00</td>
</tr>
<tr>
<td>Garlic</td>
<td>15</td>
<td>$13.00</td>
</tr>
<tr>
<td>Marbles</td>
<td>12</td>
<td>$23.00</td>
</tr>
<tr>
<td>VERY LONG ITEM THAT MAKES THE HEADER GO BRR</td>
<td>4</td>
<td>$40.00</td>
</tr>
</tbody>
</table>

background-color css property not working in table

Here is html and CSS code:
table,th,td{
border:1px solid blue;
border-collapse:collapse;
}
td{
text-align:center;
}
td{padding:10px;
color:#cc7722;
}
table{
border-spacing:5px;
background-color:yellowgreen;
font-weight:bold;
width:100%
}
#hello{
color:red;
}
.hi{
background-color:blue;
}
<table>
<caption id="hello">Employee Data</caption>
<tr>
<div class="hi">
<th>Employee Name</th>
<th>
Department
</th>
<th>Salary</th>
</div>
</tr>
<tr>
<td>Vaibhav</td>
<td>CSE</td>
<td>10000</td>
</tr>
</table>
Output
Here background color blue for class hi not shown
so what can be the reason
and what is the possible solution for this
If you use div as anything other than a cell value, you'll get misbehaving browsers.
Wrap your header row definition around the thead tag and style that instead. Don't forget to then wrap the body around tbody.
table,
th,
td {
border: 1px solid blue;
border-collapse: collapse;
}
td {
text-align: center;
}
td {
padding: 10px;
color: #cc7722;
}
table {
border-spacing: 5px;
background-color: yellowgreen;
font-weight: bold;
width: 100%
}
#hello {
color: red;
}
.hi {
background-color: blue;
}
<table>
<caption id="hello">Employee Data</caption>
<thead class="hi">
<tr>
<th>Employee Name</th>
<th>
Department
</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>Vaibhav</td>
<td>CSE</td>
<td>10000</td>
</tr>
</tbody>
</table>
you can't use a div within a table for styling. just apply the class to the tr itself
html
<table>
<caption id="hello">Employee Data</caption>
<tr class="hi">
<th>Employee Name</th>
<th>Department</th>
<th>Salary</th>
</tr>
<tr>
<td>Vaibhav</td>
<td>CSE</td>
<td>10000</td>
</tr>
</table>
Your table shouldn't have a div inside, instead you have to do like this:
<tr class="hi">
add div inside th,td it'll work not outside
table,th,td{
border:1px solid blue;
border-collapse:collapse;
}
td{
text-align:center;
}
td{padding:10px;
color:#cc7722;
}
table{
border-spacing:5px;
background-color:yellowgreen;
font-weight:bold;
width:100%
}
#hello{
color:red;
}
.hi{
background-color:blue;
}
<table>
<caption id="hello">Employee Data</caption>
<tr>
<th>Employee Name</th>
<th>
Department
</th>
<th>Salary</th>
</div>
</tr>
<tr>
<td><div class="hi">Vaibhav</div></td>
<td>CSE</td>
<td>10000</td>
</tr>
</table>
I think what you want to do is changing the <tr> background, which you can easily accomplish by removing your <div> and giving the class to your <tr> tag. As the following:
<table>
<caption id="hello">Employee Data</caption>
<tr class="hi">
<th>Employee Name</th>
<th>Department</th>
<th>Salary</th>
</tr>
<tr>
<td>Vaibhav</td>
<td>CSE</td>
<td>10000</td>
</tr>
</table>
And your CSS is just the same.
table,th,td{
border:1px solid blue;
border-collapse:collapse;
}
td{
text-align:center;
}
td{padding:10px;
color:#cc7722;
}
table{
border-spacing:5px;
background-color:yellowgreen;
font-weight:bold;
width:100%
}
#hello{
color:red;
}
.hi{
background-color:blue;
}
Does moving the class to the th accomplish what you are wanting?
table,th,td{
border:1px solid blue;
border-collapse:collapse;
}
td{
text-align:center;
}
td{padding:10px;
color:#cc7722;
}
table{
border-spacing:5px;
background-color:yellowgreen;
font-weight:bold;
width:100%
}
#hello{
color:red;
}
.hi{
background-color:blue;
}
<table>
<caption id="hello">Employee Data</caption>
<tr class="hi">
<th >Employee Name</th>
<th>
Department
</th>
<th>Salary</th>
</tr>
<tr>
<td>Vaibhav</td>
<td>CSE</td>
<td>10000</td>
</tr>
</table>
If possible to change html then You can use combination of thead, tbody to set different colors to table head and body.
Example
<style>
thead {color:green;}
tbody {background:blue;}
tfoot {color:red;}
table, th, td {
border: 1px solid black;
}
</style>
<table>
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
</thead>
<tfoot>
<tr>
<td>Sum</td>
<td>$180</td>
</tr>
</tfoot>
<tbody>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</tbody>
</table>
All tr in tbody will have background blue.
Otherwise you have to use styling for tr, th separately

Table with transparent border only on tbody

table
{
background-color: lime;
border-collapse: collapse;
}
tr
{
border-width: 0 0 1px 0;
border-style: solid;
}
tr:last-child
{
border: none; // so the last child from thead and tbody dont have border
}
<table>
<thead>
<th>Rank</th>
<th>Player</th>
<th>Pts</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>player1</td>
<td>50</td>
</tr>
<tr>
<td>2</td>
<td>player2</td>
<td>40</td>
</tr>
<tr>
<td>3</td>
<td>player3</td>
<td>30</td>
</tr>
<tr>
<td>4</td>
<td>player4</td>
<td>40</td>
</tr>
</tbody>
</table>
Now, I want a transparent border between the rows, but only the rows within tbody, but not between thead and tbody.
First, I tried
table {
border-collapse: collapse;
tr {
border-width: 0 0 1px 0;
border-style: solid;
}
tr:last-child {
border: none; // so the last child from thead and tbody dont have border
}
}
In the case, I get the border on the element I wanted, but it's black and not transparent.
Then I tried with border-spacing:
table {
border-spacing: 0 1px;
tr:last-child {
border: none;
border-spacing: none; //those two don't seem to work
}
}
Now I have transparent borders, but there are borders before and after the thead as well, which I can't eliminate.
So, I have now either:
1. border only in tbody but not between thead and first data row(good), but the borders are not transparent(bad)
or
2. transparent border(good), but unwanted border between thead and first data row(bad).
Is there a way to combine this so I have transparent border, but NOT between thead and first data row?
edit:
I want the border to be full transparent, but as soon as I set the border-color with rgba(0,0,0,0), the border "disappears". Ok, it doesn't really disappeares, but take the background-color from td(the lightgrey color, which is a rgba value as well) and I have no idea why.
use border-color: rgba(0,0,0,.5);
<!DOCTYPE html>
<html>
<head>
<style>
table
{
background:yellow;
border-spacing: 0;
}
tbody tr:not(:last-of-type) td
{
border-width: 0 0 5px 0;
border-style: solid;
border-color:black;
border-color: rgba(0,0,0,.5);
}
</style>
</head>
<body>
<table>
<thead>
<th>Rank</th>
<th>Player</th>
<th>Pts</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>player1</td>
<td>50</td>
</tr>
<tr>
<td>2</td>
<td>player2</td>
<td>40</td>
</tr>
<tr>
<td>3</td>
<td>player3</td>
<td>30</td>
</tr>
<tr>
<td>4</td>
<td>player4</td>
<td>40</td>
</tr>
</body>
</html>
The problem is that your table has a background color. If you make the border fully transparent, it is transparent, but it won't show you what's behind the table. You're seeing the table's background-color. Also, since you're using a table, any reason you don't just use border-spacing instead?
But rgba(0,0,0,0) for the border color could work as well I suppose.
table
{
border-spacing: 0 20px;
}
tr
{
background-color: lime;
}
tr:last-child
{
border: none; // so the last child from thead and tbody dont have border
}
<table>
<thead>
<th>Rank</th>
<th>Player</th>
<th>Pts</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>player1</td>
<td>50</td>
</tr>
<tr>
<td>2</td>
<td>player2</td>
<td>40</td>
</tr>
<tr>
<td>3</td>
<td>player3</td>
<td>30</td>
</tr>
<tr>
<td>4</td>
<td>player4</td>
<td>40</td>
</tr>
</tbody>
</table>
Edit:
A way to achieve this is to use an :after on the tr of the thead, so you can "simulate" a border.
thead {
background: lime;
tr:after {
background: lime;
content: "";
height: 2px;
position: absolute;
left: 56px;
top: 77px;
width: calc(100% - 111px);
}
}
With this, you can place the "border" on top of the space between thead and the tbody. Obviously, this may not be the best solution, but I couldn't find another way.
Example: https://jsfiddle.net/qapmqfau/6/
Added an empty table row after each row
<!DOCTYPE html>
<html>
<head>
<style>
table
{
border-spacing: 0;
border-collapse:collaspe;
}
th
{
background:pink;
}
tr:nth-child(even)
{
background:transparent;
height:2px;
}
tr:nth-child(odd)
{
background:green;
}
</style>
</head>
<body>
<table>
<thead>
<th>Rank</th>
<th>Player</th>
<th>Pts</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>player1</td>
<td>50</td>
</tr>
<tr></tr>
<tr>
<td>2</td>
<td>player2</td>
<td>40</td>
</tr>
<tr></tr>
<tr>
<td>3</td>
<td>player3</td>
<td>30</td>
</tr>
<tr></tr>
<tr>
<td>4</td>
<td>player4</td>
<td>40</td>
</tr>
</body>
</html>

Set same width two tables

I have two tables that show data from database.
Now I set 1st table for headlines and 2nd table for the data.
I set like this
<table class="t_status">
<td>No</td>
<td>Name</td>
<td>Address</td>
</table>
In table #2
<table class="t_status">
<td>1</td>
<td>Michael</td>
<td>California</td>
<tr>
<td>2</td>
<td>Greg</td>
<td>LA</td>
Now facing the problem when data display, table 1 and table 2 set different width.
This is the CSS
table
{
empty-cells: show;
border-collapse: collapse;
width: 100%;
}
.t_status
{
border-collapse: collapse;
background: #fff;
border: 1px solid #d9d9d9;-webkit-border-radius:3px;-moz-border-radius:3px;border-radius:3px;
width: 100%;
margin-top: -1px;
}
.t_status td, th
{
border-top: 1px solid #c0c0c0;
border-bottom: 1px solid #c0c0c0;
border-left: 1px solid #c0c0c0;
border-right: 1px solid #c0c0c0;
padding: 10px;
font-size: 40pt;
font-weight: bold;
}
.t_status td
{
color: #fff;
background-color: #000;
}
.t_status th
{
font-size: 40pt;
color: #fff;
}
Try to put them like this:
<table class="t_status">
<tr>
<td>No</td>
<td>Name</td>
<td>Address</td>
</tr>
</table>
and
<table class="t_status">
<tr>
<td>1</td>
<td>Michael</td>
<td>California</td>
</tr>
<tr>
<td>2</td>
<td>Greg</td>
<td>LA</td>
</tr>
</table>
if am correct you are using two tables for scrolling effect of head and data, so you will get table header for all the data.
to achieve this effect you can try using jquery table jtable
sample code
Your html syntax is incorrect. Use tr tags:-
<table class="t_status">
<tr>
<td>No</td>
<td>Name</td>
<td>Address</td>
</tr>
</table>
You should put all information into one table, thus you can assure that the rows have the same width.
<table class="t_status">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Michael</td>
<td>California</td>
</tr>
<tr>
<td>2</td>
<td>Greg</td>
<td>LA</td>
</tr>
</tbody>
</table>
<thead></thead> and <tbody></tbody> are not necessary.
It seems that you have forgot the <tr> tags. By the way, if you want to preserve your markup (correct or not, but two different tables), you can try with nth selectors and give a fixed width to each cell:
.t_status td:nth-child(1) {
width:2em;
}
.t_status td:nth-child(2) {
width:5em;
}
.t_status td:nth-child(3) {
width:5em;
}
Here's a working example.