Leveraging CSS nth-child selector in conjunction with ngFor in angular2

Looking for a way to style odd and even rows differently in my dynamically generated table using ngFor directive in angular2.

*ngIf="AreThereMyOldMessages"
            *ngFor="let oldm of MyOldMessages;let i=index"
*ngIf="AreThereMyNewMessages"
            *ngFor="let message of MyNewMessages;let i=index"

Despite setting up CSS to color alternate rows, all the rows are being displayed in the same color.

table tr:nth-child(odd) td

Answer №1

tr:nth-child(odd) is used to target table row elements that are odd children. In this case, each tr in the markup is considered an odd child (the first) of its parent div.

To make the CSS style work as intended, you will either need to modify the markup or apply the style to the parent div.

<table>
  <tbody>
    <ng-container *ngIf="AreThereMyOldMessages">
      <tr *ngFor="let oldm of MyOldMessages;let i of MyOldMessages.length">
      </tr>
    </ng-container>
    <ng-container *ngIf="AreThereMyNewMessages">
      <tr *ngFor="let message of MyNewMessages;let i of MyNewMessages.length">
      </tr>
    </ng-container>
  </tbody>
</table>

Answer №2

Check out this CSS snippet:

.table-row:nth-child(2n){
          background-color:blue;
          }

.table-row:nth-child(2n+1){
          background-color:green;
          }

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

Navigating through various Angular 7 projects in Express using JWT authentication and role-based routing

In my Angular 7 project, I have developed multiple applications for different roles such as admin, user, and editor. Each role has its own set of components and views. When a logged-in user accesses the application, they are directed to their respective r ...

The JSX function seems to be malfunctioning, as the function part is not displaying on the webpage as intended

This code snippet is a part of a React component in a project. I have imported a CSS file for styling and have already integrated Material UI. However, the function for the new article is not displaying on the webpage as expected. import "./Widgets. ...

Eliminating pre-set CSS styles injected in Angular 2/Ionic 2 automatically

I'm encountering an issue where adding the ionic 2 gesture (press) to a button results in inline styles being automatically applied to that button. Is there a way to override the styles it adds? Button <button ion-button (press)="toggleFavourite ...

NPM IP library susceptible to Server-Side Request Forgery (SSRF) vulnerabilities

Received Security Alert from GitHub's Dependabot Regarding an Issue in My Angular Repository A security vulnerability has been identified in all versions of the NPM package "ip," allowing a malicious actor to execute arbitrary code and access sensiti ...

create an endless loop to animate in angular2

Is there a way to add iterations, or something similar to the ccs3 animation-iteration-count in Angular 2 animations? I've looked but can't find anything related. How can we apply an infinite play to the animation? Where should we include that o ...

Unable to establish connection between Angular-CLI and backend server using proxy

Visit this link for instructions on how to set up proxying to a backend server. I followed the steps carefully, but my requests are still not being proxied. I am running an Express backend on port 8080 and an Angular2 frontend on port 4200. In my Angular ...

Display a symbol retrieved from the backend server

After receiving a response from the backend server for my Angular 2/4 application, I am presented with an attribute called "connectionStatus". This attribute indicates the status of a database connection, either as "UP" or "DOWN". In order to display this ...

What is the best way to apply "display: none;" on a span element nested within a title attribute?

I am utilizing the social-share-button gem to share blog posts on social media. The website has been internationalized, making it bilingual (English and German). Everything is functioning correctly, but I encounter an issue with the social share buttons wh ...

Troubleshooting mat-accordion title problem on mobile devices

Despite reading numerous blogs and conducting extensive searches on Google, I am still unable to resolve the CSS issue related to Angular material below. Your assistance in fixing this problem would be greatly appreciated. Desktop view: https://i.stack.im ...

Leveraging the power of Bootstrap in combination with Angular 2, integrate both ng-bootstrap and the traditional Bootstrap to enhance

Beginning a fresh Angular 2 Project, I am inclined to incorporate Bootstrap 3 for the user interface. What would be the most advisable approach in this scenario? Is it feasible to blend ng-bootstrap and the original Bootstrap components? I noticed that th ...

"Placing an image at the bottom within a DIV container in HTML

My goal is to neatly align a group of images at the bottom of a fixed-height div. The images all have the same height and width, making the alignment easier. Here is the current structure of my code: <div id="randomContainer"> <div id="image ...

Creating an Escape key press event in plain Typescript without using JQuery

Is there a way to trigger an Escape button press event in Angular unit tests? const event = document.createEvent('UIEvent'); event.initUIEvent('keydown', true, true, window, 0); (<any>event).keyCode = 27; (<any ...

Having additional space preceding list elements within a flexbox setup

Check out the code snippet below: HTML Code <ul class="menu"> <li><a href="#">Item 1</a></li> <li> <a href="#">Item 2</a> </li> <li> <a href="#">Item 3</a> </li& ...

Including Previous & Next Buttons in Product Categories

I have set up the display of category products on the home page using the block below: {{block type="catalog/product_list" template="catalog/product/home_short.phtml" category_id="5" column_count="6"}}. I now want to include previous and next buttons to ...

Vue: update data and trigger function upon completion of animation transformation, utilizing animation transformation completion listener

Check out this straightforward Vue example: https://codesandbox.io/s/sleepy-haze-cstnc2?file=/src/App.vue https://i.stack.imgur.com/zboCb.png Whenever I click on the square, it not only changes color but also expands to 200% of its original size with a 3 ...

Is there a way to navigate to a specific component selector within an ngFor loop?

I have a scenario where I have multiple components running inside *ngFor on the same page. My goal is to create button links at the top of the page that, when clicked, will scroll to the corresponding component on the page. Below are the code snippets tha ...

The UI in PrimeNG dropdown does not reflect changes made with patchValue()

Currently, I am working on a reactive form that includes a few fields - three dropdowns and one calendar. These fields are all PrimeNG components. Interestingly, all fields except for the project field successfully update their values in the user interface ...

Hide the entire TR if Mysql equals zero

How can I apply the style "display: none;" to an entire TR if the result from row credits is 0? Should I achieve this using CSS or a MySQL query? <?php $username = "root"; $password = ""; $database = "aaa"; $mysqli = ne ...

Tips for filling the offset space with a column

Having a bit of trouble phrasing my question, but here's the issue I'm facing: Currently, I need to develop two different views, the first being the mobile view However, I'm experiencing some difficulties with the second view, which can be ...

Rotate the image as you hover your cursor over it

I am trying to implement a script where the image is rotated when the user hovers over it. Unfortunately, the hovering effect is not functioning as expected. $("#alcazar-image").rotate({ bind: { mouseover : function() { $(this).rotate({anima ...