πŸ“œ ⬆️ ⬇️

Pop-up tags in forms on pure CSS

Perhaps you already came across this technique. This is an input field that looks as if it has a text prompt (placeholder), but when you start typing, it does not disappear, but moves to the side. I like this idea. Brad Frost wrote a very good article about this technique, examining in detail the pros and cons.

Most examples of using this technique rely on JavaScript. One fine day I went to nest.com, saw this technique there and thought: is it possible to do the same without JavaScript? And that's what came of it.

Here is the form on nest.com:
')
image

My version you can look at CodePen . Of course, it doesn’t look as cool as that of Nest, where the text smoothly disappears and the label comes out from below. Of course, I could have done the same with JavaScript, as probably with pure CSS, but the purpose of the article is to show the principle, and you can bring beauty to yourself.

There are two reasons why it makes sense to use this technique:

  1. Saving space. Since the input field and its label are combined, they take up less space. When entering a field gets into focus, in order to still see both, you can take up some space inside the field, or temporarily expand the space for that particular input field at the expense of others.
  2. The entire input field becomes one big button. Of course, the usual input field reacts like a click, and if you specify the for attribute for label , the label becomes active, but there is something very attractive in a large rectangle that invites you to click on it and start typing. This is especially good for mobile apps.

Although in the general case constantly visible labels are better, but, if properly implemented, this technique can sometimes be appropriate. However, there is always the risk of screwing up with accessibility, so be careful. Another disadvantage of this approach is that you cannot place information in placeholder that complements the label (for example, the label is β€œphone number”, and the hint is "(555) 555-5555").

Trick β„–1 - a label as a hint


Items
     (    ,           )   .       ,   ,         .     ,     ,     ,      . 

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

     (    ,           )   .       ,   ,         .     ,     ,     ,      . 

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

     (    ,           )   .       ,   ,         .     ,     ,     ,      . 

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

     (    ,           )   .       ,   ,         .     ,     ,     ,      . 

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

     (    ,           )   .       ,   ,         .     ,     ,     ,      . 

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>
, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

     (    ,           )   .       ,   ,         .     ,     ,     ,      . 

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

image
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

β„–2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
β„–3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

image
, . JavaScript: , , maman . , , , . CodePen.

Source: https://habr.com/ru/post/216669/


All Articles