Utilizing Loops to Generate Unique CSS Designs on an HTML Page

View reference image

->Take a look at the reference image provided.

->In the image, a for loop is used to create box designs and displayed above.

->The goal is to change the background color and border color of all boxes using a single HTML class name (as shown in the image).

->Each box should have a different border color and background color.

->Focus on the second box in the image titled "Loans". It contains "Crop Loan Admin" while others only have members. How can a condition be applied for this?

->All boxes have an arrow icon in the first row. How can a condition be set for this?

->To achieve this, check the code where separate divs are used for each box. They need to be iterated using a loop to bind names, border colors, and background colors to all boxes.

Html

<mat-card class="mat mr-4 ml-5 p-3" fxLayout="column" fxFlex>


    <div fxLayout="row wrap" fxFlex fxLayoutGap="20px">


      <ng-container *ngFor="let item of items">

      <!-- all-items  -->

      <div fxFlex="calc(33.3% - 20px)" class="m-1 pt-3">

        <div fxLayout class="cart-styling">

          <div class="overview-cart">

            <div class="ml-5 mt-4 heading-text">{{item?.title}}</div>

          </div>

          <img class="pr-5 mt-4" style="width: 90px;" alt="customer_icon" src="assets/icons/firsticon.png">

        </div>

        <div class="content pl-3">

          <div>

            <h6 fxLayoutAlign="space-between none" class="pb-1 pt-2 main-line border-bottom"> Members <mat-icon class="arrow-icon">arrow_right_alt</mat-icon> <span class="mr-4 value-color">1000</span></h6>

            <h6 fxLayoutAlign="space-between none" class="pb-1 line border-bottom"> Non-Members <span class="mr-4 value-color">200</span></h6>

            <h6 fxLayoutAlign="space-between none" class="pb-1 line border-bottom"> Traders <span class="mr-4 value-color">250</span></h6>

            <h6 fxLayoutAlign="space-between none" class="pb-1 line border-bottom"> Employee <span class="mr-4 value-color">40</span></h6>

            <h6 fxLayoutAlign="space-between none" class="pb-1 line border-bottom">-</h6>

            <h6>-</h6>

          </div>

        </div>

      </div>

      <!-- /all-items  -->

    </ng-container>

    </div>

  </mat-card>

CSS

.overview-cart {

  position: relative;

  width: calc(100% - 80px);

  background-color: #5f7dff;

  overflow: hidden;


  &:after {

    content: "";

    position: absolute;

    right: -175px;

    top: 0;

    border-width: 153px;

    border-style: solid;

    border-color: transparent;

    border-top-color: #fff;

    z-index: 99;

    transform: rotate(19deg);

  }

}


.cart-styling {

  width: 100%;

  justify-content: center;

  height: 30%;

  border: 1px solid #5f7dff;

}


.heading-text {

  font-size: 18px;

  font-weight: normal;

  color: #fff;

}


img {

  width: 90px;

  height: min-content;

}


.mat {

  height: auto;

}


.line {

  font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;

  font-size: 14px;

}


.main-line {

  font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;

  font-size: 14px;

  color: #0b4983;

}


.arrow-icon {

  line-height: 0.8;

  color: #0b4983;

  margin-right: 75%;

}


.value-color {

  color: #065a3d;

}


.content {

  background-color: #f7f7f9;

}

Ts file

items: any[]= [

    {

      id : 0,

      title: 'Customer',

      submodules: {

        members: 1000,

        non_members: 200,

        traders: 250,

        employees: 40

      }

    },

    {

      id: 1,

      title: 'Loans',

      submodules: {

        Crop_loan_admin: 1000,

        non_members: 200,

        traders: 250,

        employees: 40

      }

    },

    {

      id: 2,

      title: 'Insurance',

      submodules: {

        members: 1000,

        non_members: 200,

        traders: 250,

        employees: 40

      }

    },

  // ]

  // lists: any[]=[


    {

      id: 3,

      title: 'Inventory',

      submodules: {

        members: 1000,

        non_members: 200,

        traders: 250,

        employees: 40

      }

    },

    {

      id: 4,

      title: 'Accounts',

      submodules: {

        members: 1000,

        non_members: 200,

        traders: 250,

        employees: 40

      }

    },

    {

      id: 5,

      title: 'Masters',

      submodules: {

        members: 1000,

        non_members: 200,

        traders: 250,

        employees: 40

      }

    },

  ]

