P
P
Pavel_Proca2016-11-03 12:32:12
css
Pavel_Proca, 2016-11-03 12:32:12

How to change text styles if text is entered in the input field?

There is a simple code. When an input is clicked, its label changes position, moving up. It is necessary that after the text is entered in the input, its label remains in the same place, that is, at the top. And if the text was not entered, the label successfully descended back. Tried various pseudo-classes but didn't help. Maybe it's my hands from one place)
Here is the code:

<div>
<input type="text" id="name">
<label for="name">Your Name</label>
</div>

label {
  position: absolute;
  top: 2%;
  left: 3%;
}
input:focus + label {top: -1%;}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Tenebrius, 2016-11-03
@Tenebrius

Only via JS
stackoverflow.com/questions/3617020/matching-an-em...

C
ccharmai, 2020-09-08
@ccharmai

It is possible, but with a small crutch. Add required to input and add input:valid to css

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question