skip to Main Content

I have a grid, and basically I need to be able to define the positions of the items inside of the container code, to make it clearer, here’s how my current code looks like:

   <style>
.angry-grid {
   display: grid; 

   grid-template-rows: 1fr 1fr 1fr;
   grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
   
   gap: 0px;
   height: 100%;
   
}
  
#item-0 {

   background-color: #689779; 
   grid-row-start: 1;
   grid-column-start: 3;

   grid-row-end: 2;
   grid-column-end: 4;
   
}
#item-1 {

   background-color: #9D7F5B; 
   grid-row-start: 2;
   grid-column-start: 5;

   grid-row-end: 3;
   grid-column-end: 6;
   
}
#item-2 {

   background-color: #C756BB; 
   grid-row-start: 2;
   grid-column-start: 1;

   grid-row-end: 3;
   grid-column-end: 2;
   
}
#item-3 {

   background-color: #BDE79B; 
   grid-row-start: 3;
   grid-column-start: 4;

   grid-row-end: 4;
   grid-column-end: 5;
   
}
#item-4 {

   background-color: #5F97A5; 
   grid-row-start: 3;
   grid-column-start: 2;

   grid-row-end: 4;
   grid-column-end: 3;
   
}
</style>

<div class="angry-grid">
  <div id="item-0">&nbsp;</div>
  <div id="item-1">&nbsp;</div>
  <div id="item-2">&nbsp;</div>
  <div id="item-3">&nbsp;</div>
  <div id="item-4">&nbsp;</div>
</div>

I need to define the items’ positions inside the container code (myGrid {…}), I hope it’s clear enough.

In this example, it’s should look like this:
enter image description here

2

Answers


  1. You can’t. CSS uses selectors to select elements, and no part of CSS uses selectors are values inside a rule-set.

    The closest you can come would be to use grid-template-areas to name regions of the grid. You would still need to write a selector for each element to you want to place in those areas.

    Login or Signup to reply.
  2. As quentin stated, your best bet is to use grid-template-areas to define the positioning of the children inside the grid layout.
    There are multiple resources online to help you create your desired layout.

    It took me about 30 seconds to create this layout with this tool:
    https://cssgrid-generator.netlify.app/

    For more advanced layouts, I usually use this:
    https://grid.layoutit.com/

    .parent {
      display: grid;
      grid-template-columns: repeat(5, 1fr);
      grid-template-rows: repeat(3, 1fr);
      grid-column-gap: 0px;
      grid-row-gap: 0px;
      max-width: 500px;
    }
    
    .parent div {
      height: 100px;
      width: 100%;
    }
    
    .div1 { grid-area: 1 / 3 / 2 / 4; background-color: red; }
    .div2 { grid-area: 2 / 1 / 3 / 2; background-color: green; }
    .div3 { grid-area: 3 / 2 / 4 / 3; background-color: violet; }
    .div4 { grid-area: 3 / 4 / 4 / 5; background-color: tomato; }
    .div5 { grid-area: 2 / 5 / 3 / 6; background-color: yellow; }
    <div class="parent">
      <div class="div1"></div>
      <div class="div2"></div>
      <div class="div3"></div>
      <div class="div4"></div>
      <div class="div5"></div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search