Answer №1

Assign a color property to each item

{
      id : 0,
      title: 'Customer',
      theme : "red", // for example 
      submodules: {
        members: 1000,
        non_members: 200,
        traders: 250,
        employees: 40
      }
    },

Then incorporate ngStyl in the html file (ensure correct placement of ngStyle within div)


...

 <ng-container *ngFor="let item of items">

      <!-- all-items  -->

      <div fxFlex="calc(33.3% - 20px)" class="m-1 pt-3" [ngStyle]="{'background-color': item.theme, 'border-color': item.theme}">

        <div fxLayout class="cart-styling">

          <div class="overview-cart">

...

Answer №2

To achieve this effect, you cannot rely on just one css class. However, there is a workaround:

Include a theme color for each object:

items: any[]= [
  {
    id: 0,
    theme: '#ff0000',
    title: 'Customer',
    submodules: {
      members: 1000,
      non_members: 200,
      traders: 250,
      employees: 40
    }
  },
  {
    id: 1,
    theme: '#00ff00',
    title: 'Loans',
    submodules: {
      Crop_loan_admin: 1000,
      non_members: 200,
      traders: 250,
      employees: 40
    }
  },
];

Then, bind it to the HTML:

<mat-card class="mat mr-4 ml-5 p-3" fxLayout="column" fxFlex>
    <div fxLayout="row wrap" fxFlex fxLayoutGap="20px">
      <ng-container *ngFor="let item of items">

      <div [style]="'background-color: ' + item.theme + '; border: 1px solid ' + item.theme + ';'" fxFlex="calc(33.3% - 20px)" class="m-1 pt-3">
      </div>

    </ng-container>
    </div>
  </mat-card>

This will apply the specified theme to each card.

Check out a live example on StackBlitz.

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 reason behind the ability to access the result of a redux call "immediately" by wrapping it into a promise?

