On this page10 sections
Lecture 024: Styling Forms
| Category | 5: Pseudo-classes & Components |
| Lecture | 024 of 057 |
| Estimated video length | 12 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
Default form fields look different in every browser and often a bit old-fashioned. With some CSS they become consistent and friendly: full-width boxes, clear focus states, icons, styled selects and buttons, and colours that show whether an answer is valid.
Learning Objectives
- Style text inputs, textareas and selects
- Show focus clearly
- Add an icon inside an input
- Colour checkboxes and radios with
accent-color - Show valid and invalid states with
:valid,:invalidand:user-invalid
1. Text Inputs
Example
<style>
.field { display: block; width: 100%; box-sizing: border-box; padding: 10px 12px; margin: 6px 0 14px;
border: 1px solid #ced4da; border-radius: 8px; font: 16px system-ui, sans-serif; }
label { font: 600 14px system-ui, sans-serif; color: #495057; }
</style>
<label for="n">Full name</label>
<input id="n" class="field" placeholder="Ayesha Khan">
<label for="m">Message</label>
<textarea id="m" class="field" rows="3" style="resize: vertical;"></textarea>width: 100%+box-sizing: border-boxmakes fields fill the form without overflowing.font: 16px …stops iPhones from zooming in when you tap the field.resize: verticallets a textarea grow only in height.
2. Focus
Example
<style>
input { padding: 10px; border: 2px solid #ced4da; border-radius: 8px; font-size: 16px; transition: border-color 0.2s, box-shadow 0.2s; }
input:focus { border-color: #1971c2; outline: none; box-shadow: 0 0 0 4px rgb(25 113 194 / 0.2); }
</style>
<input placeholder="Click me">Removing outline is only acceptable because the border and glow replace it.
3. An Icon Inside an Input
Example
<style>
.search { padding: 10px 12px 10px 38px; border: 1px solid #ced4da; border-radius: 999px; font-size: 16px;
background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='18' fill='none' stroke='%23868e96' stroke-width='2'%3E%3Ccircle cx='8' cy='8' r='6'/%3E%3Cpath d='m13 13 4 4'/%3E%3C/svg%3E") no-repeat 12px center; }
</style>
<input class="search" type="search" placeholder="Search courses">The icon is a background image; left padding makes room for it.
4. Selects, Checkboxes, Radios and Buttons
Example
<style>
select { padding: 10px 12px; border: 1px solid #ced4da; border-radius: 8px; font-size: 16px; background: white; }
input[type="checkbox"], input[type="radio"] { accent-color: #2f9e44; width: 18px; height: 18px; }
button[type="submit"] { padding: 10px 22px; border: 0; border-radius: 8px; background: #2f9e44; color: white; font-size: 16px; cursor: pointer; }
button[type="submit"]:hover { background: #2b8a3e; }
</style>
<form>
<p><select name="city"><option>Karachi</option><option>Lahore</option><option>Islamabad</option></select></p>
<p><label><input type="checkbox" name="terms" checked> I accept the terms</label></p>
<p><label><input type="radio" name="plan" checked> Monthly</label> <label><input type="radio" name="plan"> Yearly</label></p>
<button type="submit">Create account</button>
</form>accent-color recolours checkboxes, radios, range sliders and progress bars in one line.
5. Valid and Invalid States
Example: type a wrong email, then click outside the box:
<style>
input { padding: 10px; border: 2px solid #ced4da; border-radius: 8px; font-size: 16px; }
input:user-invalid { border-color: #e03131; background: #fff5f5; }
input:valid:not(:placeholder-shown) { border-color: #2f9e44; }
.hint { display: none; color: #e03131; font-size: 13px; }
input:user-invalid + .hint { display: block; }
</style>
<input type="email" placeholder="you@example.com" required>
<span class="hint">Please enter a valid email address.</span>:invalid/:validmatch at once, even before the visitor types.:user-invalidwaits until the visitor has interacted, which is much friendlier.:placeholder-shownmatches an empty field that's showing its placeholder.
Other useful states: :disabled, :read-only, :required, :checked.
Common Beginner Mistakes
1. Fields wider than the form. Add box-sizing: border-box with width: 100%.
2. Font size under 16px on inputs. iPhones zoom in when the field is tapped.
3. Removing focus outlines with nothing in their place. Always show focus clearly.
4. Red errors before the visitor has typed. Use :user-invalid instead of :invalid.
Practice Questions
- Make every text input fill its form without overflowing.
- Give inputs a blue border when focused.
- Which property recolours checkboxes and radio buttons?
- What's the difference between
:invalidand:user-invalid? - How do you put an icon inside a search box?
Practice Answers
input { width: 100%; box-sizing: border-box; }input:focus { border-color: blue; }accent-color:invalidmatches straight away;:user-invalidonly after the visitor has interacted with the field.- Add it as a
backgroundimage and give the input extra left padding.
Key Takeaways
width: 100%+box-sizing: border-box+ 16px text for comfortable fields.- Clear
:focusstyles; never just remove the outline. accent-colorstyles checkboxes and radios.:user-invalidshows errors at the right moment.