答案 0 :(得分:18)
这与Daerik的答案非常相似,但我想避免使用额外的元素,并自动生成水印文本。
Array.from(document.querySelectorAll('.watermarked')).forEach(function(el) {
el.dataset.watermark = (el.dataset.watermark + ' ').repeat(300);
});

.watermarked {
position: relative;
overflow: hidden;
}
.watermarked img {
width: 100%;
}
.watermarked::before {
position: absolute;
top: -75%;
left: -75%;
display: block;
width: 150%;
height: 150%;
transform: rotate(-45deg);
content: attr(data-watermark);
opacity: 0.7;
line-height: 3em;
letter-spacing: 2px;
color: #fff;
}

<div class="watermarked" data-watermark="howbloggerz">
<img src="http://www.w3schools.com/css/img_fjords.jpg">
</div>
&#13;
答案 1 :(得分:5)
设置容器的大小,并使用绝对定位浮动文本,同时旋转transforming文本。
#watermark {
height: 450px;
width: 600px;
position: relative;
overflow: hidden;
}
#watermark img {
max-width: 100%;
}
#watermark p {
position: absolute;
top: 0;
left: 0;
color: #fff;
font-size: 18px;
pointer-events: none;
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
}
<div id="watermark">
<img src="http://www.topchinatravel.com/pic/city/dalian/attraction/people-square-1.jpg">
<p>This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark. This is a watermark.</p>
</div>
注意:对于重复文字,我建议使用JavaScript或jQuery。
答案 2 :(得分:1)
CSS:-
.watermarked::before {
position: fixed;
top: -75%;
left: -75%;
display: block;
width: 300%;
height: 300%;
transform: rotate(-45deg);
content: attr(data-watermark);
font-size: 30px;
opacity: 0.15;
line-height: 4em;
letter-spacing: 2px;
color: #ff0523;
z-index:-1;
}
HTML:-
<div class="watermarked" data-watermark="Watermark.."></div>
SCRIPT:-
<script>
Array.from(document.querySelectorAll('.watermarked')).forEach(function(el) {
el.dataset.watermark = (el.dataset.watermark + ' ').repeat(10000);
});
</script>
水印位置应固定并显示&amp;高度应该 是你的屏幕尺寸。然后水印将打印在你的整个 报告或其他什么。
答案 3 :(得分:0)
我现在正在使用svg作为背景图像。普通CSS:
body {
background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='100px' width='100px'><text transform='translate(20, 100) rotate(-45)' fill='rgb(245,45,45)' font-size='20'>watermark</text></svg>");
}
<body style="width:100%;height:100%"></body>
设置背景的Javascript:
function watermark(text) {
var body = document.getElementsByTagName('body')[0];
var bg = "url(\"data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='100px' width='100px'>" +
"<text transform='translate(20, 100) rotate(-45)' fill='rgb(245,45,45)' font-size='20' >" + text + "</text></svg>\")";
body.style.backgroundImage = bg
}
//for this test
var input = document.getElementById('a');
watermark(input.value);
input.addEventListener('input', function(evt) {
watermark(this.value);
});
<body style="width:100%;height:100%">
<input id="a" value="change me" />
</body>
答案 4 :(得分:0)
如果仅是在图像上放置文本,则这是另一个可能的css选项,它带有drop-shadow
和一个伪(text-shadow
也适用)
.watermarked:after {/* draw the watermark at screen*/
color: rgba(0, 0, 0, 0.4);
content: 'watermarked watermarked watermarked';
word-spacing: 3em;
transform: rotate(-60deg);
filter:
drop-shadow(2em 3em #000)
drop-shadow(4em 6em #000)
drop-shadow(8em 12em #000)
drop-shadow(-15em -24em #000)
;
}
/* makeup */
.watermarked {
width: max-content;
border: solid;
display: grid;
overflow: hidden;
}
img,
.watermarked:after {
grid-row: 1;
grid-column: 1;
margin: auto;
}
<div class="watermarked" data-watermark="howbloggerz">
<img src="http://www.w3schools.com/css/img_fjords.jpg">
</div>
答案 5 :(得分:0)
Array.from(document.querySelectorAll('.watermarked')).forEach(function(el) {
el.dataset.watermark = (el.dataset.watermark + ' ').repeat(300);
});
.watermarked::before {
position: fixed;
top: -75%;
left: -75%;
display: block;
width: 300%;
height: 300%;
transform: rotate(-45deg);
content: attr(data-watermark);
font-size: 30px;
opacity: 0.75;
line-height: 4em;
letter-spacing: 2px;
color: #cccccc;
}
<html>
<head>
<title>
Form and Watermarks
</title>
</head>
<body>
<h1>Form Demo</h1>
<form method="post">
First Name: <input name="first" type="text" /><br>
Last name: <input name="last" type="text" />
<br />
<input name="Submit" type="submit" value="submit" />
</form>
<div class='watermarked' data-watermark='watermark..' ></div>
</body>
</html>
效果很好,但不适用于 html 表单。表单元素被禁用。看看这个。