title: Using *ngFor in CSS Grid Layout Undesirably Displaying Everything in One Column
body: <p>I am new to programming and web dev in general. I am in the process of making a personal website.</p>
  
  <p>I want to put boxes in a grid with 4 columns, similar to what you see here: <a href="https://devpost.com/software/search?query=is%3Afeatured" rel="nofollow noreferrer">https://devpost.com/software/search?query=is%3Afeatured</a></p>
  
  <p>Each of these boxes represents an object, and I want to be able to display some of the data into the box, and the rest of data in a popup dialog when you click on the box.</p>
  
  <p>I have been playing around with the CSS grid layout, which is becoming popular these days (highly recommend this video: <a href="https://www.youtube.com/watch?v=7kVeCqQCxlk" rel="nofollow noreferrer">https://www.youtube.com/watch?v=7kVeCqQCxlk</a>).</p>
  
  <p>The thing with 4 columns work when I hardcode bunch of div elements inside a wrapper div. However, whenever I use *ngFor on the wrapper containing my array of data and feed in the data from each iteration to an inner div element, the grid layout gets destroyed, placing everything into one column.</p>
  
  <p>When I manually feed in the data using multiple div elements (item2 here), it works as desired:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.wrapper {
      margin-left: 1em;
      margin-right: 1em;
      display: grid;
      grid-template-rows: auto;
      grid-template-columns: repeat(4, 1fr);
      grid-row-gap: 1em;
      grid-column-gap: 1em;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="wrapper"&gt;
  
      &lt;div class="item2" style="background-color: deepskyblue;"&gt;
          &lt;img class="img-responsive img-rounded" src="assets/Logo/square_filler.png" alt="pic-test" &gt;
          &lt;p&gt;more text here.&lt;/p&gt;
      &lt;/div&gt;
  
      &lt;div class="item2" style="background-color: deepskyblue;"&gt;
          &lt;img class="img-responsive img-rounded" src="assets/Logo/square_filler.png" alt="pic-test" &gt;
          &lt;p&gt;more text here.&lt;/p&gt;
      &lt;/div&gt;
  
      &lt;div class="item2" style="background-color: deepskyblue;"&gt;
          &lt;img class="img-responsive img-rounded" src="assets/Logo/square_filler.png" alt="pic-test" &gt;
          &lt;p&gt;more text here.&lt;/p&gt;
      &lt;/div&gt;
  
      &lt;div class="item2" style="background-color: deepskyblue;"&gt;
          &lt;img class="img-responsive img-rounded" src="assets/Logo/square_filler.png" alt="pic-test" &gt;
          &lt;p&gt;more text here.&lt;/p&gt;
      &lt;/div&gt;
  
      &lt;div class="item2" style="background-color: deepskyblue;"&gt;
          &lt;img class="img-responsive img-rounded" src="assets/Logo/square_filler.png" alt="pic-test" &gt;
          &lt;p&gt;more text here.&lt;/p&gt;
      &lt;/div&gt;
      
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Using *ngFor... it runs for the length of the array "stickyNotes," but stacks the boxes only below each row.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="wrapper" *ngFor="let s of stickyNotes; let i = index" &gt;
      &lt;div class="item2" style="background-color: deepskyblue;"&gt;
          &lt;img class="img-responsive img-rounded" src="assets/Logo/square_filler.png" alt="pic-test" &gt;
          &lt;p&gt;more text here.&lt;/p&gt;
          &lt;p&gt;{{s.title}}&lt;/p&gt;
      &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>My hack around this was... have this div element 4 times with i incrementing by 1 on *ngIf (i+1, i+2, etc.). Problem here is that when a new row starts, grid-row-gap in CSS is ignored.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="item2" style="background-color: deepskyblue;" *ngIf="i &lt; stickyNotes.length &amp;&amp; i%4 === 0"&gt;
        &lt;img class="img-responsive img-rounded" src="assets/Logo/square_filler.png" alt="pic-test" &gt;
        &lt;p&gt;more text here.&lt;/p&gt;
        &lt;p&gt;{{stickyNotes[i].title}}&lt;/p&gt;
  &lt;/div&gt;
  
  &lt;div class="item2" style="background-color: deepskyblue;" *ngIf="i+1 &lt; stickyNotes.length &amp;&amp; i%4 === 0"&gt;
        &lt;img class="img-responsive img-rounded" src="assets/Logo/square_filler.png" alt="pic-test" &gt;
        &lt;p&gt;more text here.&lt;/p&gt;
        &lt;p&gt;{{stickyNotes[i+1].title}}&lt;/p&gt;
  &lt;/div&gt;
  
  &lt;!--2 more times until i+3--&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I am wondering if there is a way to create a custom iterable directive without destroying the grid property and without having the item2 class coded 4 times (or any other methods).</p>
  
  <p>I have tried bootstrap row and col attributes, but with 4 columns, the responsiveness wasn't as nice as the grid layout.</p>
  
  <p>Any help or suggestion would be appreciated!</p>