2014-08-28 69 views
3

我需要找到一種方法來查看div後面的圖像,通過div中的文本:使div的文本部分透明

E.g.

enter image description here

我的字體作爲Web字體。這可能不需要做圖像替換?

+2

這個問題是許多其他問題重複。 http://stackoverflow.com/questions/2289159/can-i-do-knock-out-punch-through-transparency-with-css-fonts?lq=1和http://stackoverflow.com/questions/7626219/我該如何做 - 穿透透明的文本在html-css中?lq = 1 – CRABOLO 2014-08-28 22:37:04

+3

這兩個問題都過時了。例如,它可以通過CSS完成。 http://css-tricks.com/image-under-text/ – Turnip 2014-08-28 22:42:37

+0

是的,我看到了,但不幸的是,它不完全符合我的需求。我需要能夠看到更大的包含div的背景,而不是僅僅看到實際元素上的背景圖像。 – JamesPlayer 2014-08-28 22:54:40

回答

3

使用唯一CSS完成規範的方法之一是完美地重疊兩個背景圖像,從而創建您所描述的「透明」效果。這是一個小提琴:http://jsfiddle.net/v780v1Ln/

注意:填充等會改變元素的尺寸並影響必須爲背景圖像設置的座標。

HTML:

<div id = "wrapper"> 
    <h1><span>DRD</span></h1> 
</div> 

CSS:

* { 
    padding: 0; 
    margin: 0; 
} 

html, body { 
    height: 100%; 
    background: #e2e2e2; 
} 

#wrapper { 
    position: absolute; 
    width: 70%; 
    height: 50%; 
    top: 25%; 
    left: 15%; 
    background: url(http://i58.tinypic.com/2vdieso.jpg) 
       no-repeat 
       0 0/500px 362px; 
} 

#wrapper > h1 { 
    background-color: #fff; 
    text-align: center; 
    position: absolute; 
    padding: 0 55px 0 25px; 
    top: 25px; 
    left: 25px; 
} 

#wrapper > h1 > span { 
    font: bold 70px/1 Sans-Serif; 
    text-transform: uppercase; 
    background: url(http://i58.tinypic.com/2vdieso.jpg) 
       no-repeat 
       -45px -25px/500px 362px; 
    -webkit-background-clip: text; 
    background-clip: text; 
    color: transparent; 
} 
+0

在Chrome中看起來很棒。在FireFox – Christina 2014-08-29 19:50:08

+0

中不起作用WebKit瀏覽器和Opera支持'background-clip:text'。沒有FF支持:https://developer.mozilla.org/en-US/docs/Web/CSS/background-clip。 – DRD 2014-08-29 20:56:12

+0

這個效果很好。我已經做了一箇中心文本和非webkit瀏覽器回退的示例:http://jsfiddle.net/g2tf68v7/23/ – JamesPlayer 2014-08-31 05:55:56