Differentiating CSS Styles for Odd and Even Table Elements

I am trying to implement a style where every other row in my table (myrow) has a different background color. However, currently it is only showing the color specified for odd rows.

.myrow, .myrow:nth-of-type(odd) + .myrow:nth-of-type(even) ~ .myrow:nth-of-type(odd) {
       background: #f8f8f8;;     
    }

    .myrow:nth-child(even),.myrow:nth-child(even) ~ .myrow:nth-of-type(odd), 
    .myrow:nth-child(even) + .myrow:nth-child(odd) ~ .myrow:nth-of-type(even){ 
       background: blue; 
    }

    <table class="col-xs-12 col-sm-12 col-md-12 col-lg-12 main-table "> 
    <ng-container *ngFor="let r of results">
      <tr class="myrow">
         <div class="row>
             <div class="col-xs-12 col-sm-12 col-md-11 col-lg-11">
              <table class="wd-100 bus-table">
                  <tr></tr>
              </table>
             </div>
         </div
      </tr>
    </ng-container>
    </table>
    

Answer №1

table {
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

table tr.myrow:nth-child(even) {
  background: blue;
}

table tr.myrow:nth-child(odd) {
  background: #f8f8f8;
}

table tr.myrow:hover {
  background-color: #ddd;
}

Answer №2

give this code a shot

Select every even child using CSS

table {
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

table tr:nth-child(even) {
  background: blue;
}

table tr:nth-child(odd) {
  background: #f8f8f8;
}

table tr:hover {
  background-color: #ddd;
}
<table class="col-xs-12 col-sm-12 col-md-12 col-lg-12 main-table ">
  <ng-container *ngFor="let r of results">
    <tr class="myrow">
      <div class="row">
        <div class=" col-xs-12 col-sm-12 col-md-11 col-lg-11 ">
          <table class="wd-100 bus-table ">
            <tr>
              <td>a</td>
              <td>b</td>
            </tr>
            <tr>
              <td>a</td>
              <td>b</td>
            </tr>
          </table>
        </div>
      </div>
    </tr>
  </ng-container>
</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

"Implementing a consistent body border design on all pages using CSS for print media

Having an issue with my html page layout where I am unable to display a border on both printable pages. My current code only adds the border to the first page. Any suggestions on what I need to change in order to show the border on every printed page? Be ...

One potential solution to reorganizing the layout is to utilize two rows instead of just one

My current form layout is not very user-friendly, as it appears to be too long for one row. I have fields for amount, note, date, and an option list. I would like to arrange the form so that amount and note are displayed in one row, followed by date and o ...

Improving ASP.NET MVC 4 output by bundling whitespace and formatting

While not a critical issue, I find it quite bothersome that when using the bundling feature in ASP.NET MVC 4, there is minimal white space in the output. For example: If manually rendered: <head> <title>Example page</title> < ...

Only the initial value is being processed in the for loop using Javascript

I am having issues trying to sum the values within a for loop sourced from my Django database using JavaScript. Currently, when I iterate through the loop, only the first value in the array is returned. I need assistance in finding a solution that allows ...

Ensuring Radiobuttons are Selected on a Page After Clicking the Submit Button

Seeking assistance with this issue: I am working on a page that includes radio buttons: <label>Delivery type:</label> <input type="radio" name="delivery" value="7" class="delivery-item" id="del-type-7" onclick=""><label class="label" ...

Learn the process of utilizing Uglify.js to combine JavaScript files for individual views within Express.js

My website is currently built on express.js and I am looking to optimize my JavaScript files using uglify.js in the build process. An example of a typical view on my site appears as follows: extend layout block head script(src="/js/polyfills/html5s ...

What are the best methods for aligning pseudo elements vertically?

I am facing an issue with the placement of a before and after pseudo element for articleSubTitle. The problem occurs when the text from articleSubTitle wraps to a new line within a small container or on mobile devices. This causes the after element to appe ...

Ways to layer two divs on each other and ensure button functionality is maintained

In the process of developing a ReactJS project, I encountered the challenge of overlapping my search bar autocomplete data with the result div located directly below it. For a more detailed understanding, please take a look at the provided image. Here&apo ...

Having trouble with looping the CSS background using JavaScript in CodePen

Can someone help me with looping through CSS background images? Why is the background not changing in this code example: http://codepen.io/anon/pen/dGKYaJ const bg = $('.background').css('background-image'); let currentBackground = 0; ...

What is the process for updating the background image in Ionic?

Currently facing an issue with setting an image for background in Ionic. The code provided doesn't seem to be working as expected. It appears that the style sheet is not being applied correctly. Not sure where exactly to add the style sheet or how to ...

The data table appears to be malfunctioning when the table collapses

Having trouble with the data table when adding a collapse tr. Removing the collapse tr fixes the data table issue. Any help on how to resolve this would be appreciated. $('.tableToggleUl').parent('td').css('padding','0 ...

Create a table by incorporating the information from the page along with additional content

I need to extract the data from a list and convert it into a table, add some additional content that I will provide, and then align the table accordingly. While I can easily align elements and already have the list, I am facing difficulty in converting it ...

What is the equal tr of CSS?

Is there a way to separate the menu bar from the body within a div, so that everything after 'contact' appears below it? Is there a specific code or command similar to a newline that can achieve this? Your help is greatly appreciated :) Thank you ...

implementing jquery typewriter plug-in for a responsive bootstrap image

I am using Bootstrap and my image response dynamically, but I am having trouble adding text to it. I would like to use a jQuery typewriter plugin for the text on the image, which should resize according to screen size. Here is my HTML code and I have used ...

Multiple buttons in a single field

I am attempting to replace a Dropdown list with a series of buttons that will streamline the choices previously displayed by the dropdown list. Specifically, we are using graphic png files for the types of buttons. We experimented with checkboxing and ra ...

Embed schema information into HTML tags using JavaScript

Is there a way to insert text, specifically schema data, into an HTML div tag using JavaScript? While I know how to modify existing values within a tag like class, href, and title, I am struggling to find a method to add something entirely new. Essentiall ...

Creating a WordPress widget that links to a local file

Hey there, I've been attempting to utilize the text widget in order to link to a local file, but unfortunately, it's not working as expected, and I'm unsure of the reason why: Here is what I am using: <a href="file:///D:/" target="_blan ...

The navigation bar in Bootstrap 3.2 expands in size as you scroll past it

Whenever I reach the bottom of the page, the navigation bar suddenly enlarges. The navbar is located at the top of my code. I attempted moving the navigation to the bottom of the code... but it didn't fix the issue... Could someone please identify wha ...

Adjust the position of the IMG to the left side

Struggling with some code and need some assistance: <script> function run() { document.getElementById("srt").value = document.getElementById("Ultra").value; } </script> <script> function ru ...

Switching the typeface within the content data

Recently, I incorporated this code: <div data-content="Reach" class="main-image"> along with the following CSS styling: .main-image:before { content: attr(data-content); I am now wondering if there is a method to adjust the font size and ...