【问题标题】:flexbox with 5 boxes带有 5 个盒子的 flexbox
【发布时间】:2021-04-26 05:17:43
【问题描述】:

我正在尝试使用 flexbox 实现以下布局

我已经在网格中尝试过它按预期工作。但它在 IE 中不起作用。所以我想切换到 flexbox 所以下面是我的 grid 和 flex 版本。

网格代码如下:

 //this is with grid css
.grid_container {
  width: 65%;
  margin: 10px auto;
  display: grid;
  grid-gap: 1.5rem;
  grid-template-columns: 1fr 1fr 1fr;
  max-width: 1100px;
  display: -ms-grid;
  -ms-grid-columns: 1fr 1fr 1fr;             
  -ms-grid-row-gap:1.5rem;
  -ms-grid-column-gap:1.5rem;
}
//this is using grid css
.box-item {
  width: 100%;
  height: auto;
  position: relative;
  }
 
//inside image css
.box-item img {
  max-width: 100%;
  max-height: 100%;
  vertical-align: bottom;
}

.box-text {
  position: absolute;
  bottom: 12px;
  left: 0;
  right: 0;
  width: 100%;
  text-align: center;
  font-size: 1.3rem;
  font-weight: 400;
  color: white;
}
<div class="grid_container">
 <div class="box-item">
  <img src="https://dummyimage.com/350x275/000/fff" />
  <div class="box-text">
   dummy text
  </div>
 </div>
 <div class="box-item">
  <img src="https://dummyimage.com/350x275/000/fff" />
  <div class="box-text">
   dummy text
  </div>
 </div>
 <div class="box-item">
  <img src="https://dummyimage.com/350x275/000/fff" />
  <div class="box-text">
   dummy text
  </div>
 </div>
 <div class="box-item">
  <img src="https://dummyimage.com/350x275/000/fff" />
  <div class="box-text">
   dummy text
  </div>
 </div>
 <div class="box-item">
  <img src="https://dummyimage.com/350x275/000/fff" />
  <div class="box-text">
   dummy text
  </div>
 </div>
</div>

我正在尝试在 flexbox 中做同样的事情,但我无法实现:css 代码如下所示:

//the same class using flex property
.grid_container{
    margin: 10px auto;
    max-width: 1100px;
    width: 100%;
    height: auto;
    display: flex;
    justify-content: space-around;
    flex-flow: wrap;
}

//this is with flex
.box-item {
  width: 33%;
  height: auto;
  max-width: 33%; 
  position: relative;
}

//inside image css
.box-item img {
  max-width: 100%;
  max-height: 100%;
  vertical-align: bottom;
  min-height: 200px;
}

.box-text {
  position: absolute;
  bottom: 12px;
  left: 0;
  right: 0;
  width: 100%;
  text-align: center;
  font-size: 1.3rem;
  font-weight: 400;
  color: white;
}

//trying to add responsiveness
@media only screen and (max-width: 780px) {
.box-item {
  width: 45%;
}
}

【问题讨论】:

  • css-grid 仅在 IE 11 和 13 中得到部分支持。
  • 这能回答你的问题吗? Flexbox not working in Internet Explorer 11
  • 嗨@tacoshy,我最近意识到了这一点,所以现在我想用flexbox实现同样的目标。知道如何使用 flex 实现图像中显示的相同布局

标签: html css flexbox css-grid


【解决方案1】:

对于您的主容器

.main-container {
    display: flex;
    align-items: center;
    justify-content: space-around;
    flex-wrap: wrap;
}

然后你的项目

.item {
    flex-basis: 30%;
}

【讨论】:

  • 请在此处解释什么以及为什么你改变了。
  • 问题是如何以特定的方式在 flex 容器中放置 5 个框,在我的回答中,我展示了如何做到这一点的代码,我什么也没做,只是举了一个例子。
  • 不过,您可以解释它的每个部分的作用,以便其他人可以学习。现在它只是一个复制和粘贴答案。
  • 我认为它很容易解释,为弹性项目添加一个basis 属性并用flex-wrap包装主容器
  • you 很清楚,但如果有人搜索相同的问题,最终到这里并看到这个,可能是他/她需要更多上下文。有了解释,你的答案质量会更高。
猜你喜欢
  • 1970-01-01
  • 2021-07-24
  • 1970-01-01
  • 2016-03-31
  • 2021-10-19
  • 2014-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多