【问题标题】:CSS inset text shadow with background image带有背景图像的 CSS 插入文本阴影
【发布时间】:2017-09-06 23:42:43
【问题描述】:

是否可以同时拥有带有背景图像和内阴影的文本?

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; 
}
<div>
  CAN I HAVE AN IMAGE AND INNER SHADOW?
</div>

【问题讨论】:

    标签: css text shadow


    【解决方案1】:

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

    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 );
    }
    <div>
      CAN I HAVE AN IMAGE AND INNER SHADOW?
    </div>

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

    对于嵌入阴影

    嵌入阴影需要一些技巧。

    我的方法是直接在您的第一个 div 之上添加另一个具有透明文本不透明度的 div。然后我们使用文本阴影 hack 使其显示为 inset。

    .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;
    }
    <div class="regular">
      CAN I HAVE AN IMAGE AND INNER SHADOW?
    </div>
    <div class="overlay">
      CAN I HAVE AN IMAGE AND INNER SHADOW?
    </div>

    【讨论】:

    • 内阴影怎么样?
    • @Benneb10 抱歉,嘿...我一开始并没有完全看到您问题的插入部分。我已经更新了我的答案,并通过一些 CSS 黑客设法使其插入。不得不使用黑客,因为 filter 不幸的是没有 inset 属性。
    猜你喜欢
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 2020-10-25
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多