它必须是动态的吗?做到这一点的唯一方法是使用具有透明度的图像(GIF 或更好的 PNG)。
我不确定这是否是您想要的,但无论如何都会解释一下。
情况:你有一个非普通的背景,你想通过你的文字被看到。
解决方案:没有 CSS 可以解决这个问题。您必须使用可靠的图像编辑器来创建一个带有文本的图层,以及另一个将是您的文本的底片的图层
这可以让您获得一些有趣的效果,但如果您希望它是动态的,则必须在运行服务器端生成图像。
这种诡计目前在纯 CSS 中是不可能的(在 Javascript 中可能是可能的)。
编辑
看到Paul's find on webkit 让我开始思考如何在 Firefox、Opera 和 IE 中伪造这种行为。到目前为止,我在 Firefox 上使用 canvas 元素很幸运,我正在尝试在 filter:progid:DXImageTransform.Microsoft 中找到一些行为。
到目前为止canvas,这就是我所做的
<html>
<body>
<canvas id="c" width="150" height="150">
</canvas>
<script>
ctx = document.getElementById("c").getContext("2d");
// draw rectangle filling the canvas element
ctx.fillStyle = "red";
ctx.fillRect(0,0,150,150);
// set composite property
ctx.globalCompositeOperation = 'destination-out';
// the text to be added now will "crop out" the red rectangle
ctx.strokeText("Cropping the", 10, 20);
ctx.strokeText("red rectangle", 10, 40);
</script>
</body>
</html>
通过使用detination-out compositing 和drawing text on the canvas。