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

Adjust dimensions of images in Bee3D carousel

I recently utilized the Bee3d library available on the following Github link: https://github.com/lukeed/bee3d While I found everything to be truly impressive, I am curious if there is a way to adjust the image size. Whenever I attempt to change the image ...

Encountered an issue retrieving two rows nested within a parent row using Bootstrap 4 and VueJs

Currently, I am working on a VueJs and Bootstrap4 component where my aim is to achieve a design similar to the one shown using the available bootstrap classes. After going through the documentation, I came across the customized classes h-75 and h-25 provi ...

Lazy-loading modules in SSR Angular 8 applications are currently unspecified

I am currently in the process of setting up my Angular 8 application to work with server-side rendering (SSR). However, I am encountering some undefined errors in webpack when running my application using ng serve, especially with lazy-loaded modules. Ever ...

What causes padding/margin when using overflow: hidden?

Today, I came across a peculiar issue. Adding the overflow: hidden style to a div seems to create extra blank space around its header content, resembling margin or padding. While this might go unnoticed in most cases, it's causing a problem with an an ...

Executing a Ruby function via AJAX

I am fairly new to working with ajax, and I find myself in need of using it for my Rails application. Here is the function in my controller: def check_code input = params[:input] code = params[:code] if input == code return true else retur ...

Retrieving a specific attribute pair from a JSON object

Currently, I am trying to retrieve the temperature data and display it on my webpage. Since these are objects with no specific order, I am struggling to understand how to access them without using an index. { "response": { "version": "0.1", "termsofServic ...

Tips for nesting an anchor tag within another tag

After reviewing various inquiries on this platform, it has been commonly believed that embedding an anchor tag inside another anchor tag is not achievable. However, I recently stumbled upon a website that successfully accomplishes this and I am curious to ...

directive unit testing unable to access isolatedScope as it is not recognized as a valid

Currently, I am in the process of conducting unit tests on a directive that was previously created. For my initial test, I simply want to verify a specific variable within the scope of the directive. However, whenever I attempt to execute the method isola ...

I'm having trouble getting the app.locals function within a button to work on my EJS template. Can anyone help troub

Below is the content of my script named agents.js: var express = require('express'); var router = express.Router(); var app = express(); // Is it appropriate to call Express like this twice? router.get('/', function(req, res, next) { ...

Don't forget to expand or collapse

I'm struggling to make this work. My goal is to initially display 50 characters and show the rest when the "read more" button is clicked, and vice versa with "read less." Another problem I've noticed is that when I click the back browser button, ...

Customizing Your WordPress Menu with HTML5 Features

Can someone assist me in creating a customized WordPress menu? I am facing issues with unnecessary classes added by WordPress and the lack of certain attributes on dropdowns. Below is the HTML code: <nav class="navbar navbar-default navbar-static- ...

How to incorporate "selectAllow" when dealing with dates in FullCalendar

I've been attempting to restrict user selection between two specific dates, but so far I haven't been able to make it work. The only way I have managed to do it is by following the routine specified in the businessHours documentation. Is there a ...

I'm looking for guidance on utilizing the NodeRT (Windows.Gaming.Input) module within an electron environment. Can anyone provide some

I'm having trouble importing the Gamepad class from a specific module, as I keep encountering the error message "Invalid arguments, no suitable constructor found." Here is the code snippet in question: const { Gamepad } = require('@nodert-win10-2 ...

Drag and drop functionality in Angular 2 using TypeScript

Has anyone experimented with the drag and drop functionality using Angular2 RC with TypeScript? Thanks, Sanket ...

JavaScript for rotating an element upon button click

My webpage design <div id="yabanner"> <img src="img/ok.jpg"> </div> <button>Click Me</button> <button>Click Me</button> <button>Click Me</button> My script code var button = document.getElementsBy ...

Store the current function in the cache, incorporate new features, and execute those additions upon calling another function

I have a pre-existing function that I am unable to directly access or modify. Due to this limitation, I have resorted to caching the function and incorporating additional functions alongside it. This function loads periodically, sometimes occurring on pag ...

acquiring the main class instance within a function without relying on an arrow function

Within my Angular project, I have integrated a datatable with row grouping and row callbacks. Datatable Options openPositionDatatableOptions = { sDom: 'rt<"bottom"p>', ajax: (data, callback, settings) => { this.service.ge ...

Difficulty in displaying JavaScript function output as text

I'm currently developing a program that randomly selects and prints a function from an array list. I am facing difficulties in getting the result to print correctly. Below is the snippet of code: const hiddenElements = document.querySelectorAll( &qu ...

Tips for including an authorization token in an HTTP request

I encountered a 401 unauthorized error when trying to access my REST endpoint, likely due to the security measures I have implemented. I suspect that there might be an issue with how I am handling the HTTP headers. The application utilizes a Spring Boot b ...

Arrange various Div elements of varying sizes in a predetermined sequence

I need to ensure that a large div maintains the same height as 20 other divs within the same container, when the screen size is between 1024px and 1920px in width. The challenge lies in arranging all these items in a floated layout grid that looks clean a ...