@trafiki
Posted
Hi, I;m curious, how did you achieve the email validation without JS. How did you make it display the error message and icon without js
@ApplePieGiraffe
Posted
@trafiki
Hey!
The CSS-only form validation styles are done with this bit of Sass (mostly thanks to the :invalid
pseudo-selector and CSS combinators):
.email-input {
position: relative;
height: 7rem;
padding-left: 2.25rem;
border: 1px solid var(--color-desaturated-red);
border-radius: 10rem;
background-color: rgba(0, 0, 0, 0);
outline: none;
&:focus:invalid {
border: 2px solid var(--color-soft-red);
}
&:focus:invalid + img {
opacity: 1;
}
&:focus:invalid ~ span {
opacity: 1;
}
&::placeholder {
color: var(--color-desaturated-red);
font-family: 'Josefin Sans', sans-serif;
font-weight: lighter;
}
}
Basically, there a couple of elements that are always present but are just made invisible when the input element is not in focus or has valid input. :focus
and :invalid
detect whether or not the input element is in focus and has invalid input, and changes the opacity of those invisible elements accordingly.