Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add "Watermark" effect with CSS?

Tags:

I have an image in a div. I need to add a watermark effect, or basically another image, overtop the image the div. How can I do this with css?

Example code:

<div id="image">
</div>

css:

#image {
   background:url(images/images.png) no-repeat;
}
like image 880
Zach Smith Avatar asked Oct 26 '09 16:10

Zach Smith


People also ask

How do you overlay an image watermark with pure CSS and HTML?

The ideal solution would be to apply the :after pseudo-element directly to the img element, but unfortunately most browsers don't support using :before or :after on img elements. If so we could have applied the . watermark directly to the img tag.

How do I add a watermark to my website?

Right-click anywhere on the page to which you want to insert a background picture, and then click Page Properties. Click the Formatting tab. Select the Background Picture check box. Select the Make it a watermark check box.


1 Answers

There are at least two ways to do this, one of which has sort of been touched-on by @erenon's answer.

To meet your requirements and use an image:

<div id="image">
    <img src="path/to/watermarking_image.png" alt="..." />
</div>

With the following CSS:

#image {
/* the image you want to 'watermark' */
height: 200px; /* or whatever, equal to the image you want 'watermarked' */
width: 200px; /* as above */
background-image: url(path/to/image/to/be/watermarked.png);
background-position: 0 0;
background-repeat: no-repeat;
position: relative;
}

#image img {
/* the actual 'watermark' */
position: absolute;
top: 0; /* or whatever */
left: 0; /* or whatever, position according to taste */
opacity: 0.5; /* Firefox, Chrome, Safari, Opera, IE >= 9 (preview) */
filter:alpha(opacity=50); /* for <= IE 8 */
}

#image {
  /* the image you want to 'watermark' */
  height: 200px;
  /* or whatever, equal to the image you want 'watermarked' */
  width: 200px;
  /* as above */
  background-image: url(https://www.davidrhysthomas.co.uk/linked/astrid_avatar.png);
  background-position: 0 0;
  background-repeat: no-repeat;
  position: relative;
}

#image img {
  /* the actual 'watermark' */
  position: absolute;
  top: 0;
  /* or whatever */
  left: 0;
  /* or whatever, position according to taste */
  opacity: 0.5;
  /* Firefox, Chrome, Safari, Opera, IE >= 9 (preview) */
  filter: alpha(opacity=50);
  /* for <= IE 8 */
}
<div id="image">
  <img src="https://www.davidrhysthomas.co.uk/linked/watermark.png" alt="..." />
</div>

This should generate something like the following:

   +--------------+--------------+
   |              |              |
   | 'watermark'  |              |
   |              |        __    |
   +--------------+       /  \   |
   |                     (    )  |
   |               /\     \  /   |
   |              /  \     ||    |   <-- Picture. Of...something. O_o
   | /\          /    \    ||    |
   |/  \________/      \_()||_()(|
   +-----------------------------+

The alternative way, assuming all you want to 'watermark' with is 'one word,' is to use words:

<div id="image">
    <p>Watermark text</p>
</div>

And the following CSS:

#image {
/* the image you want to 'watermark' */
height: 200px; /* or whatever, equal to the image you want 'watermarked' */
width: 200px; /* as above */
background-image: url(path/to/image/to/be/watermarked.png);
background-position: 0 0;
background-repeat: no-repeat;
position: relative;
}

#image p {
/* the actual 'watermark' */
position: absolute;
top: 0; /* or whatever */
left: 0; /* or whatever, position according to taste */
opacity: 0.5; /* Firefox, Chrome, Safari, Opera, IE >= 9 (preview) */
filter:alpha(opacity=50); /* for <= IE 8 */
}

#image {
  width: 200px;
  height: 200px;
  background-image: url(https://www.davidrhysthomas.co.uk/linked/astrid_avatar.png);
  background-position: 0 0;
  background-repeat: no-repeat;
  position: relative;
}

#image p {
  /* the actual 'watermark' */
  position: absolute;
  top: 0;
  /* or whatever */
  left: 0;
  /* or whatever, position according to taste */
  opacity: 0.5;
  /* Firefox, Chrome, Safari, Opera, IE >= 9 (preview) */
  filter: alpha(opacity=50);
  /* for <= IE 8 */
}
<div id="image">
  <p>Watermark text</p>
</div>

This should generate something like the following:

   +--------------+--------------+
   |              |              |
   |  watermark   |              |
   |    text      |        __    |
   +--------------+       /  \   |
   |                     (    )  |
   |               /\     \  /   |
   |              /  \     ||    |   <-- Picture. Of...something. O_o
   | /\          /    \    ||    |
   |/  \________/      \_()||_()(|
   +-----------------------------+

I realise that this question was probably answered to your satisfaction, but I hope this is of some use to you, even only as general information.

Updated to add an SVG option, though do bear in mind that in this example the 'watermark' text is selectable and the SVG element can be inspected to retrieve the image URL:

svg {
  width: 300px;
  height: 300px;
  border: 2px solid #000;
}
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <style>
    /* adjusting the fill, colour and transparency,
       of the watermark text while the SVG is hovered */
    svg:hover .watermark {
      fill: #666f;
      font-size: 2.1em;
    }
    /* defining the default style of the watermark */
    .watermark {
      fill: #bbbb;
      font-size: 2em;
      font-family: Ubuntu, Calibri, sans-serif;
      font-weight: bold;
      transition: all 0.3s linear;
    }
  </style>
  <!-- the image that you want to be watermarked: -->
  <image xlink:href="http://www.davidrhysthomas.co.uk/linked/astrid_avatar.png" height="200px" width="200px" />
  <!-- the watermark text: -->
  <text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" class="watermark">watermark</text>
</svg>
like image 176
David Thomas Avatar answered Sep 23 '22 17:09

David Thomas