2013-03-19 41 views
11

我會嘗試用圖像解釋我的問題。開始了。如何應用反文本掩碼與CSS

1 - 此圖片顯示文本掩蔽的圖像,到目前爲止好,我可以用下面的代碼做:

enter image description here

font-size: 120px; 
background: url(image-to-be-masked.jpg) repeat 0 0, white; 
-webkit-background-clip: text; 
-webkit-text-fill-color: transparent; 

2 - 這其他圖像,文本創建相反的效果,只留下透明的文字區域。這就是我想要的:

enter image description here

有沒有人嘗試過嗎?

+0

你會有另一個圖像嗎?爲什麼不用白色給文本着色? – bfavaretto 2013-03-19 18:20:05

+0

是的,會有另一個背後的圖像。 – 2013-03-19 18:22:49

+0

所以你可能想嘗試'-webkit-mask-image:url(the_bg_image.png);' – bfavaretto 2013-03-19 18:25:40

回答

8

我不認爲CSS可以做到這一點。但是你可以破解它一起使用三種不同的嵌套元素:

  • 最外層的元素包含背景圖像
  • 中間元素包含中間圖像
  • 內部元件包含文本,並具有相同的背景圖像作爲最外層的元素,與第一個示例中的background-clip:text;一樣被屏蔽。

這可以工作,但有點麻煩,因爲你必須補償蒙版背景位置才能達到預期的效果。這裏是一個例子:http://jsfiddle.net/dzkTE/

+0

不錯,這是一個解決方案,但我不知道它是否是最好的。謝謝! – 2013-03-19 19:08:53

+2

值得一提的是,剪切文本的背景圖像(或其使用的梯度)需要具有與參考窗口*的主圖像*完全相同的原點和尺寸。這意味着它必須在自己的邊界框之外開始,並且尺寸大於自身以便使背景正確排列 – 2015-04-26 03:53:05

-1

[抱歉,我誤解了這個。我試了一下,認爲:P] 我認爲你可以通過使用背景剪輯。

background-clip:text; 

檢查here(請在Chrome嘗試或增加供應商前綴)。你可以檢查demo page表格chris。

+0

這不是預期的效果。需要是相反的效果。 – 2013-03-19 18:36:55

+0

對不起。我編輯了答案。 – crazyrohila 2013-03-19 18:38:32

+0

該文本應該「清除」圖像 – 2013-03-19 18:39:47

1

我設法用幾種不同的方法來達到這個目的。 見我的答案在這裏:https://stackoverflow.com/a/32476482/2315496

見下圖:

有三種方式你在找什麼在HTML/CSS/JavaScript的,略有些比其他人更哈克。

  1. 使用<canvas>繪製你的形狀和類型,並設置ctx.globalCompositeOperation="xor";其中ctx是你的畫布背景。

var c=document.getElementById('myCanvas'); 
 
var ctx=c.getContext('2d'); 
 
ctx.font = "bold 36px Proxima Nova"; 
 
ctx.fillStyle='blue'; 
 
ctx.fillText("MORE",100,87); 
 
ctx.globalCompositeOperation='xor'; 
 
ctx.beginPath(); 
 
ctx.fillStyle='white'; 
 
ctx.arc(150,75,75,0,2*Math.PI); 
 
ctx.fill();
body{ 
 
    background: -webkit-radial-gradient(top left,rgb(23, 39, 34) 4%,rgb(56, 99, 99) 37%,rgb(22, 27, 15) 73%,rgb(22, 27, 14) 93%,#232323 100%); 
 
    width:100%; 
 
    height:100%; 
 
} 
 

 
html{ 
 
    height:100% 
 
}
<canvas id='myCanvas'></canvas>

  • 使用mix-blend-mode: screen和設置文本到黑的顏色。
  • .blend-mode { 
     
        background:white; 
     
        border-radius:100%; 
     
        width:150px; 
     
        height:150px; 
     
        line-height:155px; 
     
        float:left; 
     
        margin:20px; 
     
        font-family: "Proxima Nova", Hevetica, sans-serif; 
     
        font-weight:bold; 
     
        color:black; 
     
        text-align:center; 
     
        vertical-align:middle; 
     
        font-size:36px; 
     
        mix-blend-mode: screen; 
     
    } 
     
    
     
    body{ 
     
        background: -webkit-radial-gradient(top left,rgb(23, 39, 34) 4%,rgb(56, 99, 99) 37%,rgb(22, 27, 15) 73%,rgb(22, 27, 14) 93%,#232323 100%); 
     
        width:100%; 
     
        height:100%; 
     
    } 
     
    
     
    html{ 
     
        height:100%; 
     
    }
    <div class="blend-mode">MORE</div>

  • 使用-webkit-text-fill-color:transparent-webkit-background-clip:text揭示它後面的背景的副本。這需要對背景副本進行一些調整以匹配「原始」背景。
  • .wrap{ 
     
        margin:20px; 
     
        float:left; 
     
        position:relative; 
     
    } 
     
    
     
    .background { 
     
        width: 150px; 
     
        height: 150px; 
     
        border-radius: 100%; 
     
        background: white; 
     
        position:absolute; 
     
        top:0; 
     
        left:0; 
     
        z-index:4; 
     
        
     
    } 
     
    
     
    .text { 
     
        position:absolute; 
     
        top: 25px; 
     
        left: 25px; 
     
        z-index:5; 
     
        background: -webkit-radial-gradient(top left, rgb(23, 39, 34) 4%, rgb(56, 99, 99) 37%, rgb(22, 27, 15) 73%, rgb(22, 27, 14) 93%, #232323 100%); 
     
        font-family: Proxima Nova, Helvetica, sans-serif; 
     
        font-weight:bold; 
     
        line-height:100px; 
     
        font-size: 36px; 
     
        color: #666; 
     
        -webkit-background-clip: text; 
     
        -webkit-text-fill-color: transparent; 
     
    } 
     
    
     
    body{ 
     
        background: -webkit-radial-gradient(top left,rgb(23, 39, 34) 4%,rgb(56, 99, 99) 37%,rgb(22, 27, 15) 73%,rgb(22, 27, 14) 93%,#232323 100%); 
     
        width:100%; 
     
        height:100%; 
     
    } 
     
    
     
    html{ 
     
        height:100%; 
     
    }
    <div class="wrap"> 
     
        <div class="background"></div> 
     
        <div class="text">MORE</div> 
     
    </div>

    這些都不是容易的工作,以及跨瀏覽器,我沒有測試過!

    +0

    儘管此鏈接可能回答此問題,但最好在此處包含答案的基本部分並提供該鏈接供參考。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – 2015-09-09 10:42:16

    +0

    好的,我在這裏添加了來自其他帖子的內容。只是不想過郵件。 – SJT 2015-09-09 11:07:02