Assign a different color to every other pair of rows

Here is the current table I am working with:

<table>
   <tr>
       <td>a</td>
  </tr>
   <tr>
       <td>b</td>
  </tr>
   <tr>
       <td>c</td>
   </tr>
   <tr>
       <td>d</td>|
   </tr>
</table>

I would like to customize this table by making the first two rows have a red background color and the next two rows have a blue background color.

Answer №1

When styling the table rows:

To highlight the first two rows in red, use the following CSS code:
table tr:not(:nth-child(n+3)) {
    background: red;
}

For the remaining rows:

To highlight the last rows in blue, you can use this CSS code:
table tr:nth-child(n+3) {
    background: blue;
}

Answer №2

nth-child is a useful CSS selector that allows you to target specific elements based on their position in a sequence. For example, selecting the first two elements can be achieved using nth-child(-n+2), while selecting the last two elements requires nth-last-child(-n+2).

tr:nth-child(1),
tr:nth-child(2) {
  background: red;
}

tr:nth-child(3),
tr:nth-child(4) {
  background: blue;
}
<table>
  <tr>
    <td>a</td>
  </tr>
  <tr>
    <td>b</td>
  </tr>
  <tr>
    <td>c</td>
  </tr>
  <tr>
    <td>d</td>
  </tr>
</table>

tr:nth-child(-n+2) {
  background: red;
}

tr:nth-last-child(-n+2) {
  background: blue;
}
<table>
  <tr>
    <td>a</td>
  </tr>
  <tr>
    <td>b</td>
  </tr>
  <tr>
    <td>c</td>
  </tr>
  <tr>
    <td>d</td>
  </tr>
</table>

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

Efficiently adapting html content for Vue localization

I've been utilizing the v-html tag to display HTML in my Vue pages. However, I came across a string that was a complete HTML file with content like this: <html xmlns="https://www.w3.org/1999/xhtml"> <head> .... <style> < ...

Issues with spacing in navigation bar

I've been searching and experimenting for hours, but I can't seem to make it work. In my "inline-form," I have a button on the left, an input field in the middle, and a button on the right within a navbar. Everything displays correctly when the ...

Utilizing Ajax to Create a Form with Invisible Google Recaptcha

Here is my code: function submitForm(token) { $(document).ready(function() { $("#submit").click(function() { var name = $("#name").val(); var email = $("#email").val(); var password = $("#password").val(); var contact = $ ...

Having difficulties in dynamically swapping audio sources using JavaScript?

It seems like I'm overlooking something simple here. The issue I'm facing involves creating a series of buttons with different audio sources as titles. When a user clicks on a button, the corresponding source should update in the audio player. T ...

What is the method for retrieving the value of a checkbox when clicked?

Managing a dynamically created HTML table with checkbox values can be tricky, especially when the value needs to reflect changes in real-time. If you are struggling to update the status of the checkboxes based on database values and pass that information t ...

Where are Temporary files stored in the Google Chrome extension?

I've been searching for over 3 hours, but still cannot locate the exact location (under OS X 10.6.8). Utilizing PageSpeed Insights, I am attempting to find where temporary compressed images are stored. The Chrome "web" location displayed is: filesy ...

Unable to expand the width of the video element

So I have encountered a situation like this The video displayed does not fully expand to the width of its containing element. This is what my HTML structure looks like - <div class="webcam"/> <div class="control-container"> </div> ...

Utilizing a percentage for the border-width property

I want to remove a triangle from the bottom of a div using a CSS trick involving borders. This involves creating a border with one transparent side and coloring the other sides to create the triangle effect. The issue I'm facing is that the div has a ...

Problem with using Jquery DIY slider for multiple purposes

There seems to be an issue with the DIY Slider that I'm using. Whenever I attempt to use this slider more than once on a single page, the second one fails to function. I have exhausted all possible solutions that come to mind. $(".slider").diyslider ...

Creating a CSS section that expands with a minimum height of 100% and perfectly centers its content vertically

Seeking an elegant solution for having sections with a minimum height of 100% (window height). The div should expand to fit the content if it exceeds 100%, and vertically center the content within the div if it is smaller. I have managed to find a simple ...

What is the best way to include a final tag on its own line?

I'm trying to add a new line for "Payment Remaining", but it doesn't go to the next line as expected. I need the final text to be on a separate line, Payment Remaining #authorization-wizard-start-work .btn-group label.active { color: #009 ...

Regular expression that can be used to extract information from a text file by filtering and returning only

When I open a text file, it contains several lines like the examples below. surname australia enter name j.jonhs enter name j.cause enter name f.london enter I am seeking assistance to modify the code snippet below so that it captures the first line m ...

Displaying content using Jquery's slideDown feature, overlapping existing content

I am working on displaying one piece of content over another using Jquery slidedown as an overlay. Despite adding z-index properties, I am struggling to make it work. My goal is to have the div with class "selection_nip" appear as an overlay on top of ano ...

Restrict page scrolling to the vertical position of a specified div [Using jQuery, javascript, and HTML]

Currently, I am in the process of developing a personal website that features numerous expandable items. My goal is to restrict the page scrolling to the height of the expanded item once it is opened. I do not want users to be able to scroll above or below ...

Move the contents of a div to the right side

One of the issues I am facing is with aligning replies to comments correctly in my comment section. The replies are not aligning properly with the parent comment and it's causing a display problem. Link to JSFiddle for reference Here is the CSS code ...

Troubleshooting: Unable to fetch CSS file in Node.js Express framework

I'm trying to access my .html file with localhost using node.js, but I'm facing an issue with loading the CSS that is included in the file. I am using the express framework for this purpose. Code: var express = require('express'); var ...

Variations in browser rendering of SVGs as CSS list-style-images

After creating a simple SVG of a red circle to serve as the list-style-image for my website, I discovered the concept in this Stack Overflow response: The solution worked seamlessly across all browsers except Internet Explorer. In IE, the SVG image render ...

Stacked on top of one another are in-line block items

I've been working on this code for a while now, but I can't seem to get it right. My goal is to create a horizontal line of navigation links with the first link aligned to the left and the rest following in sequence like a text line. However, a ...

transfer text from one input field to all others

Here is my customized form <form id=form1 method=post accept-charset=UTF-8 action=> <input type=hidden name=video_id[0] value=K94KNsN43BU><p>Title<br> <textarea rows=1 cols=40 id=title_0 name=title[0]>custom title ...

Ensure that all content in the table rows remains visible, even in a table with unusually lengthy cells

My HTML table is structured like this: +-------------+-------------+----------+ | Single line | Single line | Very, | | | | very | | | | long | | | | text, | | ...