小编典典

带有Flexbox的CSS正方形网格

css

我正在尝试创建一个自适应的正方形网格。正方形应调整大小以适合视口的宽度。更改视口高度时,正方形不应调整大小。

我了解了如何调整每个正方形的宽度,但是我不知道如何在视口宽度改变时使元素变为正方形以及如何缩放其高度。

在小提琴下面的示例中,七个正方形应始终水平放置,并且应按正方形缩放。我不在乎有多少行可见。

<body>
<div class="flex-container">
    <div class="flex-item">1</div>
    <div class="flex-item">2</div>
    <div class="flex-item">3</div>
    <div class="flex-item">4</div>
    <div class="flex-item">5</div>
    <div class="flex-item">6</div>
    <div class="flex-item">7</div>
</div>
<div class="flex-container">
    <div class="flex-item">1</div>
    <div class="flex-item">2</div>
    <div class="flex-item">3</div>
    <div class="flex-item">4</div>
    <div class="flex-item">5</div>
    <div class="flex-item">6</div>
    <div class="flex-item">7</div>
</div>
<div class="flex-container">
    <div class="flex-item">1</div>
    <div class="flex-item">2</div>
    <div class="flex-item">3</div>
    <div class="flex-item">4</div>
    <div class="flex-item">5</div>
    <div class="flex-item">6</div>
    <div class="flex-item">7</div>
</div>
<div class="flex-container">
    <div class="flex-item">1</div>
    <div class="flex-item">2</div>
    <div class="flex-item">3</div>
    <div class="flex-item">4</div>
    <div class="flex-item">5</div>
    <div class="flex-item">6</div>
    <div class="flex-item">7</div>
</div>
<div class="flex-container">
    <div class="flex-item">1</div>
    <div class="flex-item">2</div>
    <div class="flex-item">3</div>
    <div class="flex-item">4</div>
    <div class="flex-item">5</div>
    <div class="flex-item">6</div>
    <div class="flex-item">7</div>
</div>
<div class="flex-container">
    <div class="flex-item">1</div>
    <div class="flex-item">2</div>
    <div class="flex-item">3</div>
    <div class="flex-item">4</div>
    <div class="flex-item">5</div>
    <div class="flex-item">6</div>
    <div class="flex-item">7</div>
</div>
<div class="flex-container">
    <div class="flex-item">1</div>
    <div class="flex-item">2</div>
    <div class="flex-item">3</div>
    <div class="flex-item">4</div>
    <div class="flex-item">5</div>
    <div class="flex-item">6</div>
    <div class="flex-item">7</div>
</div>



.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row;
  justify-content: space-around;
  height: 50px;
  line-height:30px;
}

.flex-item {
  background: tomato;
  margin: 5px;
  color: white;
  font-weight: bold;
  font-size: 1.5em;
  text-align: center;
  flex: 1 0 0px;
  height: auto;
}

阅读 380

收藏
2020-05-16

共1个答案

小编典典

您不应设置任何大小。您可以使用额外的元素或带有%的垂直填充的伪元素。这将允许您使用width作为参考:显示以下内容的代码段:

.flex-container {

    padding: 0;

    margin: 0;

    list-style: none;

    display: -webkit-box;

    display: -moz-box;

    display: -ms-flexbox;

    display: -webkit-flex;

    display: flex;

    -webkit-flex-flow: row;

    justify-content: space-around;



    line-height:30px;

}

.flex-item {

    background: tomato;

    margin: 5px;

    color: white;

    font-weight: bold;

    font-size: 1.5em;

    text-align: center;

    flex: 1 0 auto;

    height:auto;

}

.flex-item:before {

    content:'';

    float:left;

    padding-top:100%;

}


<body>

    <div class="flex-container">

        <div class="flex-item">1</div>

        <div class="flex-item">2</div>

        <div class="flex-item">3</div>

        <div class="flex-item">4</div>

        <div class="flex-item">5</div>

        <div class="flex-item">6</div>

        <div class="flex-item">7</div>

    </div>

    <div class="flex-container">

        <div class="flex-item">1</div>

        <div class="flex-item">2</div>

        <div class="flex-item">3</div>

        <div class="flex-item">4</div>

        <div class="flex-item">5</div>

        <div class="flex-item">6</div>

        <div class="flex-item">7</div>

    </div>

    <div class="flex-container">

        <div class="flex-item">1</div>

        <div class="flex-item">2</div>

        <div class="flex-item">3</div>

        <div class="flex-item">4</div>

        <div class="flex-item">5</div>

        <div class="flex-item">6</div>

        <div class="flex-item">7</div>

    </div>

    <div class="flex-container">

        <div class="flex-item">1</div>

        <div class="flex-item">2</div>

        <div class="flex-item">3</div>

        <div class="flex-item">4</div>

        <div class="flex-item">5</div>

        <div class="flex-item">6</div>

        <div class="flex-item">7</div>

    </div>

    <div class="flex-container">

        <div class="flex-item">1</div>

        <div class="flex-item">2</div>

        <div class="flex-item">3</div>

        <div class="flex-item">4</div>

        <div class="flex-item">5</div>

        <div class="flex-item">6</div>

        <div class="flex-item">7</div>

    </div>

    <div class="flex-container">

        <div class="flex-item">1</div>

        <div class="flex-item">2</div>

        <div class="flex-item">3</div>

        <div class="flex-item">4</div>

        <div class="flex-item">5</div>

        <div class="flex-item">6</div>

        <div class="flex-item">7</div>

    </div>

    <div class="flex-container">

        <div class="flex-item">1</div>

        <div class="flex-item">2</div>

        <div class="flex-item">3</div>

        <div class="flex-item">4</div>

        <div class="flex-item">5</div>

        <div class="flex-item">6</div>

        <div class="flex-item">7</div>

    </div>

</body>

一个inline-block的元素可以做太多,只是适应框的显示/行为和它的内容。这里的魔力来自于填充:50%0;(100%的垂直填充等于父级的宽度)。有关垂直边距和填充,

2020-05-16