跳过导航
CSS Tip! 💪 You can create these tab controls with CSS :has() + radio buttons ✨…
0:00 / 0:48

CSS Tip! 💪 You can create these tab controls with CSS :has() + radio buttons ✨…

jhey ʕ•ᴥ•ʔ

@jh3yy15万 位关注者

在 X 上查看
1,949 个赞下载原帖

437,931 次观看

CSS Tip! 💪 You can create these tab controls with CSS :has() + radio buttons ✨ .tabs:has(input:nth-of-type(3)) { --count: 3; } .tabs:has(:checked:nth-of-type(3)) { --active: 2; } .tabs::after { translate: calc(var(--active, 0) * 100%) 0; width: calc(100% / var(--count)); } Two CSS :has() tricks here combined with a rendering trick 🤙 The tab control is a container using display: grid. You can use :has() to count the number of tabs in the container: .tabs:has(input:nth-of-type(3)) { --count: 3; } .tabs:has(input:nth-of-type(4)) { --count: 4; } Using the cascade, the last valid :has() gives you the number of tabs 🫶 Once you know the number of tabs, you know how to size the indicator: .tabs::after { content: ""; position: absolute; height: 100%; width: calc(100% / var(--count)); } It's a pseudoelement that uses --count to determine its size 📏 The next :has() trick is determining which tab is active or :checked as it's an input [type=radio] .tabs:has(:checked:nth-of-type(2)) { --active: 1; } .tabs:has(:checked:nth-of-type(3)) { --active: 2; } You can use a zero-indexed translation here. If the second input is :checked, set --active: 1, then translate the pseudoelement on the tabs to that position 👉 .tabs::after { translate: calc(var(--active, 0) * 100%) 0; } The last rendering trick is using mix-blend-mode 👀 The tabs have a black background-color, the pseudoelement is white, and the label text is white. When you use mix-blend-mode: difference on the pseudoelement it will give this effect that the text transitions from white to black sliding across 😎 .tabs::after { color: hsl(0 0% 100%); mix-blend-mode: difference; } You can totally mix up the colors here though and go with a different effect. The mechanics of how you can use CSS :has() is the main point here 🙏 As always, any questions, suggestions, etc. let me know CodePen.IO link below! 👇 (There's even a Tailwind CSS play for this one too 👀)

9 条评论

评论来自 X (Twitter) 上的原帖

  • jhey ▲🐻🎈 的头像

    @jh3yy

    Here's that @CodePen link! 🚀 Remember, CSS :has() is available in all the major browsers now 👀 It's a great time to start getting familiar with it ⭐️

    108 个赞

  • toprakcavusdag.js 的头像

    @toprakcavusdag

    That's super. Thank you for sharing us

    4 个赞

  • jhey ▲🐻🎈 的头像

    @jh3yy

    More than welcome! 🙏

    1 个赞

  • Kay 🇮🇪 的头像

    @pexlledn

    Tried to find it for tailwind but no luck!?

    2 个赞

  • Carlito 的头像

    @crypitos

    Sorcery!

    2 个赞

  • JEM 的头像

    @Jacksonmills

    still wrapping my head around :has() 😵‍💫

    2 个赞

  • jhey ▲🐻🎈 的头像

    @jh3yy

    Wrote this one in 2022 that might help 🤙 Got some much cooler demos to show off for it now though 😁

    4 个赞

  • randomhuman 的头像

    @___randomhuman

    Beautiful 👌

    1 个赞

  • Souvik Bhattacharjee 的头像

    @iamsouvik_b

    Nice 🔥

    1 个赞