Search code examples
htmlcssnext.jstailwind-css

How to clip a gradient to Text over the parent element?


I want to clip the gradient of the parent element to the children but it doesnt quite work. either the gradient shows completely (not only covers the text) or my other, normal background is the background it uses to clip (so completely white). i basically want the same gradient to be applied to both h1, so the patterns remain is there a workaround?

<div className="flex flex-col w-full mesh-gradient-background bg-clip-text z-10">
   <h1 className="animate-fade-up bg-clip-text text-transparent text-center font-display text-2xl font-bold tracking-[-0.03em] opacity-0 drop-shadow-sm [text-wrap:balance] md:text-5xl md:leading-[5rem] z-[10]" 
    style={{ animationDelay: "0.15s", animationFillMode: "forwards" }}>
            Long Text 1
   </h1>
   <h1 className="transition-all duration-1000 animate-fade-up text-center font-display text-4xl font-bold tracking-[+0.03em] text-transparent opacity-0 drop-shadow-sm [text-wrap:balance] md:text-7xl md:leading-[5rem] bg-size-200 bg-pos-0 hover:bg-pos-100 w-min cursor-default"
   style={{ animationDelay: "0.15s", animationFillMode: "forwards" }}>
            Long Text 2
   </h1>
</div>

I also tried making the div absolute but the problem remained


Solution

  • This involves a potential bug in Chrome, Edge, and the latest Safari. See transform scale not working with background clip and gradients or the bug report on Chromium.

    Same gradient applied separately:

    If you want the same gradient to be applied to both h1, you can just set the background on h1 instead. (I've changed the drop-shadow to make it visible, but you can change it back to whatever you want.) However, if what you meant is to have a shared gradient background, then see the second half of the answer.

    Either way, you should move the opacity animation to the background div instead.

    .mesh-gradient-background {
      background: linear-gradient(to right, purple, orange);
    }
    
    .animate-fade-up {
      animation: fade-up 1s;
    }
    
    @keyframes fade-up {
      from {
        opacity: 0;
      }
      to {
        opacity: 1;
      }
    }
    <script src="https://cdn.tailwindcss.com"></script>
    
    <div class="grid w-full z-10 opacity-0 animate-fade-up" 
         style="animation-delay:0.15s; animation-fill-mode:forwards">
      <h1 class="bg-clip-text text-transparent text-center font-display text-2xl font-bold 
                 w-max justify-self-center tracking-[-0.03em] [text-wrap:balance] md:text-5xl 
                 md:leading-[5rem] mesh-gradient-background z-[10] bg-clip-text 
                 drop-shadow-[0px_8px_8px_pink]">
        Long Text 1
      </h1>
      <h1 class="transition-all duration-1000 text-center font-display text-4xl font-bold 
                 tracking-[+0.03em] text-transparent [text-wrap:balance] 
                 md:text-7xl md:leading-[5rem] bg-size-200 bg-pos-0 hover:bg-pos-100 w-min 
                 cursor-default mesh-gradient-background bg-clip-text drop-shadow-[0px_8px_8px_pink]">
        Long Text 2
      </h1>
    </div>


    Shared gradient background:

    After some testing with isolation:isolate, it appears to me that background-clip:text doesn't reflect texts of the child elements that forms a new stacking-context. Hence, any properties on the h1 that causing a new stacking-context to form including opacity, z-index, and drop-shadow will make the text being ignored.

    For opacity it is easy to solve in your case. It can be applied to the background itself. And since z-index is not needed, just remove it. This however requires to remove the drop-shadow.

    .mesh-gradient-background {
      background: linear-gradient(to right, purple, orange);
    }
    
    .animate-fade-up {
      animation: fade-up 1s;
    }
    
    @keyframes fade-up {
      from {
        opacity: 0;
      }
      to {
        opacity: 1;
      }
    }
    <script src="https://cdn.tailwindcss.com"></script>
    
    <div class="flex flex-col w-full mesh-gradient-background bg-clip-text z-10 opacity-0 animate-fade-up" style="animation-delay:0.15s; animation-fill-mode:forwards">
      <h1 class="text-transparent text-center font-display text-2xl font-bold 
                  tracking-[-0.03em] [text-wrap:balance] md:text-5xl md:leading-[5rem]">
        Long Text 1
      </h1>
      <h1 class="transition-all  duration-1000 text-center font-display text-4xl font-bold 
                tracking-[+0.03em] text-transparent [text-wrap:balance] md:text-7xl 
                md:leading-[5rem] bg-size-200 bg-pos-0 hover:bg-pos-100 w-min cursor-default">
        Long Text 2
      </h1>
    </div>

    To include the drop-shadow with background-clip using a shared linear background, one potential solution is to use background-attachment:fixed. However, the background position and size will be relative to the viewport which might not be ideal.

    .mesh-gradient-background {
      background: linear-gradient(to right, purple, orange);
      background-attachment: fixed;
      background-size: 100% 100%;
    }
    
    .animate-fade-up {
      animation: fade-up 1s;
    }
    
    @keyframes fade-up {
      from {
        opacity: 0;
      }
      to {
        opacity: 1;
      }
    }
    <script src="https://cdn.tailwindcss.com"></script>
    
    <div class="flex flex-col w-full z-10 opacity-0 animate-fade-up" style="animation-delay:0.15s; animation-fill-mode:forwards">
        <h1 class="bg-clip-text text-transparent text-center font-display text-2xl font-bold 
                    tracking-[-0.03em] [text-wrap:balance] md:text-5xl md:leading-[5rem] 
                    mesh-gradient-background drop-shadow-[0px_8px_8px_pink]">
        Long Text 1
      </h1>
      <h1 class="transition-all bg-clip-text duration-1000 text-center font-display text-4xl font-bold 
                  tracking-[+0.03em] text-transparent [text-wrap:balance] md:text-7xl 
                  md:leading-[5rem] bg-size-200 bg-pos-0 hover:bg-pos-100 w-min cursor-default 
                  mesh-gradient-background drop-shadow-[0px_8px_8px_pink]">
        Long Text 2
      </h1>
    </div>