Inner text shadow with CSS

后端 未结 22 1339
情深已故
情深已故 2020-11-28 18:07

I am currently playing around with CSS3 and trying to achieve a text effect like this (the black blurry inner shadow):

相关标签:
22条回答
  • 2020-11-28 18:31

    Here's my best try:

        .inner_shadow {
        color:transparent;
        background-color:white;
        text-shadow: 0 0 20px rgba(198,28,39,0.8), 0 0 0 black;
        font-family:'ProclamateHeavy';  // Or whatever floats your boat
        font-size:150px;
        }
       
        <span class="inner_shadow">Inner Shadow</span>
       

    The problem is how to clip the shadow that bleeds around the edges!!! I tried in webkit using background-clip:text, but webkit renders the shadow above the background so it doesn't work.

    Making a Text Mask with CSS?

    Without a top mask layer it is impossible to do a true inner shadow on text.

    Perhaps someone should recommend that the W3C add background-clip: reverse-text, that would cut a mask through the background instead of cutting the background to fit inside the text.

    Either that or render the text-shadow as part of the background and clip it with background-clip: text.

    I tried absolutely positioning an identical text element above it, but the problem is background-clip: text crops the background to fit inside the text, but we need the reverse of that.

    I tried using text-stroke: 20px white; on both this element and the one above it, but the text stroke goes in as well as out.

    Alternate Methods

    Since there is currently no way to make an inverted-text mask in CSS, you could turn to SVG or Canvas and make a text replacement image with the three layers to get your effect.

    Since SVG is a subset of XML, SVG text would still be select-able and searchable, and the effect can be produced with less code than Canvas.

    It would be harder to achieve this with Canvas because it doesn't have a dom with layers like SVG does.

    You could produce the SVG either server-side, or as a javascript text-replacement method in the browser.

    Further Reading:

    SVG versus Canvas:

    http://dev.opera.com/articles/view/svg-or-canvas-choosing-between-the-two/

    Clipping and Masking with SVG Text:

    http://www.w3.org/TR/SVG/text.html#TextElement

    0 讨论(0)
  • 2020-11-28 18:34

    This looks like it's working: http://tips4php.net/2010/08/nice-css-text-shadow-effects/

    He's using multiple shadows to achieve that effect as explained here: http://www.w3.org/Style/Examples/007/text-shadow#multiple

    0 讨论(0)
  • 2020-11-28 18:37

    You can kind of do this. Unfortunately there's no way to use an inset on text-shadow, but you can fake it with colour and position. Take the blur right down and arrange the shadow along the top right. Something like this might do the trick:

    background-color:#D7CFBA;
    color:#38373D;
    font-weight:bold;
    text-shadow:1px 1px 0 #FFFFFF;
    

    ... but you'll need to be really, really careful about which colours you use otherwise it will look off. It is essentially an optical illusion so won't work in every context. It also doesn't really look great at smaller font sizes, so be aware of that too.

    0 讨论(0)
  • 2020-11-28 18:37

    Building on the :before :after technique by web_designer, here is something that comes closer to your look:

    First, make your text the color of the inner shadow (black-ish in the case of the OP).

    Now, use an :after psuedo class to create a transparent duplicate of the original text, placed directly on top of it. Assign a regular text shadow to it with no offset. Assign the original text color to the shadow, and adjust alpha as needed.

    http://dabblet.com/gist/2499892

    You don't get complete control over spread, etc. of the shadow like you do in PS, but for smaller blur values it is quite passable. The shadow goes past the bounds of the text, so if you are working in an environment with a high contrast background-foreground, it will be obvious. For lower contrast items, especially ones with the same hue, it's not too noticeable. For example, I've been able to make very nice looking etched text in metal backgrounds using this technique.

    0 讨论(0)
  • 2020-11-28 18:37

    For normal-sized text on small-ish buttons

    I found the other ideas on this page to lose their efficacy when used on small buttons with small text lettering. This answer expands on RobertPitt's answer.

    Here is the minor tweak:

    text-shadow: 1px 1px transparent, -1px -1px black;
    

    $('button').click(function(){
      $('button').removeClass('btnActive');
      $(this).addClass('btnActive');
    });
    body{background:#666;}
    .btnActive{
      border: 1px solid #aaa;
      border-top: 1px solid #333;
      border-left: 1px solid #333;
      color: #ecf0f8;
      background-color: #293d70;
      background-image: none;
      text-shadow: 1px 1px transparent, -1px -1px black;
      outline:none;
    }
    
    button{
      border: 1px solid #446;
      border-radius: 3px;
      color: white;
      background-color: #385499;
      background-image: linear-gradient(-180deg,##7d94cf,#1c294a 90%);
      cursor: pointer;
      font-size: 14px;
      font-weight: 600;
      padding: 6px 12px;
      xxtext-shadow: 1px 1px 2px navy;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <button class="btnActive">Option One</button>
    <button>Option Two</button>

    Note:

    I used this site to finesse the color shades.

    0 讨论(0)
  • 2020-11-28 18:40

    More precise explanation of the CSS in kendo451's answer.

    There's another way to get a fancy-hacky inner shadow illusion,
    which I'll explain in three simple steps. Say we have this HTML:

    <h1>Get this</h1>
    

    and this CSS:

    h1 {
      color: black;
      background-color: #cc8100;
    }
    

    It's a good slogan

    Step 1

    Let's start by making the text transparent:

    h1 {
      color: transparent;
      background-color: #cc8100;
    }
    

    It's a box

    Step 2

    Now, we crop that background to the shape of the text:

    h1 {
      color: transparent;
      background-color: #cc8100;
      background-clip: text;
    }
    

    The background is the text!

    Step 3

    Now, the magic: we'll put a blurred text-shadow, which will be in front of the background, thus giving the impression of an inner shadow!

    h1 {
      color: transparent;
      background-color: #cc8100;
      background-clip: text;
      text-shadow: 0px 2px 5px #f9c800;
    }
    

    We got it! Yay!

    See the final result.

    Downsides?

    • Only works in Webkit (background-clip can't be text).
    • Multiple shadows? Don't even think.
    • You get an outer glow too.
    0 讨论(0)
提交回复
热议问题