I have a grid layout for a dashboard I am building. I am trying to achieve the following:
I am curious if there is a pure CSS method for ensuring the row stays complete or if this will require javascript. Either answer will suffice but I will favor a pure css solution.
My code so far:
.grid-4-2-1 {
  display: grid;
  grid-template-areas: "grid-child-1 grid-child-2 grid-child-3 grid-child-4";
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-gap: 20px;
  margin: 20px;
}
.grid-child {
  position: relative;
  align-items: center;
  justify-content: space-between;
  padding-top: 100%;
  background-color: #fff;
  position: relative;
}
.grid-child .grid-content {
  position: absolute;
  right: 0px;
  top: 0px;
  height: 100%;
  width: 100%;
}
@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
  /* Styles */
}
/* iPads (portrait and landscape) ----------- */
@media only screen and (max-width: 768px) {
  .grid-4-2-1 {
    grid-template-areas: "grid-child-1 grid-child-2 grid-child-3 grid-child-4";
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-gap: 20px;
    margin: 20px;
  }
}
/* Smartphones (portrait) ----------- */
@media only screen and (max-width: 480px) {}
<div class="grid-4-2-1">
  <grid-child-1 class="grid-child">
    <div class="grid-content">Overview</div>
  </grid-child-1>
  <grid-child-2 class="grid-child">
    <div class="grid-content">Overview</div>
  </grid-child-2>
  <grid-child-3 class="grid-child">
    <div class="grid-content">Overview</div>
  </grid-child-3>
  <grid-child-4 class="grid-child">
    <div class="grid-content">Overview</div>
  </grid-child-4>
</div>
Grid items have an initial size of min-width: auto and min-height: auto . You can override this behavior by setting grid items to min-width: 0 , min-height: 0 or overflow with any value other than visible .
Just use width: 100% and height: 100% in the CSS class of the item you want to fill the grid. Join a max-width property and a max-height property if you don't want a grid item inside a grid container to grow more than some size.
You could use pure CSS to create a grid system by using the display grid and display grid-auto-columns attriubutes. link for more reference (https://www.w3schools.com/cssref/pr_grid-auto-columns.asp)
.box {
  display: grid;
  grid-auto-flow:columns;
  grid-auto-columns:1fr;
  counter-reset: divs;
  margin: 10px;
  border:1px solid;
}
.box div  {
  border:1px solid blue;
}
.box div:before {
  counter-increment: divs;
  content: counter(divs);
}
.box div:nth-child(1):nth-last-child(2) ~ *,
.box div:nth-child(2):nth-last-child(3) ~ *,
.box div:nth-child(3):nth-last-child(4) ~ *,
.box div:nth-child(4):nth-last-child(5) ~ *,
.box div:nth-child(5):nth-last-child(6) ~ *,
.box div:nth-child(6):nth-last-child(7) ~ *
{
   grid-row:2;
}
<div class="box">
  <div></div>
  <div></div>  
  <div></div>
</div>
<div class="box">
  <div></div>
  <div></div>
</div>
<div class="box">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>
<div class="box">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>
<div class="box">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>
<div class="box">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div> 
  <div></div> 
</div>
<div class="box">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div> 
  <div></div> 
  <div></div> 
  <div></div> 
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With