CSS网格问题与背景和包装

时间:2019-03-23 00:53:32

标签: html css css3 css-grid

我有一个网格,我希望它可以像它那样工作,只是当我只希望有1个或2个单元格自动调整大小,而不是由3个组成的集合时,最后一个保留其单元格空间。当我将repeat(3,100px)值设置为自动适应时,它将终止列布局。 我仍然需要将其包裹在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>

1 个答案:

答案 0 :(得分:0)

因此,您要使 grid-container 的大小自动调整为少于3个项目,然后将其包装-这是使用隐式网格的解决方案。

  • 使用单列嵌入式网格容器

    display: inline-grid;
    grid-template-columns: 100px;
    grid-template-rows: 100px;
    
  • 使用以下方法将隐式网格的大小设置为100px x 100px:

    grid-auto-columns: 100px;
    grid-auto-rows: 100px;
    
  • 使用grid-column: 2将您的第二项放入第二行;和您的第三项使用grid-column: 3

  • 第三行

现在您将拥有一个3 x 3的网格-请参见下面的演示

* {
  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>