css 不使用display属性,如何在图片下方显示文本?[已关闭]

14ifxucb  于 2022-12-30  发布在  其他
关注(0)|答案(2)|浏览(122)

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
16小时前关门了。
Improve this question
实际上,我想在一个框中的图片下面显示文本,我不想使用display属性,我还可以使用其他哪个属性?
我尝试了网格模板行属性,但它没有给出所需的外观。

bqujaahr

bqujaahr1#

你应该提供一个你尝试过的例子,最好是一个可以执行的代码片段,让我们看到。
也就是说,您所描述的内容听起来像是<figure><figcaption>元素的用途:

<figure>
  <img src="https://via.placeholder.com/300" alt="" />
  <figcaption>This is your caption</figcaption>
</figure>
brccelvz

brccelvz2#

如果元素的内容被限制为带样式的纯文本,则可以使用css策略,使用伪元素::after
考虑一下,正如注解中强调的那样,这个解决方案并没有满足可访问性方面的问题。
这只是一个尝试,只使用css来达到给定的结果,但打破了一些规则。
我将把它留在这里,告诉你如何不这样做。

.gallery{
  display: flex;
  gap: 1em;
}

#target{
  outline: solid red 4px;
  width: 100px;
  height: 200px;
  display: block;
  position: relative;
}

#target::after{
  content: attr(data-caption);
  position: absolute;
  bottom: 0;
  left: 0;
  outline: solid 1px gray;
  width: 100%;
  text-align: center;
}
<div class="gallery">
  <img src="404_1.jpg" alt="<img>" id="target" data-caption="Caption 1...">
  <img src="404_2.jpg" alt="<img>" id="target" data-caption="Caption 2...">
  <img src="404_3.jpg" alt="<img>" id="target" data-caption="Caption 3...">
  <img src="404_4.jpg" alt="<img>" id="target" data-caption="Caption 4...">
</div>

相关问题