【问题标题】:Positioning text in flexbox under pictures在图片下的flexbox中定位文本
【发布时间】:2023-03-13 01:21:02
【问题描述】:

我的文本出现问题,我根本看不到它,或者它不像我认为它会出现在弹性框中一样。我现在在 flexbox 中有三个图像,但我想在每个图像下放置小的“标题”(不是在 p 元素中,紫色,但我想将它放在白色上,就在紫色框(p元素)。我认为通过添加一个子元素,该元素至少会与它上面的元素垂直对齐,但我想我错了。有人能帮忙吗?另一条信息真的是我的图像是 250 像素,但我想适应 sn-p,所以我将其设为 50 像素,但这可能无关紧要。

#footer {
  display: flex;
  height: 130px;
  width: 100%;
  background-color: #862d59;
  clear: both;
}

#footer, #wrapper:after{
  height: 130px;
}

.wrap {
  margin: 0 auto;
  width: 100%;
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
}

.sub {
  padding: 12px;
  width: 32%;
  height: 100px;
  color: white;
  border-right: solid white 1px;
}

.sub:last-child {
  border: 0px;
}

html {
  height: 100%;
}

body {
  height: 100%;
  margin:0;
  font-family: courier;
  font-size: 22px;
  color: white;
}

#wrapper {
  position: relative;
  margin-left: auto;
  margin-right: auto;
  width: 85%;
  min-height: 100%;
  margin-top: -130px;
}

#inner {
  position:absolute;
  display:flex;
  flex-wrap: wrap;
  height: 600px;
  top:50%;
  align-items: center;
  justify-content: space-between;
  margin-top: -300px;
  align-content: center;
  width: 100%;
}

#inner p {
  background-color: #26004d;
  padding: 60px;
  border-radius: 9px;
}

#inner img {
  border-radius: 8px;
}
<div id="wrapper">
  <div id="inner">
    <p><img src="cat1.jpeg" alt="Picture of a cat" width="50" height="50"></p>
    <p><img src="dog1.jpg" alt="Picture of a cat" width="50" height="50"></p>
    <p><img src="park.jpg" alt="Picture of a cat" width="50" height="50"></p>
  </div>
</div>
<div id="footer">
  <div class="wrap">
    <div class="sub"></div>
    <div class="sub"></div>
    <div class="sub"></div>
  </div>
</div>

【问题讨论】:

  • 添加您要完成的任务的屏幕截图。目前,它有点不清楚(尤其是看 sn-p)。您还可以使用占位符图像使您的布局在 sn-p 中匹配:placehold.it
  • 我想放置文本并将其置于每个 p 元素下方(每个紫色框下方)。所以文本将在紫色之外,但在每个 p 元素下方居中。就像它完全在紫色之外,但在每个盒子的正下方。像这样snag.gy/LtCew1.jpg

标签: html css text flexbox positioning


【解决方案1】:

没有额外的信息/图片,这是我能想出的解决方案。如果您想将每个图像/标题组合在一起,请将它们包装在另一个父级 div 中。然后只需在其下方添加标题,这是一个block 元素,应该按照预期在图像下方流动。片段如下。

#footer {
  display: flex;
  height: 130px;
  width: 100%;
  background-color: #862d59;
  clear: both;
}

#footer, #wrapper:after{
  height: 130px;
}

.wrap {
  margin: 0 auto;
  width: 100%;
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
}

.sub {
  padding: 12px;
  width: 32%;
  height: 100px;
  color: white;
  border-right: solid white 1px;
}

.sub:last-child {
  border: 0px;
}

html {
  height: 100%;
}

body {
  height: 100%;
  margin:0;
  font-family: courier;
  font-size: 22px;
  color: white;
}

#wrapper {
  position: relative;
  margin-left: auto;
  margin-right: auto;
  width: 85%;
  min-height: 100%;
}

#inner {
  position:absolute;
  display:flex;
  flex-wrap: wrap;
  height: 600px;
  top:50%;
  align-items: center;
  justify-content: space-between;
  margin-top: -300px;
  align-content: center;
  width: 100%;
}

#inner p {
  background-color: #26004d;
  padding: 60px;
  border-radius: 9px;
}

#inner p.caption {
  color: #000;
  background-color: transparent;
  border-radius: 0;
}

#inner img {
  display: block;
  border-radius: 8px;
}
<div id="wrapper">
  <div id="inner">
    <div class="image-wrapper">
      <p>
        <img src="http://placehold.it/100x100" alt="Picture of a cat">
      </p>
      <p class="caption">Caption</p>
    </div>
    <div class="image-wrapper">
      <p>
        <img src="http://placehold.it/100x100" alt="Picture of a cat">
      </p>
      <p class="caption">Caption</p>
    </div>
    <div class="image-wrapper">
      <p>
        <img src="http://placehold.it/100x100" alt="Picture of a cat">
      </p>
      <p class="caption">Caption</p>
    </div>
  </div>
</div>
<div id="footer">
  <div class="wrap">
    <div class="sub"></div>
    <div class="sub"></div>
    <div class="sub"></div>
  </div>
</div>

如果您有任何问题,或者这不符合您的描述,请告诉我。

【讨论】:

  • 谢谢。不幸的是,它一定破坏了 flexbox,即使高度应该能够容纳,但不确定它为什么会这样做。但这是我得到的:snag.gy/fPJXdz.jpg 所以我不太确定如何将我的 flexbox 保持在一个整体并添加标题。
  • 嗯,奇怪。如果您不是 100% 需要 flexbox(如果您没有在响应式屏幕上重新排列照片顺序),您可以将所有三个项目放在 ul 元素中作为 li 子元素和 display: inline-block 并且可能避免您在那里看到的所有问题。 Flexbox 目前有点不稳定,有时使用一种经过验证的方法更容易,也支持更多的跨浏览器。
  • 有点好笑,但由于没有个人消息系统,我想我会在这里问你。如何正确缩进代码 sn-p 中的所有内容?
猜你喜欢
  • 2017-02-07
  • 2022-11-17
  • 2023-03-30
  • 1970-01-01
  • 2022-11-21
  • 1970-01-01
  • 1970-01-01
  • 2022-07-21
  • 1970-01-01
相关资源
最近更新 更多