Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
964 views
in Technique[技术] by (71.8m points)

html - css grid problem with background and wrapping

I have a grid where I want it to work somewhat like it is except when there are only 1 or 2 cells I want it to auto size and not be a set of 3 with the last one holding its cell space. When I set repeat(3, 100px) value to auto-fit then it kills the column layout. I still need it to wrap at 3.

.parent {
  background: #f4f4f5;
  font-size:2em;
  border: 1px solid black;
  padding: 20px 0;
  text-align: center;
  width: 100%;
}
.parent .wrapper {
  display: inline-grid;
  grid-template-columns: repeat(3, 100px);
  box-shadow: 1px 0px 5px 0px rgba(0,0,0,0.75);
  grid-auto-flow: row;
  background-color: white;
}

.cell {
  border: 1px solid gray;
  background-color: white;
}

.cell .inner {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100px;
  height: 100px;
  text-align: center;
}
<div class="parent">
  <div class="wrapper">
    <div class="cell">
      <div class="inner">Cell 1</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 2</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 3</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 4</div>
    </div>
  </div> 
</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So you want to make the grid-container auto-sized less that 3 items and wrap after that - here's a solution that uses implicit grids.

  • use a single column inline grid container:

    display: inline-grid;
    grid-template-columns: 100px;
    grid-template-rows: 100px;
    
  • size your implicit grids as 100px x 100px using:

    grid-auto-columns: 100px;
    grid-auto-rows: 100px;
    
  • place your second item to the second row using grid-column: 2; and your third item to the third row using grid-column: 3

Now you will have a 3 x 3 grid - see demo below:

* {
  box-sizing: border-box;
}

.parent {
  background: #f4f4f5;
  font-size: 2em;
  border: 1px solid black;
  padding: 20px 0;
  text-align: center;
  width: 100%;
}

.parent .wrapper {
  display: inline-grid;
  grid-template-columns: 100px; /* one column layout */
  grid-template-rows: 100px; /* sizes the rows */
  grid-auto-columns: 100px; /* sizing columns of implicit grid */
  grid-auto-rows: 100px; /* sizing rows of implicit grid */
  box-shadow: 1px 0px 5px 0px rgba(0, 0, 0, 0.75);
  grid-auto-flow: row;
  background-color: white;
}

.cell:nth-child(2) {
  grid-column: 2; /* places it in the second column */
}

.cell:nth-child(3) { 
  grid-column: 3; /* places it in the third column */
}

.cell {
  border: 1px solid gray;
  background-color: white;
}

.cell .inner {
  display: flex;
  justify-content: center;
  align-items: center;
  /* width: 100px; */
  /* height: 100px; */
  height: 100%; /* added */
  text-align: center;
}
<div class="parent">
  <div class="wrapper">
    <div class="cell">
      <div class="inner">Cell 1</div>
    </div>
  </div>
</div>

<div class="parent">
  <div class="wrapper">
    <div class="cell">
      <div class="inner">Cell 1</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 2</div>
    </div>
  </div>
</div>

<div class="parent">
  <div class="wrapper">
    <div class="cell">
      <div class="inner">Cell 1</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 2</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 3</div>
    </div>
  </div>
</div>

<div class="parent">
  <div class="wrapper">
    <div class="cell">
      <div class="inner">Cell 1</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 2</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 3</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 4</div>
    </div>
  </div>
</div>

<div class="parent">
  <div class="wrapper">
    <div class="cell">
      <div class="inner">Cell 1</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 2</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 3</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 4</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 5</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 6</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 7</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 8</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 9</div>
    </div>
    <div class="cell">
      <div class="inner">Cell 10</div>
    </div>
  </div>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...