I have an input fields with floating labels. My HTML for each field is:
<div class="input-container">
<input placeholder="Email" type="text" name="email" id="email">
<label for="email">Email:</label>
</div>
In my css I added rules so that the labels are shown half-way on top of the input fields on focus of the input field, and when a placeholder is no longer shown. This is what it looks like when the fields are filled out:
Whenever an input field is focused, a box shadow is visible on the input field with the following scss:
input {
position: relative;
background: #fff;
display: block;
z-index: 2;
&:focus {
border: solid 1px $primary;
box-shadow: 0 0 3px 2px rgba($primary, .8);
//....
}
I wish to show the same shadow on the top-half of my label so it would show everywhere the white color meets the darker color. My idea was to create a pseudo-element that is half the height of the label, with a lower z-index than both the label and the input field. I did this by adding to the &:focus
:
& ~ label {
background-color: white;
z-index: 3;
&:before {
content: "";
width: 100%;
height: .5em;
border: solid 1px $primary;
box-shadow: 0 0 3px 2px rgba($primary, .8);
position: absolute;
left: 0;
top: 0;
z-index: 1;
}
Though the result on focus is that the pseudo-element is on top of everything, casting a shadow on top of my label:
Having read this question and this question I tried setting z-indexes and creating a wrapper, but without success. Even though the z-index for the pseudo-element is set to -1, it still appears on top of everything else. How to make sure the pseudo-element is below my label and input field? Here is also a JSFiddle.