Skip navigation
CSS Trick! 🤙 You can create gradient borders on translucent elements using mask-clip and…
0:00 / 0:37

CSS Trick! 🤙 You can create gradient borders on translucent elements using mask-clip and…

jhey ʕ•ᴥ•ʔ

@jh3yy149K followers

Visit on X
2,321 likesDownloadOriginal post

270,066 views

CSS Trick! 🤙 You can create gradient borders on translucent elements using mask-clip and mask-composite with a pseudo-element 🔥 .gradient-border::after { mask-clip: padding-box, border-box; mask-composite: intersect; mask: linear-gradient(transparent, transparent), linear-gradient(white, white); } It's the same "Transparent border trick" from before. But, now you apply it to a pseudo-element 😎 The trick is to create a pseudo-element with a gradient background and then mask it so we only see the part we want, the border ✨ mask-clip defines the area affected by a mask. Similar to how you can define background-size. Using padding-box and border-box constrains the two masks. mask-composite is the magic part ✨ It defines a compositing operation for stacked mask layers. Using intersect means that the parts that overlap get replaced. And this seems to work in all browsers 🙌 As for the rest of the styles... – Make sure you set pointer-events: none on the pseudo-element – Make sure it fills the parent element. You can use position: absolute and inset: 0 – Make sure the background fills the space including the border-width. You can use calc to achieve that: --bg-size: calc(100% + (2px * var(--border))); background: var(--gradient) center center / var(--bg-size) var(--bg-size); That's it! 🚀 Gradient borders on translucent elements. You can set all the backdrop-filter: blur() you like! 😅 CodePen.IO link below! 👇

11 Comments

Comments from the original post on X (Twitter)

  • jhey ▲🐻🎈's profile picture

    @jh3yy

    Here's that @CodePen link! 🤙 Should work fine across the board. Tried it out in Chrome, Firefox, and Safari ⭐️ ʕ – ᴥ – ʔ

    81 likes

  • Ana Tudor 🐯🖤🌻's profile picture

    @anatudor

    "Using intersect means that the parts that overlap get replaced." That's actually incorrect. `intersect` means the alphas get multiplied where they intersect. I explained it here in detail a few years back In this case, in the padding area, it's 0*1 = 0.

    7 likes

  • Alohe's profile picture

    @alemalohe

    That's cool, but can you do this?

    6 likes

  • jhey ▲🐻🎈's profile picture

    @jh3yy

    Yes. Yes I can.

    5 likes

  • SILAS PATH's profile picture

    @silaspath

    incredible. your comprehensive deconstruction is legendary. this is pushing dev forward.

    2 likes

  • Nicholas Ryan Howard's profile picture

    @NicholasRyanH

    Did you ever release a solution to have an *animated* gradient border with translucent elements? I think a while back you said that was coming, but I never saw it drop!

    2 likes

  • jhey ▲🐻🎈's profile picture

    @jh3yy

    this technique right here can handle that 🤙 then you can animate the border with a conic-gradient or using offset-path depending on the type of effect you're looking for ✅ not sure if posted, but definitely spoke about it at Config

    2 likes

  • Erik Rasmussen 👨‍💻🇺🇸🇪🇸's profile picture

    @erikras

    Dude.

    1 like

  • Armando J. Perez-Carreno's profile picture

    @perezcarreno

    These demos are awesome, @jh3yy , keep pushing the limits!

    1 like

  • //pentagear.gg's profile picture

    @Pentagear

    Can I Use is saying mask-composite isn't supported?

    1 like

  • jhey ▲🐻🎈's profile picture

    @jh3yy

    There's the prefixed version too 🤙 autoprefixer will convert to this 👇 -webkit-mask-composite: source-in, xor; mask-composite: intersect; It does seem to work across the board

    3 likes