简体   繁体   English

基于背景图像反转文本

[英]Text invert based on background image

Is it even possible to obtain something like this only with CSS/CSS3 or with javascript? 是否有可能只用CSS / CSS3或javascript获得这样的东西? but no image for text. 但没有文字图片。

在此输入图像描述

With limited support from old browsers, you can achieve this with text clipping: 在旧浏览器的有限支持下,您可以通过文本剪辑实现此目的:

for this html: 对于这个HTML:

<div class="filter">
<div class="base">SOME TEXT</div>
</div>

with that CSS: 用那个CSS:

.filter {
    width: 250px;
    height: 250px;
    background: -webkit-linear-gradient(-45deg, black 0%,black 50%,white 51%,white 100%);
}
.base {
    width: 100%;
    height: 100%;
    font-size: 80px;
    background: -webkit-linear-gradient(-45deg, white 0%,white 50%,black 51%,black 100%);
    -webkit-background-clip: text;
    color: transparent;
}

You will get the background from the base (that has the inverted colors of the first div) to show only thru the text. 您将从基础(具有第一个div的反色)获得背景,以仅显示文本。

webkit demo webkit演示

Editing 编辑

Have found that it can be done in a single element. 已经发现它可以在一个元素中完成。 The style would be: 风格如下:

.base {
    width: 100%;
    height: 100%;
    font-size: 80px;
    background: -webkit-linear-gradient(-45deg, white 0%,white 50%,black 51%,black 100%), -webkit-linear-gradient(-45deg, black 0%,black 50%,white 51%,white 100%);
    -webkit-background-clip: text, border;
    color: transparent;
}

To achieve this in CSS you would need to use gradients. 要在CSS中实现这一点,您需要使用渐变。 One for the background and one for the text, the gradient would be constant . 一个用于背景,一个用于文本,渐变将是恒定的。 see this for generating them: http://www.colorzilla.com/gradient-editor/ to produce this: 看到这个生成它们: http//www.colorzilla.com/gradient-editor/来产生这个:

    background: #000000; /* Old browsers */
background: -moz-linear-gradient(-45deg,  #000000 0%, #000000 50%, #ffffff 51%, #ffffff 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, right bottom, color-stop(0%,#000000), color-stop(50%,#000000), color-stop(51%,#ffffff), color-stop(100%,#ffffff)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(-45deg,  #000000 0%,#000000 50%,#ffffff 51%,#ffffff 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(-45deg,  #000000 0%,#000000 50%,#ffffff 51%,#ffffff 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(-45deg,  #000000 0%,#000000 50%,#ffffff 51%,#ffffff 100%); /* IE10+ */
background: linear-gradient(135deg,  #000000 0%,#000000 50%,#ffffff 51%,#ffffff 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#ffffff',GradientType=1 ); /* IE6-9 fallback on horizontal gradient */

the main problem you still face though is not being able to "detect" the background image in order to make the text color opposite. 你仍然面临的主要问题是无法“检测”背景图像以使文本颜色相反。

If you wanted to detect the background you could look into the webkit -webkit-background-composite which has an XOR and plus-darker and plus-lighter for composite effects 如果你想检测背景你可以看一下webkit -webkit-background-composite,它有一个XOR,加上更暗,加上更轻的复合效果

Hope this helps. 希望这可以帮助。

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

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