Home > database >  How to create layout with grid
How to create layout with grid

Time:03-05

I want to create layout where the last element is centered. I should do with grids, but cant center the last element

Desired view

.box {
  min-width: 50px;
  min-height:50px;
  background: red;
  margin: 5px;
}

.container {
   width: 200px;
   display: grid;
   grid-template-columns: auto auto auto;

}

.box7 {
    grid-column-start: 2;
    grid-row-start: 3;
    grid-column-end: 4;
}
<div class='container'>
  <div class='box'>1</div>
  <div class='box'>2</div>
  <div class='box'>3</div>
  <div class='box'>4</div>
  <div class='box'>5</div>
  <div class='box'>6</div>
  <div class='box box7'>7</div>
<div>

Desired view

CodePudding user response:

.box {
  min-height: 50px;
  background: red;
  margin: 5px;
  grid-column: span 2;
}

.container {
   width: 200px;
   display: grid;
   grid-template-columns: repeat(6, 1fr);

}

.box7 {
   grid-column-start: 2;
   grid-column-end: 6;
}
<div class='container'>
  <div class='box'>1</div>
  <div class='box'>2</div>
  <div class='box'>3</div>
  <div class='box'>4</div>
  <div class='box'>5</div>
  <div class='box'>6</div>
  <div class='box box7'>7</div>
<div>

You can also find multiple ways on this discussion

CodePudding user response:

I hope this approach of mine will help you:

.box {
  min-width: 50px;
  min-height:50px;
  background: red;
  margin: 5px;
}

.container {
   width: 200px;
   display: grid;  
   grid-template-rows: repeat(3, [row] auto  );
   grid-template-columns: repeat(3, [col] auto) ;
}

.subgrid {
 grid-column: col / span 3;
 grid-row: row 3;
}

.box7
{
width:66%;
margin-left:auto;
margin-right:auto;
}
<div class='container'>
  <div class='box'>1</div>
  <div class='box'>2</div>
  <div class='box'>3</div>
  <div class='box'>4</div>
  <div class='box'>5</div>
  <div class='box'>6</div>
  <div >
    <div class='box box7'>7</div>
  </div>
<div>

  • Related