html - 定位一个 figcaption 以固定在图像的右下角

标签 html css image

我想在图像顶部创建一个 figcaption,默认位于图像右下角,大约是图像宽度的 40%。 这是我的意思的图像:http://imgur.com/a/U9ETh

这是我到目前为止的代码,但似乎无法在不溢出的情况下默认向右:

figure {
    margin: 0;
    line-height: 0;
    position: relative;
}

figcaption {
    font-size: .9em;
    line-height: 1.5;
    color: #fff;
    padding: 1em;
    background: rgba(19, 43, 102, .85);
    position: absolute;
    bottom: 0;
    right: 0;
    width: 40%;
}
 <figure>
    <img src="img/anImage.png">
    <figcaption>
        <h4>Blog Post</h4>
    </figcaption>
</figure>

最佳答案

<figure>将是容器的整个宽度,而图像将是其自然大小。

你有两个选择。

  1. 使图像全宽(当然取决于样式)
  2. 限制图形元素的宽度

选项 1:

figure img {
    height: auto;
    max-width: none;
    width: 100%;
}

我假设您已经为图像设置了最大宽度并将其删除。

选项 2:

figure { 
    display: table;
    margin: 0 auto;
    position: relative;
}

如果您还没有这样做,您可能还想将图像设置为 display: block;vertical-align: top;消除任何微小的偏差。

关于html - 定位一个 figcaption 以固定在图像的右下角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41777314/

相关文章:

html - 图片缩略图后的 CSS 和测试对齐

javascript - 同位素插件自动高度问题

javascript - 视差滚动不适用于移动 css

c# - 编辑图像的细节

html - 弃用代码 : <b> vs style ="font-weight:bold;"

html - 如何在 Flex 设计中使水平居中的导航链接更靠近?

HTML + CSS 菜单问题

javascript - 样式范围属性

javascript - 是否可以使用百分比相对定位将图钉图像定位在另一个图像上

android - 使用图像android移动我的进度条