DEV Community

CSS-only floating label

Paul on November 01, 2017

One of those nifty things Google did with the Material Design guidelines is include floating labels. Don't know what a floating label is? Check it ...
Collapse
 
ben profile image
Ben Halpern

You're correct about the lack of CodePen tag, will try to get it up soon.

Collapse
 
gwillz profile image
gwillz • Edited

I believe there is a way to get around the required requirement.
Use .form-control:not(:placeholder-shown) + .form-control-placeholder.

On another note, :pseudo + .sibling doesn't work in IE or Edge.

Collapse
 
peiche profile image
Paul

Since writing this, I have learned a lot about the :placeholder-shown pseudo-class. Thanks!

Collapse
 
thesheebs profile image
sheba

...and placeholder-shown is experimental and does not work with IE or Edge.
developer.mozilla.org/en-US/docs/W...

Collapse
 
gwillz profile image
gwillz

It gets worse.

IE has :-ms-input-placeholder as a selector for :placeholder-shown and Edge does not.

Whereas Edge has ::-ms-input-placeholder as an element for ::placeholder and IE does not.

Collapse
 
rpalo profile image
Ryan Palo

Shoop πŸ‘ŒπŸ»

Collapse
 
lexlohr profile image
Alex Lohr

A small drawback is that you now can't use the form constraints API anymore to validate in the front-end.

Collapse
 
mcheff profile image
McHeff

Well done dude! This is perfect for what I was after!

Collapse
 
davidshare profile image
David Itam Essien

This doesn't work for me.