Currently, we are operating in a Redux (with thunk middleware) / React environment. The piece of code below is causing some issues: onMyClick = () => { this.props.mySynchronousActionWhichWillCreateNewReducerState(); this.setState(...my state ch ...

Aligning text with a scalable vector graphic (SVG) illustration

Is there a way to position the text next to the SVG object? The current output is as follows: The desired outcome is to have the text displayed alongside the SVG image: Below is the HTML code snippet: <div class="alert alert-success smallFont" id="i ...

Why does it fire off numerous requests even though I only called it once?

Everything seemed to be working fine with my project. However, I noticed in the console network that one of my GET requests is being sent twice even though I only triggered it once. View network console here If I comment out the entire created function co ...

Guidelines for showcasing a map on my website with the option for users to select a specific country

Is there a method to showcase a global map on my site and let the user select a country to receive relevant information? I am aware of embedding Google Maps, however, it includes unnecessary controls like zooming which I prefer not to inconvenience the us ...

Setting the button value to a textbox and refreshing the status information (Codeigniter)

How do I pass the value of my "ACTIVE" status attribute to my textbox? I want to update the user's status by clicking the ACTIVE button. The user's status is currently pending. While I can easily pass the userID, I'm facing an issu ...

Implementing a persistent header on a WordPress site with Beaver Builder

My website URL is: . I have chosen to use beaver builder for building and designing my website. I am in need of a fixed header that can display over the top of the header image. Here is the code snippet that I currently have: <div id="header">html ...

Using AngularJS routing with an Express 4.0 backend API

Recently, I began working on an application utilizing Express 4.0 server. Following a tutorial on scotch.io (http://scotch.io/tutorials/javascript/build-a-restful-api-using-node-and-express-4), I structured the routes to support a backend api serving an An ...

Encountering a new challenge in Angular: The error "InvalidPipeArgument: '' for pipe 'AsyncPipe'

Whenever I try to fetch data from the server, these errors keep popping up. This code was written by someone else and I would like to improve upon it. Could anyone suggest the best approach to handle this situation? Are there any coding patterns that sho ...

Vue 2.0 custom filter not producing any output

I am attempting to create a customized filter that identifies and returns the items that correspond to a given input. It functions effectively with basic arrays like ['Apple', 'Banana', 'Cupple'], but encounters difficulty whe ...

Executing one specific test in Protractor using Selenium

How can I execute a single test in Protractor with Selenium? describe('Login page', function() { beforeEach(function() { browser.ignoreSynchronization = true; ptor = protractor.getInstance(); }); it('should contain navigation items&ap ...

Internet Explorer causing issues with Jasmine mocking ajax calls

Recently, I attempted to develop a spec that enables mocking out Ajax calls. The testing procedure functions seamlessly on browsers such as Chrome and Firefox; however, I encountered some difficulties while executing the test case on Internet Explorer (spe ...

What is the reason for the unique behavior of v-bind with boolean attributes? More specifically, why is the number 0 considered truthy in

According to the official documentation, <button v-bind:disabled="isButtonDisabled">Button</button> In this example, the disabled attribute will be added if isButtonDisabled is equal to 0, despite the fact that in JavaScript, 0 is co ...

Creating an email in Gmail with a pre-filled HTML body

Currently, I am developing a Django web application and seeking a way to enable my app to send emails via Gmail. I have explored creating a hyperlink that opens the compose window in Gmail, as well as prepopulating fields such as "to," "cc," "bcc," and bod ...

How can Vue JS 3 components exchange data between each other?

I am attempting to share data from a variable favorite_count in the Favorites component located in the file Favorites.vue. My goal is to pass this data to the App Component in the App.vue file but I have been unsuccessful so far. I want any changes made to ...

What is the significance of the "#" character in the URL of a jQuery mobile

As I encounter a strange issue with my jQuery mobile page, I notice that when accessing page.php everything looks good, but once adding #someDetailsHere to the URL, it only displays a blank white page. What could be causing this and how can I resolve it? ...

The "export 'ɵɵcomponentHostSyntheticListener' (imported as 'i0') was not found in '@angular/core" error occurred during the Angular build

Trying to set up an Angular application with clarity UI. Using Angular version 10.1.1 and after adding clarity, encountering build errors. ERROR in ./node_modules/@clr/angular/esm2015/utils/animations/expandable-animation/expandable-animation.js 33:8-43 &q ...

Using ExpressJS with the GET method to retrieve JSON data in conjunction with Angular version 4 and above

This inquiry may be a duplicate, I apologize for any repetition. My objective is to console.log JSON data from the "Server" folder. Please find the attached folder structure below. https://i.stack.imgur.com/uec4O.png server.js const express = require(&a ...

"Utilizing SVG format to manipulate text, adjusting font size within tspan elements

There seems to be an issue with the font size on SVG. Even though both the SVG and paragraph have a font-size of 16px, they appear different. I would like the font size in the SVG to match that of the paragraph. Another concern is how to center <tspan ...

Investigating issues with console errors and variables within callback functions in Angular 2 unit tests using Jasmine

I need assistance in achieving full coverage for this simple function, but I am struggling to do so. name-list.service.ts import { Injectable } from '@angular/core'; import { Http, Response } from '@angular/http'; import { Observable ...

Retrieving HTML content from an AJAX request

Is it possible to query HTML data returned from an AJAX request? I attempted the following code: $.post("gethtml.php", { url: $url.val() }, function(data) { var $html = $(data), $links = $("link, script, style", $html), $body ...