skip to Main Content

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

2

Answers


  1. To achieve the effect of clipping a gradient background applied to a parent element so that it only covers the text of its children, you can utilize a combination of CSS properties. Specifically, setting the parent to have the gradient background and ensuring that the children are set to be transparent will allow the gradient to show through only the text.

    Here’s a possible workaround using CSS:

    Ensure the parent element has a gradient background.
    Use bg-clip-text and text-transparent on the child elements.
    Set the background-clip property on the parent to text for the text to show the gradient.

    <div class="flex flex-col w-full relative">
    <div class="mesh-gradient-background absolute inset-0 z-0"></div>
    <h1 class="animate-fade-up text-transparent bg-clip-text 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="animation-delay: 0.15s; animation-fill-mode: forwards;">
        Long Text 1
    </h1>
    <h1 class="transition-all duration-1000 animate-fade-up text-transparent bg-clip-text text-center font-display text-4xl font-bold tracking-[+0.03em] opacity-0 drop-shadow-sm [text-wrap:balance] md:text-7xl md:leading-[5rem] z-[10]"
        style="animation-delay: 0.15s; animation-fill-mode: forwards;">
        Long Text 2
    </h1>
    

    Make sure you have the following CSS for the gradient background:

    .mesh-gradient-background {
    
      background: linear-gradient(to right, #ff7e5f, #feb47b); /* Example gradient */
    
      -webkit-mask-image: linear-gradient(to right, rgba(0, 0, 0, 1), rgba(0, 0, 0, 1));
    
      mask-image: linear-gradient(to right, rgba(0, 0, 0, 1), rgba(0, 0, 0, 1));
    
      z-index: -1; /* Send the gradient behind the text */}
    
    Login or Signup to reply.
  2. Its is a potential bug in Chrome/Edge, though I’m not exactly sure if it disobeyed the draft spec or not. See transform scale not working with background clip and gradients or the issue on Chromium.

    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>

    After some testing with isolation:isolate, it appears to me that background-clip:text doesn’t count in 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. However, if you need to use drop-shadow, you probably need to use a whole nother approach.

    .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="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]">
        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>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search