【问题标题】:Responsive, transparent CSS image caption with graceful degradation?具有优雅降级的响应式、透明 CSS 图像标题?
【发布时间】:2012-10-18 07:42:40
【问题描述】:

在图像上创建响应式、透明的 CSS 标题的正确方法是什么——在旧浏览器中优雅地降级?

我正在努力实现:

  • 居中的垂直图像列
  • 图片的高度和宽度相同
  • 每张图片都有一个应该居中的标题
  • 标题应具有透明背景
  • 如果在不支持透明度的旧浏览器中背景变成黑色就更好了

如果你看一下this Fiddle example,它显然有很多问题。

HTML5的基本前提是:

<section>
    <figure>
        <img src="1.jpg">
        <figcaption>Caption 1</figcaption>
    </figure>

    <figure>
        <img src="2.jpg">
        <figcaption>Caption 2</figcaption>
    </figure>

    <figure>
        <img src="3.jpg">
        <figcaption>Caption 3</figcaption>
    </figure>
</section>

但是CSS3 code 是我们遇到问题的地方。这是正确的方法吗?我让它与一些微调(不包括在内)一起工作,但微调对我来说似乎没有语义意义。

例如,结果如下:

我感觉 CSS 在很多层面上都是错误的(双关语)。

【问题讨论】:

标签: image html css responsive-design graceful-degradation


【解决方案1】:

我稍微修改了你的 CSS。主要变化是将position: relative; 添加到父元素,将position: absolute; 添加到标题。

CSS

section {
    overflow: auto;
}

section figure {
    float: left;
    clear: both;

    position: relative;
    overflow: auto;

    margin: 0 auto;
    padding: 30px 0 0 0;
    font-size: 15px;
}

section figure img {
    vertical-align: bottom;
}

section figure figcaption {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;

    background: rgba(0,0,0,0.7);
    text-align: center;
    color: #fff; 
    padding: 10px;
}


section {
    padding-bottom: 30px;
    background: #ccc;

}

演示:http://jsfiddle.net/XjthT/6/

【讨论】:

  • 只需要注意 rgba(0,0,0,0.7) 是 CSS3 属性,在 IE8 和以前的版本中不起作用
  • 在 IE8 及更早版本中,您可以使用渐变滤镜来获得半透明背景filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#bb000000, endColorstr=#bb000000);。如果你想要响应式图像,那么你可能应该将它们的宽度设置为相对于它们的父级jsfiddle.net/thebabydino/XjthT/15 如果你在一个元素上应用position: absolute,那么top/right/bottom/width您对其应用的值是相对于其具有position: relative 的第一个祖先。我不明白你为什么说它没有居中对齐,在我看来是居中对齐...
  • 调整大小以查看它的响应jsfiddle.net/thebabydino/XjthT/15/embedded/result
  • 奇怪,我看到它们在 Win 7 上的 Chrome 和 FF 中都居中对齐:-??这个例子dabblet.com/gist/3971071 说明了为什么绝对定位figcaption 会比使用负数margin 更好(white outline 是围绕imgyellow 围绕figcaption )。我唯一要更改的另一件事是在em 中使用尺寸,而不是在px 中使用它们(这与缩放时缩放的方式有关 - 请参阅kyleschaeffer.com/best-practices/…)。
  • 负边距方法修复了figcaption 框的top 相对于其前一个兄弟(img)的bottom。在较小的屏幕上,figcaption 文本跨越更多行,看起来像是从图像中溢出。 绝对定位方法figcaption框的bottom固定为其父框的bottomfigure)。当figcaption 的文本跨越更多行时,自动上升的是top
猜你喜欢
  • 1970-01-01
  • 2022-01-07
  • 2014-05-31
  • 1970-01-01
  • 2011-08-29
  • 1970-01-01
  • 2010-10-06
  • 2011-01-28
  • 2011-11-21
相关资源
最近更新 更多