如何用纯 CSS 和 HTML 覆盖 image/watermark

How to overlay an image/watermark with pure CSS and HTML

是否有一种简单的方法可以通过传递 class 来将透明的 PNG(或任何其他图像)覆盖在带有 CSS 的图像标签上?

 <img class="watermarked" src="http://placehold.it/500x325.jpg" alt="Photo">

那么 CSS 可能与此类似(无效):

.watermarked{
  background-image: url("http://placehold.it/100x100/09f/fff.png");
  position: relative;
  left: 30px;
  opacity: 0.5;
}

理想情况下,我可以在 CSS 中定义我的 "watermark" 叠加图像的路径,然后我添加 "watermarked" class 的任何图像都会得到这张图片以一些负面的相对定位叠加在上面。它应该能够应用于单个页面上的多个图像,因此单次使用 DIV 将不起作用。

显然,这对保护底层图像没有任何作用...因此将其称为水印并不准确...更多的是临时覆盖。我很惊讶没有现成的答案,但我四处寻找并没有在这里或 google.

上找到答案

.watermarked {
  background-image: url("http://via.placeholder.com/100x100/09f/fff.png");
  position: relative;
  left: 30px;
  opacity: 0.5;
}
<div class="col-lg-3 col-md-6 mb-4">
  <div class="card">
    <img class="card-img-top watermarked" src="http://placehold.it/500x325" alt="">
    <div class="card-body">
      <h4 class="card-title">Card title</h4>
      <p class="card-text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sapiente esse necessitatibus neque.</p>
    </div>
  </div>
</div>

如果您想要一个仅 CSS 的解决方案,我建议您将要加水印的图像包装在一个容器 div 中,然后我们可以添加一个 :after 伪元素包含我们的水印。

理想的解决方案是将 :after 伪元素直接应用于 img 元素,但遗憾的是大多数浏览器不支持使用 :before:afterimg 个元素上。如果是这样,我们可以将 .watermark 直接应用于 img 标记。

在下面的解决方案中,我们在整个图像上覆盖了一个 :after 伪元素,然后将水印徽标放在左上角。

这个解决方案的一个优点是 :after 元素覆盖了整个图像,这可以防止用户右键单击并保存图像(尽管这不会阻止任何有网络经验的人找到图片 URL 下载)。因为 :after 元素覆盖了整个图像,我们还可以应用半透明背景来稍微遮盖带水印的图像。

所以我们剩下这个解决方案:

.watermarked {
  position: relative;
}

.watermarked:after {
  content: "";
  display: block;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0px;
  left: 0px;
  background-image: url("http://placehold.it/100x100/09f/fff.png");
  background-size: 100px 100px;
  background-position: 30px 30px;
  background-repeat: no-repeat;
  opacity: 0.7;
}
<div class="watermarked">
  <img src="http://placehold.it/500x325.jpg" alt="Photo">
</div>

另一种方法是创建一个相对的div并将其放入页面流中,先放置主图像,然后将水印放在最上面。

<!Doctype>
<html>
<head>
    <style>
    .card
    {
        position: relative;
        top: 0;
        left: 0;
    }
    .watermark
    {
        position: absolute;
        top: 60px;
        left: 80px;
    }
    </style>
</head>
<body>
    <div style="position: relative; left: 0; top: 0;">
        <img src="img/cardX.png" class="card"/>
        <img src="img/watermark2.png" class="watermark"/>
    </div>
</body>
</html>

或者您可以 overlay 您的图像带有水印文本。这可以使用 HTML 或 CSS 轻松完成。

HTML:

<figure class="watermark">
<img class="demo" src="image.jpg" alt="Image" />
<figcaption class="watermark-text">Watermark XYZ</figcaption>
</figure>

CSS 代码较长,因为您需要定义 class 等,并使用上述 Brett 所述的 :after 元素。

.watermark {
position: relative;
padding: 0;
margin: 0;
}
.watermark img {
display: block;
max-width: 100%;
height: auto;
}
.watermark:after {
content: "";
position: absolute;
display: block;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0) linear-gradient(to bottom, rgba(0, 0, 0, 0) 0px, rgba(0, 0, 0, 0.6) 70%) repeat 0 0;
z-index: 1;
}
.watermark-text {
display: block;
position: absolute;
width: 100%;
color: #fff;
left: 0;
bottom: 0;
padding: 1em;
font-weight: 700;
z-index: 2;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}

使用实际图像作为背景图像,然后在标签中无限重复使用水印。这使得将水印后面的背景图像另存为变得更加困难,但也可以防止爬虫为图像编制索引。如果您仍希望抓取带有水印的照片,您只需在 .txt 文件中列出它们的 URL,然后将其作为站点地图提交。