On this page10 sections
  1. Learning Objectives
  2. 1. Text Inputs
  3. 2. Focus
  4. 3. An Icon Inside an Input
  5. 4. Selects, Checkboxes, Radios and Buttons
  6. 5. Valid and Invalid States
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 024: Styling Forms

Category5: Pseudo-classes & Components
Lecture024 of 057
Estimated video length12 min
TargetCSS3 · any modern browser
LevelBeginner

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, :invalid and :user-invalid

1. Text Inputs

Example

html
<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-box makes fields fill the form without overflowing.
  • font: 16px … stops iPhones from zooming in when you tap the field.
  • resize: vertical lets a textarea grow only in height.

2. Focus

Example

html
<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

html
<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

html
<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:

html
<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 / :valid match at once, even before the visitor types.
  • :user-invalid waits until the visitor has interacted, which is much friendlier.
  • :placeholder-shown matches 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

  1. Make every text input fill its form without overflowing.
  2. Give inputs a blue border when focused.
  3. Which property recolours checkboxes and radio buttons?
  4. What's the difference between :invalid and :user-invalid?
  5. How do you put an icon inside a search box?

Practice Answers

  1. input { width: 100%; box-sizing: border-box; }
  2. input:focus { border-color: blue; }
  3. accent-color
  4. :invalid matches straight away; :user-invalid only after the visitor has interacted with the field.
  5. Add it as a background image and give the input extra left padding.

Key Takeaways

  • width: 100% + box-sizing: border-box + 16px text for comfortable fields.
  • Clear :focus styles; never just remove the outline.
  • accent-color styles checkboxes and radios.
  • :user-invalid shows errors at the right moment.