简体   繁体   English

使用 CSS 在缺少替代文本的图像上叠加颜色

[英]Use CSS to overlay a color on an image missing an alt text

For the life of me I am trying to find a simple way to overlay a color over an image when it is missing an alt or the alt is empty.对于我的生活,我试图找到一种简单的方法,当缺少 alt 或 alt 为空时,将颜色覆盖在图像上。 I have been able to get the code right for discovering if the alt is indeed missing.我已经能够获得正确的代码来发现是否确实缺少 alt。 If I put something like width: 50px;如果我放一些类似 width: 50px; 的东西。 I can get the image to change size, but I am looking for the color overlay.我可以让图像改变大小,但我正在寻找颜色叠加。 I am trying to create this as a CSS.我正在尝试将其创建为 CSS。

I have looked at other posts on here but they seem more about hovering and that is not what I am looking for.我看过这里的其他帖子,但它们似乎更多是关于悬停,这不是我想要的。

img[alt=""] {width: 50px;}

Try something like the following.尝试类似以下内容。 Which combines my initial answer with PossessWithin 's.它将我的初始答案与PossessWithin相结合。 You can leverage the usual semantic syntax of a <figure/> element along with the sibling and pseudo elements to create an overlay over the entire <figure/> .您可以利用<figure/>元素的常用语义语法以及同级和伪元素在整个<figure/>上创建覆盖。

Unfortunately, you have to have additional markup around the <img> to get it to work since the go to answer for something like this (using a pseudo :after element) won't work for <img> .不幸的是,您必须在<img>周围添加额外的标记才能使其工作,因为回答这样的事情(使用伪:after元素)对<img>不起作用。 See Does :before not work on img elements?请参阅:before 不适用于 img 元素吗?

 .container { position: relative; } img:not([alt])+.label::after { content: "missing alt"; display: flex; align-items: center; justify-content: center; position: absolute; top: 0; right: 0; bottom: 0; left: 0; background-color: rgba(255, 0, 0, 0.2); }
 <figure class="container"> <img src="https://via.placeholder.com/150" width="150" height="150"> <figcaption class="label">Placeholder 1</figcaption> </figure> <figure class="container"> <img alt="Placeholder" src="https://via.placeholder.com/150" width="150" height="150"> <figcaption class="label">Placeholder 2</figcaption> </figure>


You could also try a filter as suggested by j08691 .您也可以按照j08691 的建议尝试使用过滤器

You can use one or more CSS filters.您可以使用一个或多个 CSS 过滤器。 Eg filter: contrast(175%) hue-rotate(90deg) sepia(100%) brightness(90%) ;例如filter: contrast(175%) hue-rotate(90deg) sepia(100%) brightness(90%)

 img:not([alt]) { filter: opacity(25%); }
 <img src="https://via.placeholder.com/150" width="150" height="150"> <img alt="Placeholder" src="https://via.placeholder.com/150" width="150" height="150">

As far as I'm aware of, you'll need two things to achieve what you're looking for:据我所知,你需要做两件事来实现你想要的:

  1. A parent element for <img /> so we can add an overlay using :after . <img />父元素,因此我们可以使用:after添加叠加层。 For this example I will use the figure element.在这个例子中,我将使用figure元素。
  2. A little bit of Javascript to locate all the empty alts & add a class to its parent with the overlay you want.一点点的 Javascript 来定位所有空的 alt 并将一个类添加到它的父类中,并带有你想要的覆盖层。

Here is an example:下面是一个例子:

HTML HTML

<figure class="custom-figure">
    <img src="https://via.placeholder.com/200x200" alt="">
</figure>

<figure class="custom-figure">
    <img src="https://via.placeholder.com/200x200" alt="123">
</figure>

CSS CSS

.custom-figure{
    display: inline-block;
    margin: 10px;
}

.custom-figure--overlay{
    position: relative;
}
.custom-figure--overlay:after{
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: linear-gradient(120deg, #eaee44, #33d0ff);
    opacity: .7;
}

JS JS

const addOverlayToEmptyAlt = () => {
    const images = document.querySelectorAll(".custom-figure > img");

    for (let each of images) {
        const alt = each.getAttribute("alt").trim();

        if (alt === "") {
            each.closest(".custom-figure").classList.add("custom-figure--overlay");
        }
    }
};

addOverlayToEmptyAlt();

Live example on Codepen Codepen 上的实时示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM