Modifying the color of table rows using HTML and CSS

Seeking guidance on HTML and CSS with a simple query.

How can I assign different colors to each row in a table? For instance, row 1 as red, row 2 as blue, and so on.

Here is my HTML code:

#table {
  font-family: Arial, Helvetica, sans-serif;
  width: 600px;
  border-collapse;
  collapse;
}
#table td,
#table th {
  font-size: 12x;
  border: 1px solid #4D365B;
  padding: 3px 7px 2px 7px;
}
#table th {
  font-size: 14px;
  text-align: left;
  padding-top: px;
  padding-bottom: 4px;
  background-color: #4D365B;
  color: #918CB5;
}
#table td {
  color: #000000;
  background-color: #979BCA;
}
<table id="table">
  <tr>
    <th>Company</th>
    <th>Contact
      <th>Country</th>
  </tr>
  <tr>
    <td>1</td>
    <td>2
      <td>3</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2
      <td>3</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2
      <td>3</td>
  </tr>
</table>

Answer №1

If I understand correctly, you are looking to style each row with a different color in a table. There are various ways to achieve this:

  • Assign a class to each row and then style those classes individually.
  • Utilize the direct sibling selector tr + tr.
  • Use the :nth-of-type selector.

table tr {background: red;}
table tr:nth-of-type(2) {background: blue;}
table tr:nth-of-type(3) {background: green;}
table tr:nth-of-type(4) {background: yellow;}
table tr:nth-of-type(5) {background: grey;}
<table id="table">
  <tr>
    <th>Company</th>
    <th>Contact
      <th>Country</th>
  </tr>
  <tr>
    <td>1</td>
    <td>2
      <td>3</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2
      <td>3</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2
      <td>3</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2
      <td>3</td>
  </tr>
</table>

Answer №2

Give this a try too!

#table tr{background: red;}
#table tr:nth-child(2n+1){background: blue;}

#table {
font-family: Arial, Helvetica, sans-serif;
width:600px;
border-collapse;collapse;

#table td, #table th {
font-size:12x;
border:1px solid #4D365B;
padding: 3px 7px 2px 7px;

#table th {
font-size:14px;
text-align:left;
padding-top:px;
padding-bottom:4px;
background-color:#4D365B;
color:#918CB5;

#table td {
color:#000000;
background-color:#979BCA;
}
<table id="table">
<tr><th> Company</th><th>Contact<th>Country</th></tr>
<tr><td> 1</td><td>2<td> 3</td></tr>
<tr><td> 1</td><td>2<td> 3</td></tr>
<tr><td> 1</td><td>2<td> 3</td></tr>

Answer №3

To achieve this effect, simply employ pseudo selectors like nth-child.

#table tr:nth-child(odd){background:green}
#table tr:nth-child(even){background:yellow}
<table id="table">
<tr><th> Name</th><th>Age<th>Country</th></tr>
<tr><td>John</td><td>25<td>USA</td></tr>
<tr><td>Sarah</td><td>30<td>Canada</td></tr>
<tr><td>Michael</td><td>22<td>Japan</td></tr>
</table>

Answer №4

tr:nth-child(even) {
  background: #ff0101;
}

 tr:nth-child(odd) {
  background: #0030ff;
}
<table class="example1">
  <tbody>
    <tr>
      <th>Month
      </th>
      <th>'94
      </th>
      <th>'95
      </th>
      <th>'96
      </th>
      <th>'97
      </th>
      <th>'98
      </th>
      <th>'99
      </th>
      <th>'00
      </th>
      <th>'01
      </th>
      <th>'02
      </th>
    </tr>
    <tr>
      <td>Jan
      </td>
      <td>14
      </td>
      <td>13
      </td>
      <td>14
      </td>
      <td>13
      </td>
      <td>14
      </td>
      <td>11
      </td>
      <td>11
      </td>
      <td>11
      </td>
      <td>11
      </td>
    </tr>
    <tr>
    ... (remaining table rows)
  </tbody>
</table>

Answer №5

If you want to style each row individually using CSS, you can follow this approach:

table tr:first-child {background: red;}
table tr:nth-child(2) {background: blue;}
table tr:nth-child(3) {background: orange;}
table tr:nth-child(4) {background: yellow;}
table tr:last-child {background: purple;}

Answer №6

This snippet of code will modify the appearance of the table row's color.

 table, td, th {
    border: 1px solid red;
}

th {
    background-color: red;
    color: black;
}

Reference:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to apply a Javascript function to multiple tags that share a common id?

