跳过导航
Future CSS Tip! ⚡️ You can combine CSS :has() & :user-valid/:user-invalid to power micro…
0:00 / 0:32

Future CSS Tip! ⚡️ You can combine CSS :has() & :user-valid/:user-invalid to power micro…

jhey ʕ•ᴥ•ʔ

@jh3yy15万 位关注者

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

284,043 次观看

Future CSS Tip! ⚡️ You can combine CSS :has() & :user-valid/:user-invalid to power micro interactions 😍 No JavaScript. Watch for the little animations ✨ label { --color: var(--default); color: var(--color); border-color: var(--color); } .group:has(:user-valid) { --color: var(--valid); } .group:not(:focus-within):has(:user-invalid) { --color: var(--invalid); } These new pseudo-elements only fire once an input has been interacted with. This means you don't need to do things like the "transparent :placeholder-shown" hack from before 🙌 Then you can lean into the power of scoped custom properties and change the theme based on the status of a form group 😎 It's a nice little touch. But remember, don't rely on color to communicate things! CodePen.IO link below! 👇

10 条评论

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

  • jhey ▲🐻🎈 的头像

    @jh3yy

    Here's that @CodePen link! 🚀 Wildly. This one has "varied" support atm. It works in Safari Technology Preview ✅ In Chrome 118 it works to activate but then doesn't deactivate 🤔 In Firefox, the :has() flag in Nightly doesn't work for me atm 🥲

    20 个赞

  • jhey ▲🐻🎈 的头像

    @jh3yy

    You could already do this if you have access to :has() 👀 But it requires somewhat of a transparent placeholder hack 🤓 .group:not(:focus-within):has(:invalid:not(:placeholder-shown)) { ... } Here's the @CodePen for that way 👇

    11 个赞

  • Middle-belt Jay 的头像

    @DerSchickMann

    Man! 😲

    1 个赞

  • jhey ▲🐻🎈 的头像

    @jh3yy

    You can do a version with :has() alone. But it isn't as clean as using :user-valid/:user-invalid IMO 🤓

    1 个赞

  • Muwaffaq Elbadawi 的头像

    @MuwaffaqBadawi

    Pretty hot 🔥🔥🥵

    1 个赞

  • jhey ▲🐻🎈 的头像

    @jh3yy

    We're trying! 😍

    1 个赞

  • Helios Dev 的头像

    @HeliosCreator

    I wonder if you can combine this with :after { content: “invalid email”; } somewhere above the input.

    1 个赞

  • jhey ▲🐻🎈 的头像

    @jh3yy

    You could! 💯 An explicit element could be an easier solution to maintain. The idea here is also that I'm leaning into the built-in validation of the browser. So you would get the little browser built-in tooltips 🤓

    4 个赞

  • fa fa-code 2x 的头像

    @Adebobby_tech

    @pikaso_me screenshot this

    1 个赞

  • Pikaso 的头像

    @pikaso_me

    @jh3yy Here's your screenshot. ⚡ Want to automatically post your tweets on Instagram? Visit