CSS插入文本阴影与背景图像

时间:2017-09-06 23:42:43

标签: css text shadow

是否可以同时包含背景图像和内部阴影的文本?

<div>
  CAN I HAVE AN IMAGE AND INNER SHADOW?
</div>
{{1}}

1 个答案:

答案 0 :(得分:1)

使用CSS过滤器属性,您可以添加任何形状轮廓的阴影:

&#13;
&#13;
div{   
    position:fixed;
    z-index: 2;
    color: white;  /* Fallback: assume this color ON TOP of image */
    background: url('https://placekitten.com/g/500/500') repeat;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    font-size: 100px;
    font-weight: 800;
    text-transform: uppercase; 
    text-shadow: 2px 3px 8px rgba(59, 37, 17, 0.35) inset; 
    filter: drop-shadow( 10px 10px 10px #888 );
}
&#13;
<div>
  CAN I HAVE AN IMAGE AND INNER SHADOW?
</div>
&#13;
&#13;
&#13;

现在所有主流浏览器都支持基本filterhttp://caniuse.com/#feat=css-filters

对于插入阴影

插入阴影需要一些技巧。

我的方法是直接在第一个div上添加另一个div,并使用透明文本不透明度。然后我们使用文本阴影黑客使其显示插入。

&#13;
&#13;
.regular {
    position:fixed;
    z-index: -1
    color: white;  /* Fallback: assume this color ON TOP of image */
    background: url('https://placekitten.com/g/500/500') repeat;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    font-size: 100px;
    font-weight: 800;
    text-transform: uppercase; 
    text-shadow: 2px 3px 8px rgba(59, 37, 17, 0.35) inset; 
}
.overlay {
    position:fixed;
    z-index: 10;
    color: transparent;
    font-size: 100px;
    font-weight: 800;
    text-transform: uppercase; 
    text-shadow: 2px 2px 3px rgba(255,255,255,0.5);
    -webkit-background-clip: text;
       -moz-background-clip: text;
            background-clip: text;
}
&#13;
<div class="regular">
  CAN I HAVE AN IMAGE AND INNER SHADOW?
</div>
<div class="overlay">
  CAN I HAVE AN IMAGE AND INNER SHADOW?
</div>
&#13;
&#13;
&#13;