Problem: Custom Floating Label Input
Problem description
Given an HTML page containing a div with class float-label that wraps an <input type="text" class="floating-input" placeholder=" " id="username" required> and its associated <label for="username">Username</label>, write CSS to:
Position the label inside the input field so it appears as placeholder text.
Use the
.float-labelwrapper’s:focus-withinor the input’s:focusand:not(:placeholder-shown)states to trigger styling.Animate the
labelto move above the input (e.g.,top: 0), scale down (e.g.,scale(0.8)), and changecolor(e.g.,#0066cc).Ensure smooth transitions for
position,transform, andfont-size.
Goal
Write CSS rules that create a text input where the label floats up and shrinks when the user focuses the field or has entered text, mimicking a material-design style floating label.
Constraints
Use only CSS (no JavaScript).
Input dimensions should be as follows:
width100%,padding0.75rem 0.5rem,border1px solid #ccc, andborder-radius4px.Label initial should be as follows: positioned absolutely at left
0.5rem, top50%, transformedtranslateY(-50%),font-size1rem, andcolor#666.Floated
labelshould be as follows: top0, transformedtranslateY(-100%) scale(0.8),font-size0.8rem, andcolor#0066cc.Transitions should be as follows:
top,transform, andfont-sizeover0.2s ease.
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: Custom Floating Label Input
Problem description
Given an HTML page containing a div with class float-label that wraps an <input type="text" class="floating-input" placeholder=" " id="username" required> and its associated <label for="username">Username</label>, write CSS to:
Position the label inside the input field so it appears as placeholder text.
Use the
.float-labelwrapper’s:focus-withinor the input’s:focusand:not(:placeholder-shown)states to trigger styling.Animate the
labelto move above the input (e.g.,top: 0), scale down (e.g.,scale(0.8)), and changecolor(e.g.,#0066cc).Ensure smooth transitions for
position,transform, andfont-size.
Goal
Write CSS rules that create a text input where the label floats up and shrinks when the user focuses the field or has entered text, mimicking a material-design style floating label.
Constraints
Use only CSS (no JavaScript).
Input dimensions should be as follows:
width100%,padding0.75rem 0.5rem,border1px solid #ccc, andborder-radius4px.Label initial should be as follows: positioned absolutely at left
0.5rem, top50%, transformedtranslateY(-50%),font-size1rem, andcolor#666.Floated
labelshould be as follows: top0, transformedtranslateY(-100%) scale(0.8),font-size0.8rem, andcolor#0066cc.Transitions should be as follows:
top,transform, andfont-sizeover0.2s ease.
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.