Home > Software design >  Grid property instead of grid-row & grid-column
Grid property instead of grid-row & grid-column

Time:01-19

According to MDN Web Docs, the grid CSS property is a shorthand property that sets all of the explicit and implicit grid properties in a single declaration. I wonder how we can use it instead of grid-row & grid-column in the following sample code so it can be further shortened:

.grid-container {
  display: grid;
  gap: 10px;
  background-color: #2196F3;
  padding: 10px;
}

.grid-container div {
  background-color: rgba(255, 255, 255, 0.8);
  text-align: center;
  padding: 20px 0;
  font-size: 30px;
}

.item3 {
  grid-column: 2;
  grid-row: 1 / 3;
}
<div >
  <div >1</div>
  <div >2</div>
  <div >3</div>
</div>

CodePudding user response:

You are looking for grid-area. grid is for the container

.grid-container {
  display: grid;
  gap: 10px;
  background-color: #2196F3;
  padding: 10px;
}

.grid-container div {
  background-color: rgba(255, 255, 255, 0.8);
  text-align: center;
  padding: 20px 0;
  font-size: 30px;
}

.item3 {
  /*grid-column: 2;
  grid-row: 1 / 3;*/
  grid-area: 1 / 2 / 3 /auto; 
  /* row-start / column-start / row-end / column-end */
}
<div >
  <div >1</div>
  <div >2</div>
  <div >3</div>
</div>

CodePudding user response:

The grid property is a shorthand for the following:

  • grid-auto-columns
  • grid-auto-flow
  • grid-auto-rows
  • grid-template-areas
  • grid-template-columns
  • grid-template-rows

See https://developer.mozilla.org/en-US/docs/Web/CSS/grid This property is used to define the grid itself, so it's something you would add to grid-container rather than to its children.

You could shorten your statement in item3 using grid-area, which is shorthand for:

  • grid-row-start

  • grid-column-start

  • grid-row-end

  • grid-column-end

    So item3 could be styled as:

    .item3 { grid-area: 1 / 2 / 3; }

CodePudding user response:

No, that is not the purpose of it. The shorthand replaces grid-template-columns and grid-template-rows on the grid container.

  •  Tags:  
  • Related