23

Is there anyway to use this text gradient method and also have a drop shadow on the text. http://css-tricks.com/snippets/css/gradient-text/

When I set my dropshadow like

text-shadow: 1px 1px 2px #000;

Then it messes up my text gradient because the background is set to transparent. Does anyone know of a solution to this for webkit browsers.

1

4 Answers 4

45

You can combine with filter: drop-shadow CSS property that will make the trick.

h1 {
  font-size: 72px;
  background: -webkit-linear-gradient(#eee, #333);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  filter: drop-shadow(2px 2px #333);
}
<h1>Example</h1>

Sign up to request clarification or add additional context in comments.

1 Comment

wow thank you so much
13

updated

Found a solution for you, only problem is it requires the html to have a attribute.

http://jsfiddle.net/2GgqR/2/

    h1 {
      font-size: 72px;
      background-image: -webkit-linear-gradient(#eee, #333);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      position:relative;  
    }

    h1:after {
      background: none;
      content: attr(data-text);
      left: 0;
      top: 0;
      z-index: -1;
      position: absolute;
      text-shadow: 1px 1px 2px #000;
    }

original is here :) http://viget.com/inspire/background-clip-text-shadow-gradients

updated link from comments http://jsfiddle.net/2GgqR/5/

here I added the background color of the background to the :after

    h1:after {
      background: #f3f3ee;
    }

    .background{
        background-color: #f3f3ee;
        position: relative;
        z-index: 1;
    }

1 Comment

Hi Karl this works great until you try to have a wrapper div around it which I need to have a background image behind it all. see my updated fiddle with just a background color wrapper around it. jsfiddle.net/2GgqR/3
0

Since some browsers didnt get the "clipping" rule I needed to find a way to create a gradient using just text shadow. So I managed to do it repeating the word and using color transparent . Check the result here

HTML

<h2 class="points">
  <p><span class="text">REPEAT THIS WORD</span><span class="gradient">REPEAT THIS WORD</span><span class="gradient">REPEAT THIS WORD</span></p>
</h2>

CSS

html, body {
  background: black;
  font-size: 16px;
  height: 100%;
  width: 100%;
}

.points {
  font-size: 2.8em;
  padding: 0 0.2em 0 1em;
  display: flex;
  justify-content: center;
  align-content: center;
  width: 100%;
}
.points p {
  position: relative;
}
.points span {
  width: 100%;
}
.points span.text {
  color: white;
  position: relative;
  text-shadow: -2px 2px 0 #882b06, 2px 2px 0 #882b06, 2px -2px 0 #882b06, -2px -2px 0 #882b06;
}
.points span.gradient {
  color: transparent;
  position: absolute;
}
.points span:nth-child(2), .points span:last-child {
  color: transparent;
  left: 0;
  overflow: hidden;
  right: 0;
  width: 100%;
  white-space: nowrap;
}
.points span:nth-child(2) {
  bottom: 0;
  text-shadow: 0 -0.33em 2px #fbe8a1;
  height: 0.8em;
}
.points span:last-child {
  bottom: 0.2em;
  text-shadow: 0 -0.55em 3px #ecb752;
  height: 0.4em;
}

https://codepen.io/kengreg/pen/JjrwLyL

Comments

0

I have figured out a solution which does not use filter and z-index. This can be used if you have a background or a foreground:

    h1 {
        position: relative;
    }

    h1::after {
        background-image: linear-gradient(#f00, #fcc);
        -webkit-background-clip: text;
        background-clip: text;
        -webkit-text-fill-color: transparent;
        content: attr(data-content);
        position: absolute;
        top: 0;
        left: 0;
    }

    h1::before {
        text-shadow: #000 1px 1px 2px;
        content: attr(data-content);
        position: absolute;
        top: 0;
        left: 0;
    }

Effect: https://jsfiddle.net/m9dtf6po/2/

Of course, since I am using ::before and ::after, an attribute is needed, but the attribute can be shared. The problem of using z-index is that:

This was the problem I faced when I did have a background and a foreground sandwiching my text, as in the following image: enter image description here

The text S+ needs to be on top of a bar, but on top of all things there can be a box. I actually found this post and tried Karl's solution but the shadow disappeared behind the bar.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.