Creating various styles for cards using ngFor within Angular

I have been working on applying different styles to cards created using ngFor in Angular. Here's how it currently looks: https://i.sstatic.net/UhbSp.png

My aim is to set unique styles for each card. Due to the ngFor loop used in creating them, I initially could only change the style for all cards collectively.

However, I was able to assign a class to each card element as shown below:

<div *ngFor="let module of modules" class="{{module.name}}">

Subsequently, I applied CSS styling like this:

div.Rekrutacja {
  background-color: rgb(1, 1, 1);
} 

This approach partly worked as it targeted the first element but did not allow further modifications. When I attempted to set the background color to black, here is what happened:

https://i.sstatic.net/FDvs0.png

The style seemed to be applied to an unclear area, preventing me from specifically targeting the mat-card element for customization.

Below is the HTML structure being used:

Here is the relevant CSS code:

And finally, the TypeScript code defining the modules:

Answer №1

In order to change the background color of the <mat-card> within each specific section, target the individual <mat-card> instead of wrapping <div> elements:

div.Rekrutacja .mat-card {
  background-color: rgb(1, 1, 1);
}

div.Sprzedaż .mat-card {
  background-color: blue;
}

div.Ustawienia .mat-card {
  background-color: yellow;
}

Check out this simplified example for demonstration.

I hope this clarifies things for you!

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

Tips on sending a form to the server with ajax technology

I'm struggling with sending a button id to my server through ajax in order to submit a form without having to constantly reload the page every time I click on a button. Unfortunately, it's not working as expected and I can't figure out why. ...

What is the best way to create a timer using JavaScript?

I'm looking for a reverse timer to track session timeout. I found a code on codepen that works as a clockwise timer, but my attempts to make it counterclockwise have failed. Can someone please suggest a solution? I want the timeout to be either 1 hour ...

Refresh the Angular view only when there are changes to the object's properties

I have a situation where I am fetching data every 15 seconds from my web API in my Angular application. This continuous polling is causing the Angular Material expansion panel to reset to its default position, resulting in a slow website performance and in ...

"Enhance Your Video Experience with a Personalized Play Button

Is it possible to customize the play icon for an embedded YouTube video? I came across a post discussing this topic: Can I change the play icon of embedded youtube videos? However, when trying to use something transparent as the new play button, the origin ...

How to Lazy Load a Standalone Component with Parameters in Angular 17?

This is my HTML code snippet: <a class="dropdown-item" [routerLink]="['/videos', p.param]">{{ p.title }}</a> Below is the code in app.route.ts file: { path: 'videos/:folder', loadComponent: () =& ...

Exploring observables for querying the OMDB API and obtaining information on movies

Hey everyone, I'm currently working on implementing a live search feature using Observables in Angular2 to fetch Movie data from the OMDB API. While I can see that it is functioning correctly in the Chrome Network tab, the results aren't showing ...

Maximizing HTML5 Game Performance through requestAnimationFrame Refresh Rate

I am currently working on a HTML5 Canvas and JavaScript game. Initially, the frames per second (fps) are decent, but as the game progresses, the fps starts decreasing. It usually starts at around 45 fps and drops to only 5 fps. Here is my current game loo ...

A nifty little JavaScript tool

Recently, I created a bookmarklet that opens a new window with a specified URL and passes variables to a PHP script using the GET method. However, I now require it to load the same PHP script and pass the same variables but within a div element this time. ...

When using the `.push` method, the array becomes null

Currently, I am in the process of developing an angular application. Within my component's .ts file, there exists an array structured as follows: public myArray = []; public dataFromAPI = []; In a particular method within this component, whenever I ...

Guide to utilizing variables with Pattern.compile in Java for selenium RC

I am facing a challenge where I need to check if the date in a field corresponds to the current day. In Selenium IDE, this is easily achieved: <tr> <td>storeExpression</td> <td>javascript{var date = new Date ...

Center aligning text within an accordion button using Bootstrap 5 framework

I'm struggling to center align the text inside the accordion button while keeping the arrow on the right side Here is an example of what I'm trying to achieve: https://i.sstatic.net/2brrY.png In the Bootstrap 5 documentation, there's an e ...

Is your idTabs design in need of some sprucing up

Just stumbled upon idTabs and trying to implement a basic one on my server. I've taken the code from the 'Usual' page, included the plugin file and jQuery, but all I'm seeing is... - Tab 1 - Tab 2 - Tab 3 This is tab 1. Seems like it& ...

Extracting Data from Web Pages

How can I efficiently extract data from a webpage with dynamic loading using AJAX? For instance, imagine a webpage that initially shows 20 images, but as the user scrolls down, more images are loaded (similar to Facebook). In this scenario, how would you ...

Conceal the PayPal Button

Currently, I'm facing a challenge where I need to dynamically show or hide a PayPal button based on the status of my switch. The issue is that once the PayPal button is displayed, it remains visible even if the switch is toggled back to credit card pa ...

Resetting the input value for the typeahead component is currently not feasible

When using the typeahead component, it appears that clearing the input value after the results list is displayed is not possible. However, if the results list does not appear, the model can be cleared without any issues by using a button. Here are the ste ...

Unveiling individual modules of an Angular library using public-api.ts in the latest version of Angular (Angular 13)

After completing an upgrade on my Angular library project from version 11 to 13, I encountered an issue when attempting to execute the ng build command. In version 11, the setup looked like this: I had multiple smaller modules, each containing various co ...

CSS animation is supported on most browsers with the exception of Safari

I'm facing an issue with my animation that works fine on all browsers except Safari. I've tried using the autoprefix extension in VSCode to automatically add CSS vendor prefixes, but the animation still isn't functioning correctly. Here&apo ...

Generate an auto-focus Power BI report seamlessly and effortlessly

I'm working on a straightforward Microsoft Power BI example that showcases a list of employees grouped by gender. <iframe width="300" height="200" src="https://app.powerbi.com/view?r=******" ></iframe> The issue I am facing is that the ...

Text element in SVG not displaying title tooltip

Looking for a solution to display a tooltip when hovering over an SVG Text element? Many sources suggest adding a Title element as the first child of the SVG element. This approach seems to work in Chrome, but not in Safari - the primary browser used by mo ...

Calculating the subtotal amount using Vue.js is a straightforward process

My Vue.js project functions as a shopping cart, and I am looking to calculate the subtotal for each product row. Here is an excerpt of my HTML code: <div id="app"> <table border="1"> <thead> <tr> <th>#</th ...