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

0

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>

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 */}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật