Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling a NgFor horizontally

Tags:

css

angular

How to horizontally display each <ng-container> of an *ngFor directive.

I have:

<div *ngIf="hasFinished">
    <ng-container *ngFor="let champ of champStats">
      <h2> {{champ[1].name}} </h2>
      <p> Level: {{champ[1].Level}} </p>
      <p> XP: {{champ[1].XP}} </p>
      <p> Time lost: {{champ[1].CharacterTimePlayed}} </p>
      <p> Wins: {{champ[1].CharacterWins}} </p>
      <p> Loosses: {{champ[1].CharacterLosses}} </p>
      <p> XP : {{champ.CharacterRanked2v2Wins}} </p>
      <p> XP : {{champ.CharacterRanked2v2Losses}} </p>

    </ng-container>
  </div>

I'm using bootstrap, I want that each element take a column of 4 (like the 3 first ones are in a row, the 3 after another row etc...)

Just like this:

<div class="row">
    <ng-container class="col-lg-4> ...
    </ng-container>
    <ng-container class="col-lg-4> ...
    </ng-container>
    <ng-container class="col-lg-4> ...
    </ng-container>
</div>
<div class="row">
    <ng-container class="col-lg-4> ...
    </ng-container>
    //...
</div>

EDIT: The other problem is : if there is 7 elements, how to manage the last one? (for example)

like image 954
Izio Avatar asked Jan 28 '26 20:01

Izio


2 Answers

You can achieve this through CSS3 flex

HTML:

  <div class="horizontal" *ngIf="hasFinished">
    <ng-container *ngFor="let champ of champStats">
      <p> Name: {{champ.name}} </p>
      <p> Level: {{champ.Level}} </p>
      <p> XP: {{champ.XP}} </p>
      <p> Time lost: {{champ.CharacterTimePlayed}} </p>
      <p> Wins: {{champ.CharacterWins}} </p>
      <p> Loosses: {{champ.CharacterLosses}} </p>
      <p> XP : {{champ.CharacterRanked2v2Wins}} </p>
      <p> XP : {{champ.CharacterRanked2v2Losses}} </p>

    </ng-container>
  </div>

CSS:

.horizontal {
    display: flex;
    justify-content: space-between;
    flex-wrap:wrap;
}

p {
    display: inline-block;
    flex-grow: 1;
    height:100px;    
    width: calc(100% * (1/4) - 10px - 1px)
}
like image 55
Mano Avatar answered Jan 30 '26 09:01

Mano


You can use display: flex in CSS as:

HTML

<div class="horizontal" *ngIf="hasFinished">
    <div *ngFor="let champ of champStats">
      <h2> {{champ[1].name}} </h2>
      <p> Level: {{champ[1].Level}} </p>
      <p> XP: {{champ[1].XP}} </p>
      <p> Time lost: {{champ[1].CharacterTimePlayed}} </p>
      <p> Wins: {{champ[1].CharacterWins}} </p>
      <p> Loosses: {{champ[1].CharacterLosses}} </p>
      <p> XP : {{champ.CharacterRanked2v2Wins}} </p>
      <p> XP : {{champ.CharacterRanked2v2Losses}} </p>
    </div>
</div>

CSS

.horizontal {
    display: flex;
    justify-content: space-between;
}
like image 28
Anshuman Jaiswal Avatar answered Jan 30 '26 08:01

Anshuman Jaiswal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!