Pseudo and pseudon’t

I like CSS pseudo-classes. They come in handy for adding little enhancements to interfaces based on interaction.

Take the form-related pseudo-classes, for example: :valid, :invalid, :required, :in-range, and many more.

Let’s say I want to adjust the appearance of an element based on whether it has been filled in correctly. I might have an input element like this:

<input type="email" required>

Then I can write some CSS to put green border on it once it meets the minimum requirements for validity:

input:valid {
  border: 1px solid green;
}

That works, but somewhat annoyingly, the appearance will change while the user is still typing in the field (as soon as the user types an @ symbol, the border goes green). That can be distracting, or downright annoying.

I only want to display the green border when the input is valid and the field is not focused. Luckily for me, those last two words (“not focused”) map nicely to some more pseudo-classes: not and focus:

input:not(:focus):valid {
  border: 1px solid green;
}

If I want to get really fancy, I could display an icon next to form fields that have been filled in. But to do that, I’d need more than a pseudo-class; I’d need a pseudo-element, like :after

input:not(:focus):valid::after {
  content: '✓';
}

…except that won’t work. It turns out that you can’t add generated content to replaced elements like form fields. I’d have to add a regular element into my markup, like this:

<input type="email" required>
<span></span>

So I could style it with:

input:not(:focus):valid + span::after {
  content: '✓';
}

But that feels icky.

Update: See this clever flexbox technique by Kitty Giraudel for a potential solution.

Have you published a response to this? :

Responses

SelenIT

input:not(:focus):valid — чтобы «светофор» валидации поля не отвлекал при наборе. Полезная идея Джереми Кита: adactio.com/journal/10000

# Posted by SelenIT on Friday, December 18th, 2015 at 8:28am

Ryan

Oh yea good call! input:not(:focus):valid - show validation styles when the value’s accepted but no longer focused adactio.com/journal/10000

# Posted by Ryan on Friday, December 18th, 2015 at 2:38pm

Si

A some-what icky but neat trick to styling valid inputs in a less obtrusive manner adactio.com/journal/10000

# Posted by Si on Monday, January 4th, 2016 at 2:21pm

2 Likes

# Liked by Marc Drummond on Thursday, December 17th, 2015 at 6:57pm

# Liked by Kitty Giraudel on Sunday, March 7th, 2021 at 5:13pm

Related posts

Who knows?

Had you heard of these bits of CSS? Me too/neither!

Assumption

Separate your concerns.

Culture and style

Styling a document about The Culture novels of Iain M Banks.

Supporting logical properties

Using the CSS trinity of feature queries, logical properties, and unset.

Let’s get logical

Let me hear your blocky talk.

Related links

6 CSS Snippets Every Front-End Developer Should Know In 2025 · 19 January 2025

  • Springy easing with linear()
  • Typed custom properties
  • View transitions for page navigation
  • Transition animation for dialog and popover
  • Transition animation for details
  • Animated adaptive gradient text

Tagged with

Justified Text: Better Than Expected? – Cloud Four

Some interesting experiments in web typography here.

Tagged with

CSS wants to be a system - daverupert.com

CSS wants you to build a system with it. It wants styles to build up, not flatten down.

Truth!

Tagged with

My Modern CSS Reset | jakelazaroff.com

I like the approach here: logical properties and sensible default type and spacing.

Tagged with

Printing music with CSS grid

Laying out sheet music with CSS grid—sounds extreme until you see it abstracted into a web component.

We need fluid and responsive music rendering for the web!

Tagged with

Previously on this day

11 years ago I wrote Sasstraction

The design of CSS.

12 years ago I wrote Hackfarming Politmus

Eighteen go to Dorset.

16 years ago I wrote Welcome to the machine tag

More Flickr API methods to play with.

22 years ago I wrote Hobbits and Macs

Four hobbits gathered ‘round an iMac.

23 years ago I wrote Monty Python and LEGO

Here’s a LEGO version of the Camelot song from Monty Python And The Holy Grail (from the people who brought you the one minute long version of 2001:A Space Odyssey in LEGO).