css - 有没有更好的方法将图形元素限制为所包含图像的大小

标签 css html figure

我有带有标题的图像,如下所示:

<figure class="imageBoxShadow">
    <img alt="Alt-Text" src="http://lorempixel.com/280/375">
    <figcaption>A Caption for this image</figcaption>
</figure>

“imageBoxShadow”类在图像周围放置了一个 boxShadow(谁能猜到?)。我把它放在图上,因为标题应该位于边框内。只要标题比图像宽,就可以正常工作。

但是,如果我有一个小图像和/或一个长标题,标题将尽可能扩展图形元素,这看起来很愚蠢。

当然,我可以通过添加硬编码样式来设置宽度来限制图形元素:

<figure class="imageBoxShadow" style="width: 100px">
    <img alt="Alt-Text" src="http://lorempixel.com/100/150">
    <figcaption>Using a hardcoded style to restrict the figure element works but feels stupid.</figcaption>
</figure>

但这感觉很愚蠢,而且在 CMS 环境中不太方便。

有更好的方法吗?

有一个 jsFiddle 链接来查看:http://jsfiddle.net/Sorcy/h5sNB/

最佳答案

添加

figure {
  display: table;
  width: 1px;
}

到你的CSS [建议here ] 然后你就完成了。

关于css - 有没有更好的方法将图形元素限制为所包含图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13342721/

相关文章:

javascript - <a> 标签内的 <a> 标签?

css - 无法使用更少的 PHP 解析媒体查询

javascript - 如果 `navigator.clipboard.writeText` 在 android 上由 alert() 继续,为什么不将文本复制到剪贴板

HTML 严格 & CSS : How do I close the gap?

html - 对齐图形及其文本/图形说明

jquery - 如何在 html/css 中使用 sprite 图像并使用 jQuery 切换更改

javascript - 排行榜。将工具提示置于所有元素之上

html - 如何覆盖第 n 个 child

python - 使用 PyCharm 我想显示 plot extra figure 窗口

html - 在 Twitter Bootstrap 的 Carousel 模板中将字幕居中对齐并置于特写图像下方