带有 Flex-Wrap 的 Flexbox 会留下不需要的水平空白空间

问题描述 投票:0回答:1

在此示例中: https://jsbin.com/sehuqowuca/edit?html,css,输出

如果我将视图大小调整到

508px
334px
之间,则会留下水平空间,但我无法填充它,因为
item2
高度占据了整行

如何摆脱这个水平空间并让

item-3
填充该间隙,如下所示:https://imgur.com/a/x2EUmg3, 我可以用
flex-box
做这样的事情吗?或者我需要使用
grid
布局,如果是这样,我将如何使用
grid
实现它?

.flex-container{
  border:1px solid red;
  border-radius:5px;
  padding:1rem;
  display:flex;
  align-items:flex-start;
  justify-content:flex-start;
  gap:4rem;
  flex-wrap:wrap;
}
.flex-item{
  border:1px solid lightblue;
  padding: .5rem;
  border-radius:5px
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div class="flex-container">
  <div class="flex-item item-1">
    <h1>item1</h1>
    <h1>item1</h1>
 
  </div>
    <div class="flex-item item-2">
    <h1>item2</h1>
    <h1>item2</h1>
    <h1>item2</h1>
    <h1>item2</h1>
    <h1>item2</h1>
    <h1>item2</h1>
      
  </div>
    <div class="flex-item item-3">
    <h1>item3</h1>
    <h1>item3</h1>
    <h1>item3</h1>
    <h1>item3</h1>
    <h1>item3</h1>
    <h1>item3</h1>
  </div>
</div>
</body>
</html>

html css flexbox grid
1个回答
0
投票

无需使用网格问题是由于

align-items:flex-start;
。删除它或使用
align-items: stretch;
问题将得到解决。下面是工作示例,如果我错过了什么,请分享。

.flex-container {
  border: 1px solid red;
  border-radius: 5px;
  padding: 1rem;
  display: flex;
  align-items: stretch;
  justify-content: flex-start;
  gap: 4rem;
  flex-wrap: wrap;
}

.flex-item {
  border: 1px solid lightblue;
  padding: .5rem;
  border-radius: 5px
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>

<body>
  <div class="flex-container">
    <div class="flex-item item-1">
      <h1>item1</h1>
      <h1>item1</h1>

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

    </div>
    <div class="flex-item item-3">
      <h1>item3</h1>
      <h1>item3</h1>
      <h1>item3</h1>
      <h1>item3</h1>
      <h1>item3</h1>
      <h1>item3</h1>
    </div>
  </div>
</body>

</html>

© www.soinside.com 2019 - 2024. All rights reserved.