Problem: Floating Label Input with Validation Message
Problem description
Given an HTML page containing a <div class="validate-wrapper"> that wraps an <input type="email" id="email" class="validate-input" placeholder=" " required> and a <label for="email" data-error="Please enter a valid email address">Email</label>, write CSS to:
Define CSS variables on
.validate-wrapper:--primary-color,--error-color, and--transition-duration.Set
.validate-wrappertoposition: relativewith toppaddingfor the floating label and bottompaddingfor error space.Style
.validate-inputwithwidth: 100%,padding,border: 1px solid #ccc,border-radius: 4px,background: #fff, and nooutline.Absolutely position the label inside the wrapper at
left: 0.5rem,top: 2.375rem, withtransform: translateY(-50%),background: #fff, and add transitions fortop,transform, andcolor.Float the label above the input using
.validate-input:focus + labeland.validate-input:not(:placeholder-shown) + label, settingtop: 1rem,transform: translateY(-50%) scale(0.8), andcolor: var(--primary-color).Style
label::afterto show the validation message usingcontent: attr(data-error), placed attop: 4.5rem,left: 0, withfont-size: 1.1rem,color: var(--error-color),opacity: 0, andtransform: translateY(-0.5rem).Reveal the error message when the input is invalid and unfocused using
.validate-input:invalid:not(:focus):not(:placeholder-shown) + label::after, settingopacity: 1andtransform: translateY(0).
Goal
Write CSS rules so that the label floats above the input on focus or when filled, and a red error message appears beneath the field when the input is invalid.
Constraints
Use CSS only (no JavaScript is allowed).
CSS variables should be
--primary-color: #0066cc;,--error-color: #cc0000;, and--transition-duration: 0.2s;.Floating label transforms to
scale(0.8)and moves totop: 0.Error message transitions should be
opacityandtransformover--transition-duration.Use
attr(data-error)incontentfor the error message.Ensure
pointer-events: noneon pseudo-elements.
Sample visual output
Here’s what the output would look like:
Good luck trying the problem! If you’re unsure how to proceed, check the Solution.
Problem: Floating Label Input with Validation Message
Problem description
Given an HTML page containing a <div class="validate-wrapper"> that wraps an <input type="email" id="email" class="validate-input" placeholder=" " required> and a <label for="email" data-error="Please enter a valid email address">Email</label>, write CSS to:
Define CSS variables on
.validate-wrapper:--primary-color,--error-color, and--transition-duration.Set
.validate-wrappertoposition: relativewith toppaddingfor the floating label and bottompaddingfor error space.Style
.validate-inputwithwidth: 100%,padding,border: 1px solid #ccc,border-radius: 4px,background: #fff, and nooutline.Absolutely position the label inside the wrapper at
left: 0.5rem,top: 2.375rem, withtransform: translateY(-50%),background: #fff, and add transitions fortop,transform, andcolor.Float the label above the input using
.validate-input:focus + labeland.validate-input:not(:placeholder-shown) + label, settingtop: 1rem,transform: translateY(-50%) scale(0.8), andcolor: var(--primary-color).Style
label::afterto show the validation message usingcontent: attr(data-error), placed attop: 4.5rem,left: 0, withfont-size: 1.1rem,color: var(--error-color),opacity: 0, andtransform: translateY(-0.5rem).Reveal the error message when the input is invalid and unfocused using
.validate-input:invalid:not(:focus):not(:placeholder-shown) + label::after, settingopacity: 1andtransform: translateY(0).
Goal
Write CSS rules so that the label floats above the input on focus or when filled, and a red error message appears beneath the field when the input is invalid.
Constraints
Use CSS only (no JavaScript is allowed).
CSS variables should be
--primary-color: #0066cc;,--error-color: #cc0000;, and--transition-duration: 0.2s;.Floating label transforms to
scale(0.8)and moves totop: 0.Error message transitions should be
opacityandtransformover--transition-duration.Use
attr(data-error)incontentfor the error message.Ensure
pointer-events: noneon pseudo-elements.
Sample visual output
Here’s what the output would look like:
Good luck trying the problem! If you’re unsure how to proceed, check the Solution.