26
loading...
This website collects cookies to deliver better user experience
div
element instead.input
element and its related div
s inside a container. To create a placeholder, we have defined a separate div
which will act as a placeholder rather than using the :placeholder
pseudo-element.<div class="input-contain">
<input type="text" id="fname" name="fname" autocomplete="off" value="" aria-labelledby="placeholder-fname">
<label class="placeholder-text" for="fname" id="placeholder-fname">
<div class="text">First Name</div>
</label>
</div>
input
element and it's container..input-contain{
position: relative;
}
input{
height: 5rem;
width: 40rem;
border: 2px solid black;
border-radius: 1rem;
}
input
element by setting the position of the placeholder text to absolute
so that it matches the width and height of the input
container..placeholder-text{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
border: 3px solid transparent;
background-color: transparent;
display: flex;
align-items: center;
}
pointer-events
to none
for the placeholder element..placeholder-text{
pointer-events: none;
}
.text{
font-size: 1.4rem;
padding: 0 0.5rem;
background-color: transparent;
color: black;
}
input, .placeholder-text{
font-size: 1.4rem;
padding: 0 1.2rem;
}
border-color
rather than keeping the outline on focus event.input:focus{
outline: none;
border-color: blueviolet;
}
font-size
a little bit when the input element is focused. Also, we can change the color of the placeholder text. Here's how we can do that. Change the background-color
to resemble the surrounding color to make it more elegant.input:focus + .placeholder-text .text{
background-color: white;
font-size: 1.1rem;
color: black;
transform: translate(0, -170%);
border-color: blueviolet;
color: blueviolet;
}
transition
property to the placeholder text..text{
transform: translate(0);
transition: transform 0.15s ease-out, font-size 0.15s ease-out, background-color 0.2s ease-out, color 0.15s ease-out;
}
value
attribute for the input
element. This will come handy.<input type="text" id="fname" name="fname" autocomplete="off" value="" aria-labelledby="placeholder-fname">
:not
pseudo-class on the input element value. Here's how we can do that.input:focus + .placeholder-text .text, :not(input[value=""]) + .placeholder-text .text{
background-color: white;
font-size: 1.1rem;
color: black;
transform: translate(0, -170%);
}
input:focus + .placeholder-text .text{
border-color: blueviolet;
color: blueviolet;
}
value
attribute to the string entered by the user just like this,let input_element = document.querySelector("input");
input_element.addEventListener("keyup", () => {
input_element.setAttribute("value", input_element.value);
})