CSS :placeholder-shown class

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
I never hear about this, quite useful for this option! I'll use in a future project for sure to highlight required fields without any JS.
Yeah I think nowadays if you don't have to support IE, you can make superb websites no-js required!
Thanks, Mohd,
You could technically use it to showcase required fields that have no value.
Might not be enough in UX, but def a helpful argument. You could also do extensive extra css art if you're into that.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Hey everyone, let's talk about placeholders. The wonderful addition to form elements.
Today we won't be talking about them as accessibility issues and hazards, but just on how to style the inputs that have them.
In CSS we can style the actual placeholder text by using the ::placeholder pseudo-element.
But did you know there is also a pseudo-class called :placeholder-shown? This will select the actual input field and style that, therefore we can all of a sudden add borders and stuff!
Our end result will be an input field that is styled based on the fact that the placeholder is shown. Once we type this styling should be removed.

Let's first start by creating a basic HTML to render two input fields in, once will have a placeholder and one will have a value.
<div class="container">
<input type="text" value="I have a value" />
<input type="text" placeholder="I have a placeholder" />
</div>
Oke let's first add some basic styling to our page:
body {
min-height: 100vh;
background: #efe9e7;
display: flex;
justify-content: center;
align-items: center;
}
.container {
background: #dae0f2;
padding: 2rem;
border-radius: 1rem;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.container input {
font-size: 1.5rem;
padding: 0.5rem;
margin: 0.5rem 0;
border-radius: 0.5rem;
}
By running this, we will get a basic form that will look like the image below.

Now how can we make the one where the placeholder is active render differently?
.container input:placeholder-shown {
border: 5px dashed teal;
}
Now we should see a dashed teal border, and once we put a value in it, that border should disappear!
To recap we can use a ::placeholder pseudo-element to change the actual placeholder text styling:
input::placeholder {
color: teal;
}
And we can use the :placeholder-shown pseudo-class to style the actual input styling.
input:placeholder-shown {
border: 5px dashed teal;
}
You can find a full demo to play with on this Codepen.
The main browser support this fully, of course, IE has to be a pain in the ass, I would suggest using this as a nice addition, but don't fully rely on it.

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter