0

I am trying to write css for dynamic classes, for that I generated classes using ngFor like below

<div style="max-height: 450px;" *ngFor="let item of data;let i = index" class="card{{i + 1 }}">
.....
</div>

Now I am writing css for above dynamic classes like this ->

@media (min-width: 1399px) {
  .card2,
  .card6 {
   padding-left: 0px;
   padding-right: 0px;
  }
  .card3,
  .card7 {
   padding-right: 0px;
  }
}
@media (min-width: 576px) and (max-width: 768px) {
  .card2,
  .card4,
  .card6 {
   padding-left: 0px;
 }
}

Now I have less than 8 divs but it can be above 20 and then I will be in trouble.

So Is there any shortest way for defining above css rules like card(n+1){....} ?

5 Answers 5

1

If you are using sass or less, you can use control directives, your sass code would look like :

@for $i from 0 through 20 {
  .card-#{$i} { padding-left: 0px; }
} 

Here is a running example.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you @ibenjelloun !!! Here you give static value 20...instead of that can we give here dynamic value such as length of list etc.....
The control directives are executed before runtime, on runtime only css code produced by sass will be executed. I don't think that it is possible to give the length of a javascript array to a sass control directive as an input.
0

Use your current classes as IDs, and use a common class for all.

class="card" id="card-{{ i }}"

1 Comment

If you need special styles for given cards, please explain the end goal and provide a minimal reproducible example.
0

If I understood your question correctly, you can see my implementation for inserting classes dynamically here.

Basically you need to create a function that returns your class's name.

If what you need is to create specific CSS styles for every n+1 card element then you can try this:

card:nth-child(n+1){
  background-color: red;
}

Comments

0

If there is no pattern and therefore you cannot use sass, then you could look at [ngStyle] directive. Example of using:

<div *ngFor="let item of data;let i = index" class="card-{{i + 1 }}" [ngStyle]="calcStyle(item, i)">
.....
</div>

and return your expected style in your .ts file, like:

calcStyle(item, i){
  if (i==6){
     return {
       'padding-left': '0px;',
       'padding-right': '0px;'
       }
    }
 }

But i think, that you should try to avoid setting padding by index of current item. Instead try to use some grid system.

Comments

0

Easy peasy. Just change the value type of your class attribute to accept javascript instead string and add the concatenation.

<div
  style="max-height: 450px;"
  *ngFor="let item of data;let i = index"
  [class]="'card' + (i + 1)">
  .....
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.