Skip navigation
CSS Tip! ✨ You can lean into form control state and layered backgrounds to create input status…
0:00 / 0:27

CSS Tip! ✨ You can lean into form control state and layered backgrounds to create input status…

jhey ʕ•ᴥ•ʔ

@jh3yy147K followers

Visit on X
1,826 likesDownloadOriginal post

239,700 views

CSS Tip! ✨ You can lean into form control state and layered backgrounds to create input status strokes 😎 input:valid { --valid: 1; } input { background: var(--bg) padding-box, ..., var(--valid-bg) calc((1 - var(--valid)) * 100cqi) 0 / 100% 100% border-box; } The trick is to layer up backgrounds using background-clip to leave a border space 🤏 Applying a transparent border gives you the space for the layered background to transition in 🎞️ Then you can use custom properties to dictate the background-position of each layer. By default, the valid and invalid layers are positioned outside the bounding box. But when one of those states is active, the background layer transitions in to 0 0. To respect people's animations preferences, we can wrap that transition in a media query 🤙 @​media (prefers-reduced-motion: no-preference) { input { transition: background-position: 0.5s; } } Was revisiting techniques around :has and :user-valid and it dawned that you could do it like this too! Hope you find it useful! p.s Don't rely on color to communicate form status. It's a cool trick though for an added touch ✨ CodePen.IO link below! 👇

9 Comments

Comments from the original post on X (Twitter)

  • jhey ▲🐻🎈's profile picture

    @jh3yy

    Here's that @CodePen link! 🚀 Should rock in all browsers 🤙

    55 likes

  • jhey ▲🐻🎈's profile picture

    @jh3yy

    Haha - Thanks Masda 🤙

    1 like

  • Pratyay Banerjee (নীল) 's profile picture

    @Neilzblaze007

    Ummmm

    2 likes

  • jhey ▲🐻🎈's profile picture

    @jh3yy

    Interestingly, that's valid. There are some funky email patterns that are valid for email. You can take it further with the "pattern" attribute if needed. Quite cool if you wanted to only accept email from certain domains, etc.

    7 likes

  • Marcel's profile picture

    @MarcelFromMimic

    How can you make tweets so long, for me if its too long it just doesnt work.

    2 likes

  • TypeScriptGuru's profile picture

    @TypeScriptGuru

    🔥🔥

    1 like

  • Sebastian's profile picture

    @hiiinternet

    @artur_bien

    1 like

  • Armin~the Vlone{tech} thug's profile picture

    @minraxoxo

    Wow

    1 like

  • Alex Leybourne's profile picture

    @AlexLeybourne

    Just when I think i’m pretty good and knowledgeable at css You somehow tech me something new almost every day 🤩

    1 like