CSS Tip! ✨ You can lean into form control state and layered backgrounds to create input status…
jhey ʕ•ᴥ•ʔ
在 X 上查看@jh3yy15万 位关注者
239,700 次观看
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 条评论
评论来自 X (Twitter) 上的原帖

@jh3yy
Here's that @CodePen link! 🚀 Should rock in all browsers 🤙
55 个赞

@jh3yy
Haha - Thanks Masda 🤙
1 个赞

@Neilzblaze007
Ummmm
2 个赞

@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 个赞

@MarcelFromMimic
How can you make tweets so long, for me if its too long it just doesnt work.
2 个赞

@TypeScriptGuru
🔥🔥
1 个赞

@hiiinternet
@artur_bien
1 个赞

@minraxoxo
Wow
1 个赞

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