I am experimenting with javascript to create a mouseover effect that changes the text color of specific tags in an HTML document. Here is an example: function adjustColor() { var anchorTags = document.querySelectorAll("#a1"); anchorTags.forEach(func ...

Unseen related model fields

I am struggling to figure out what is causing an issue in my code. Can anyone explain why the fields in locations = Location.objects.filter(user=add_profile.user) are not showing up on my html page? models.py class Location(models.Model): user = mode ...

An issue with excess whitespace appearing below the footer while adjusting the size of the webpage

I have a database table on my website that I am styling with the help of bootstrap. Check out this screenshot for the issue: https://i.sstatic.net/CUkjz.jpg For the HTML code, you can view it here: And for the relevant CSS, click on this link: I used pa ...

How to perfectly align text vertically using Bootstrapstylesheet

Previously attempted solutions from: here for vertical-align with Bootstrap 3 and also from: here for vertical-align: middle with Bootstrap 2 but unfortunately, neither worked. If you want to view the page, click here: Check out the code snippet: <di ...

ReactJs - Reactstrap | Issue with Jumbotron displaying background color

I am trying to create a gray background using Jumbotron in reactstrap. After installation and reference in required places - index.js import 'bootstrap/dist/css/bootstrap.css'; Jumbotron has been implemented in SignIn.js - import Re ...

Using Perl's regular expressions to perform substitutions

Within an html file, my goal is to incorporate a link to the numbers that appear within the [ ] in various patterns such as: [1] or [1-2] or [1,3,6] or [1,3,4-6, 9]. While I have managed to match the simple pattern like the first one: $html =~ s`\[&b ...

JavaScript xPath is ineffective at providing a return value

Trying to work through an issue with xPath and I am new to this concept. Any guidance or assistance in JavaScript would be greatly appreciated. Here is the simple HTML document that I have: <!DOCTYPE html> <html> <head> < ...

Can one select a radio button without corrupting the data in VUE?

How can I preselect a value for radio buttons created using Vue's List Rendering without having a dataset in the script? Below is the code snippet for a better understanding of my goal: SCRIPT: data() { return { formData: {} //formData st ...

Tips for choosing the ancestor's ancestor in CSS

<div id="maincontent"> <div id="content"> <div id="admin"></div> </div> </div> Looking for a CSS rule that will target #maincontent only when #admin is present? I have a background color set in the admi ...

Struggling to connect HTML elements with AngularJS and TinyMCE?

My objective is to edit text using tinymce, persist it in a database, and display it within a div by incorporating angularJS for the same styling and formatting. I am utilizing tinymce 3.5.8 with an angularUI directive. I have successfully saved the wysiw ...

How can TailwindCSS be used to wrap a nested flexbox at a specific viewport?

Utilizing TailwindCSS, I have the following code snippet: <div class="box-border flex w-auto flex-row gap-2 rounded-2xl border p-4 my-4 mx-4"> <div class="grow-0"> <div class="flex h-full flex-col items-center ...

Tips for keeping two divs aligned horizontally on a webpage

Currently diving into the world of Bootstrap 5, I am facing a challenge with keeping my links and the label "Testing" on the same line. Despite using text-start and text-end classes, they refuse to cooperate. How can I ensure that they both stay aligned ho ...

Utilizing if conditions within loops in a Jade template

Hey there. I am attempting to create a `tr` element at the start and after every 9th item. I am using the modulo operator as shown in the example above. However, if I want to include `td` elements inside that same `tr`, with an `else` condition for inst ...

The Body and HTML elements compress to sizes significantly smaller than the viewport

I am currently working on making my WordPress website responsive. On one of the pages, I have two images that I want to set a max-width of 100% to ensure they are responsive. However, when I shrink the page in Chrome dev tools, I noticed that the <html& ...

How to style the videojs chapter menu with CSS to extend the box to the full length of the line

I am currently working on customizing the videojs CSS for the ChapterButton menu in order to make it expand to accommodate varying line lengths without wrapping. Even though I can set it to a fixed width, I require it to be able to adjust to different line ...

The NgbTypeahead element is not able to scroll when placed within a scrollable container

Currently, I am utilizing the NgbTypeahead component from ng-bootstrap. The issue I am facing is that when I place the typeahead component within a scrollable element and proceed to scroll down, the position of the dropdown container remains unchanged. &l ...

Click automatically upon loading the page

I'm looking for help to automatically click a button after the page loads. I currently have to manually click the "Buy the basket" button, but I am not familiar with coding at all. I need a fully ready code that can automate this process for me. Can s ...

Is there a way to direct to a particular section of an external website without relying on an id attribute in the anchor tag?

I am aware that you can link to specific id attributes by using the following code: <a href="http://www.external-website.com/page#some-id">Link</a> However, what if the external HTML document does not have any ids to target? It seems odd tha ...

Using node.js to submit a form and upload an image

Seeking some assistance as a newcomer to node. I am trying to achieve a task without the use of any other frameworks like Express. Here is the scenario: I have created a form where users can upload photos to a server running on a node. The form contains t ...

Ways to align an image and text vertically next to each other within a div

After coming across the solution for ''Vertical Align in a Div ", I decided to implement Adam's answer. However, my challenge lies in figuring out how to display both an image and text side by side with the text centered vertically. View My ...