how can I apply Styles to these cells [1]: https://i.stack.imgur.com/6LV50.png
please help me with the code
already tried the nth child to tr and td but not getting the logic
CodePudding user response:
3 way of styling tables using css:
- inline
- style html tag
- CSS file
you should apply styles to the table html tags representing cells:
td{
padding:7px;
vertical-align:middle;
}
th{
padding:7px;
vertical-align:middle;
}
tr td:nth-child(4){
color:green;
}
tr td:nth-child(4){
color:red;
}
tr{
border-bottom:solid 1px #f1f1f1;
}
table{
background:#fff;
}
CodePudding user response:
Use nth-child(even)
for tr
& td
first-child
for only selecting every first td
cell at even tr
row.
While need to select all last td
cells then use :last-child
A nice place to learn more about tables styling
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
tr:nth-child(even) td:first-child {
color: red;
}
td:last-child {
color: green;
}
<h2>HTML Table</h2>
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
You can also use nth-child(3)
nth-child(4)
..... and can better implement after going through tutorials available on the web