Search code examples
cssvue.jscss-grid

v-for classes rendered over each other


I've got an array of objects I want to iterate over. Each object is displayed in it's own div. In this particular list are only three objects so I could just place them manually with grid-row in css-grid, but for another page I possibly need hundreds.

Currently, the three classes are rendered on top of each other in stead of under each other.

    <div class="sidebar-last-three" v-for="patient in this.lastThree">
        <div class="patient-entry">
            <span class="patient-name">{{patient.value}}</span>
            <span class="patient-date">{{patient.key}}</span>
            <span class="dots">...</span>
            <span class="line"></span>
        </div>
    </div>

This is the css I use.

.sidebar-last-three{
    display: grid;
    grid-row: 7/30;
    grid-column: 1/11;
    grid-template-rows: repeat(30, 1fr);
    grid-template-columns: repeat(5, 1fr);
}
.patient-entry{
    display: grid;
    grid-column: 1/6;
    grid-row: auto;
    grid-template-columns: repeat(15, 1fr);
    grid-template-rows: repeat(15, 1fr);
}

How it currently is

How I want it to be

I guess it has something to do with using v-for to create the classes, since normally with css-grid the child divs automatically go into rows and columns. Thanks in advance!


Solution

  • Styling you need:

    .sidebar {
      display: grid;
      position: fixed;
      grid-template-columns: repeat(10, 1 fr);
      grid-template-rows: repeat(10, 1 fr);
      grid-column: 1;
      width: 16.8vw;
      height: 100%;
      box-shadow: 0 5px 10px 0 rgba(0, 0, 0, 0.3);
      background: #26292c;
      z-index: 1; /* Stay on top */
      top: 0; /* Stay at the top */
      left: 0;
    }
    .patient-name{
      font-size: 16px;
      color: #ffffff;
      grid-column: 3/15;
      grid-row: 1/2;
    }
    .patient-date{
      font-size: 14px;
      color: #98acc1;
      grid-column: 3/15;
      grid-row: 2/3;
    }
    .line{
      background-color: #424a51;
      width: 100%;
      height: 1px;
      grid-column: 1/16;
      grid-row: 5;
    }
    .dots{
      justify-self: center;
      font-size: 16px;
      align-self: center;
      grid-column: 14/15;
      grid-row: 1/2;
      color: #ffffff;
    }
    .sidebar-last-three{
      display: grid;
    }
    .patient-entry{
      display: grid;
      grid-column: auto;
      grid-row: auto;
    }
    

    I've moved your example to codepen.

    Try if this works for you. Here is working example: