I am trying to create a floating label input field, i am able to achieve but on click of outside the input, the label text is over-aligning the input field. How could i make the label on the top when there is text or cursor active in input field.
Input.js
import React from "react";
import { FloatingContainer, FloatingInput, FloatingLabel } from "./style.js";
export const StyledFloatingInput = () => {
return (
<FloatingContainer>
<FloatingInput />
<FloatingLabel>Text</FloatingLabel>
</FloatingContainer>
);
};
style.js
import styled from "@emotion/styled";
export const FloatingContainer = styled.div`
position: relative;
margin-bottom: 20px;
&::before,
&::after {
box-sizing: border-box;
}
`;
export const FloatingInput = styled.input`
font-size: 14px;
padding: 4px 4px;
display: block;
width: 100%;
height: 30px;
background-color: transparent;
border: none;
border-bottom: 1px solid #757575;
&:focus {
outline: none;
border-bottom: 2px solid #5264ae;
}
&:focus ~ label {
top: -18px;
font-size: 14px;
color: #5264ae;
}
`;
export const FloatingLabel = styled.label`
color: #999;
font-size: 14px;
font-weight: normal;
position: absolute;
pointer-events: none;
left: 5px;
top: 5px;
transition: 0.2s ease all;
-moz-transition: 0.2s ease all;
-webkit-transition: 0.2s ease all;
`;
Here is the codesandbox what i have tried so far. Any help is appreciated