I am building a little custom input for my project, where the it has a little label that moves around when the user interacts with it. So far it has been working ok , but I noticed if the field is invalid and the user removes focus from the input the label i have set moves back to it's original position and goes over the actual input text.
See what I mean here - this field is type email and if you type in just "a" for example and unfocus the input, the label will fall back down over the input's text
http://codepen.io/ajmajma/pen/RobGbj?editors=1100
Here is the html I have so far :
<div class="container">
<form>
<div class="test-input">
<input type="email" required>
<label>Email</label>
</div>
</form>
</div>
And the scss for the input:
.test-input {
position:relative;
margin-bottom:45px;
input {
font-size:14px;
height: 50px;
padding:20px 10px 10px 10px;
display:block;
width:300px;
border:none;
border:1px solid #979797;
}
label {
color:#000;
font-size:14px;
font-weight:normal;
position:absolute;
pointer-events:none;
left:15px;
top:15px;
transition:0.2s ease all;
-moz-transition:0.2s ease all;
-webkit-transition:0.2s ease all;
}
input:focus ~ label, input:valid ~ label {
top: 6px;
left: 10px;
font-size:12px;
color: #999;
}
}
I'm not sure I want to use that required
tag itself, but it is enabling me to keep the label flag above the text when the user leaves the focus of the input itself.
It would be great to not have to be dependent on the required tag as some of the fields won't actually be required in the form they are being used in. Is there another was to approach/fix this ? I looking into the css3 psuedo class selectors (http://www.w3schools.com/Css/css_pseudo_classes.asp) for inputs but I'm not sure what I could use to more specifically target the use case I am looking for. Thanks for reading!