Benneb10
Benneb10

Reputation: 1489

CSS inset text shadow with background image

Is it possible to have text with a background image and inner shadow at the same time?

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>

Upvotes: 0

Views: 1886

Answers (1)

basement
basement

Reputation: 718

With the CSS filter property you can add shadows that contour any shape:

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>

All major browsers now support basic filter! http://caniuse.com/#feat=css-filters

For Inset Shadows

Inset shadows require some trickery.

My approach was to add another div directly on top of your first one with transparent text opacity. Then we use a text shadow hack to make it appear 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>

Upvotes: 1

Related Questions