Imagine you are building a simple search form, where a user types their search into an input box, and then clicks the search button to search.
Her...
For further actions, you may consider blocking this person and/or reporting abuse
CanIUse shows support for
:placeholder-shown
as pretty solid everywhere except IE (partial) and Edge (no support). If you don't have to support either of those browsers, then go for it! Otherwise, this is still a fantastic pure-CSS technique but should probably be paired with a JS fallback if the functionality is critical.I still wouldn't use it for a button (as exemplified) as you'd only be able to see it when the input is focused. For whatever reason, the user might tab away and then decide to run the search.
Accessbility-wise, it's not a good practice either. If you change the focus from the input to the button using tab, this technique would hide the button given the input is not focused anymore.
(edit: fix typo)
Those are both very good points I hadn't considered. Maybe visually show/hide the button on input focus, and then persist that state if the user has entered some input into the field?
Maybe this isn't the best example of the
:not(:placeholder-shown)
selector in use, but now it has me thinking about other ways this selector could be used to enhance rather than diminish accessibility.:placeholder-shown doesn't rely on focus so it would still work while navigating with the keyboard, but screen readers would still be lost if you wanted to dim the search button. Also, you'd have to use either the disabled attribute, or better, aria-disabled (it's considered bad practice to disable a button as you might not know what disabled it, so you should still be able click it and get a proper error message), but both would require some JS to dynamically change their value. So yeah, it can be a useful pseudo-class but it turns out to be tricky, rather than a "trick" ;-)
It doesn't seem so neat to have a button popping out on the page all of the sudden, as it makes more sense for it to be disabled, but nice information about the placeholder-shown
The "display: none;" style on the button is just a possibility: it may have been better to simply make the button slightly transparent initially, and have it be opaque when the user has typed something, but of course, there are so many ways to implement this.
For example, I am currently building a password manager, which uses the placeholder-shown trick to display an arrow icon for entering the password-vault when the user has typed content into the password field.
This is more practical implementation of the example in the article. I had used javascript for this effect in past projects. Do you have codepen for this?
Yes, I do! codepen.io/xtrp/pen/QWWGGOQ, see line 96 of CSS to see the :not(:placeholder-shown) trick in action. As I said, this is part of a password-manager desktop application I am currently building, which is open sourced on GitHub as well.
— Gabriel
According to usability experts, buttons should not appear and disappear, especially the crucial ones.
Source: smashingmagazine.com/printed-books...
You can float it through the input (position: absolute) and padding the input for preventing text go behind the button.....
I actually have 2 thoughts about this - first, is that actually better that validation? especially in the case when it is a login form that requires some front-end validation anyway?
Secondly, how do you handle hitting enter than? By default, input element is also submitted by pressing enter and as far as I know, there is no way to disable that without JS
In my experience, this trick can be applied to a vast array of things, not just forms that require validation. In the case with login form, of course, I wouldn't recommend using this trick, as the validation wouldn't be covered by :not(:placeholder-shown). In general, I've personally found this trick most useful in search forms, and forms that require little frontend validation such as a text box and button for posting a comment in a comments section.
Your second question is a great one, and brings up a point that I really haven't thought of that much myself. Personally, I would just disable the submit-on-enter functionality via JavaScript (as you said) and use the CSS trick on the side as well, but there may be a pure CSS way to accomplish this. Sorry that I couldn't provide a better answer, but great questions regardless.
//you can also simulate a disabled button
button {
background-color: blue;
color:white;
opacity:.5;
pointer-events:none;
}
input:not(:placeholder-shown) + button {
opacity:1;
pointer-events: all;
}
I am using this trick to move around
<label>
s and such. I love it, so much better than dealing with focus/blur stuff in js.This is so cool. I always try to avoid unnecessary JavaScript and use CSS instead if possible. And this is going to come in handy in my projects. Thanks for sharing this.
All the Css people :) won't love what I say.
My opinion, state should stay in the JavaScript realm as much as possible. State should be in one place!
It is ashame that css state is even promoted as a good thing. When a Js developer comes to develop feature, he will be surprised to find state in Css left there by a loving Css developer. And then, he will have to live with it or migrate it to Js.
This is pretty cool! I didn’t know that this existed. Pushing it into the memory bank for future projects 🎉
I didn't know... thanks for share, so useful
Really!! Easy trick with CSS than the JavaScript thanks for sharing
I've used this trick to create a material input with only html & css, you can see it on my personal site (in french) here : benjamin.caradeuc.info/2019/09/10/...
Very nice, thanks for the tip. I prefer to disable the button in order to not mess up the layout.
In order to achieve that I am using the pointer-events:none and the opacity to 80%
this is my trick to validate if a form is filled:
codepen.io/leandroruel/pen/oavYZB
Thanks for writing this. Even if it useful for the use-case you've exemplified but it can be used in other cases.
This is nice to know but I can't see myself using IRL.
Neat trick. I havent focused much on CSS in my endeavors. I really only learned both CSS and HTML as a necessity to implement JS. Go figure. I think I'll change that.
That is pretty brilliant! I will agree with what other people are saying though. Always best to cover your butt with un-supported browsers. :) Thanks for sharing though!
A little click-baity title. :P
I often use 'enter' to complete online forms, this method does noting to block this functionality (give it a try on codepen).
Please note that if the user clicks the spacebar, the placeholder will be hidden and the input is empty, so you need to make sure that the input has actual characters not white spaces.