CSS Flex忽略某些子元素

发布于 2025-02-14 00:30:45 字数 2219 浏览 0 评论 0原文

我对学习CS和Flex的知识很新,并且我的某些Flex容器遇到了一些困难。我将我的代码放在下面,但本质上是:Flex容器的子元素是Divs,每个元素包含一个带有下面字幕的图像。这些divs本身也是弹性元素。这些图像彼此之间的间距加载,但是字幕部分或完全被其他图像覆盖,好像它们的尺寸被忽略一样(即使它们是图像所属于的相同flex容器的一部分)。我觉得这可能有一个简单的解决方案,但我不能为自己的一生弄清楚。

这就是我所得到的所有这些:

”输出“

.subtitle {
  font-family: Helvetica;
  color: seashell;
  font-weight: bold;
  font-size: 25px;
}

.tea-sub {
  margin-top: 10px;
  margin-bottom: 10px;
}

#tea-containers {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  align-content: center;
  justify-content: space-around;
  width: 80%;
  height: auto;
  margin: 30px auto;
}

.tea {
  display: inline-flex;
  flex-direction: column;
  height: 245px;
  min-width: 300px;
  justify-content: center;
  align-items: center;
}

.tea-pic {
  height: 245px;
}
<div id='tea-containers'>

  <div id='tea1' class='tea'>
    <img src='resources/images/img-berryblitz.jpg' alt='Fall Berry Blitz Tea' class='tea-pic'>
    <h4 class='subtitle tea-sub'>Fall Berry Blitz Tea</h4>
  </div>
  <div id='tea2' class='tea'>
    <img src='resources/images/img-spiced-rum.jpg' alt='Spiced rum tea' class='tea-pic'>
    <h4 class='subtitle tea-sub'>Spiced Rum Tea</h4>
  </div>
  <div id='tea3' class='tea'>
    <img src='resources/images/img-donut.jpg' alt='donuts' class='tea-pic'>
    <h4 class='subtitle tea-sub'>Seasonal Donuts</h4>
  </div>


  <div id='tea4' class='tea'>
    <img src='resources/images/img-myrtle-ave.jpg' alt='myrtle ave tea' class='tea-pic'>
    <h4 class='subtitle tea-sub'>Myrtle Ave Tea</h4>
  </div>
  <div id='tea5' class='tea'>
    <img src='resources/images/img-bedford-bizarre.jpg' alt='bedford bizarre tea' class='tea-pic'>
    <h4 class='subtitle tea-sub'>Bedford Bizarre Tea</h4>
  </div>

</div>

I'm pretty new to learning CSS and flex, and I'm having some difficulty with some of my flex containers. I'll put my code below, but essentially: the child elements of the flex container are divs, each containing an image with a subtitle below. These divs are also flex elements themselves. The images load with the appropriate spacing between each other, but the subtitles are covered either partially or entirely by other images as if their dimensions are being ignored (even though they're a part of the same flex container that the images are). I feel like there's probably an easy solution to this but I can't for the life of me figure it out.

Here's what I get with all of that:

output

.subtitle {
  font-family: Helvetica;
  color: seashell;
  font-weight: bold;
  font-size: 25px;
}

.tea-sub {
  margin-top: 10px;
  margin-bottom: 10px;
}

#tea-containers {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  align-content: center;
  justify-content: space-around;
  width: 80%;
  height: auto;
  margin: 30px auto;
}

.tea {
  display: inline-flex;
  flex-direction: column;
  height: 245px;
  min-width: 300px;
  justify-content: center;
  align-items: center;
}

.tea-pic {
  height: 245px;
}
<div id='tea-containers'>

  <div id='tea1' class='tea'>
    <img src='resources/images/img-berryblitz.jpg' alt='Fall Berry Blitz Tea' class='tea-pic'>
    <h4 class='subtitle tea-sub'>Fall Berry Blitz Tea</h4>
  </div>
  <div id='tea2' class='tea'>
    <img src='resources/images/img-spiced-rum.jpg' alt='Spiced rum tea' class='tea-pic'>
    <h4 class='subtitle tea-sub'>Spiced Rum Tea</h4>
  </div>
  <div id='tea3' class='tea'>
    <img src='resources/images/img-donut.jpg' alt='donuts' class='tea-pic'>
    <h4 class='subtitle tea-sub'>Seasonal Donuts</h4>
  </div>


  <div id='tea4' class='tea'>
    <img src='resources/images/img-myrtle-ave.jpg' alt='myrtle ave tea' class='tea-pic'>
    <h4 class='subtitle tea-sub'>Myrtle Ave Tea</h4>
  </div>
  <div id='tea5' class='tea'>
    <img src='resources/images/img-bedford-bizarre.jpg' alt='bedford bizarre tea' class='tea-pic'>
    <h4 class='subtitle tea-sub'>Bedford Bizarre Tea</h4>
  </div>

</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然 2025-02-21 00:30:45

您的图像与它们所在的容器相同,因此它们填满了整个空间。这应该修复它:

.tea {
  height: 245px;
}

.tea-pic {
  height: 100%;
}

Your images are the same height as the container they're in, so they fill up the whole space. This should fix it:

.tea {
  height: 245px;
}

.tea-pic {
  height: 100%;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文