agyanadda@gmail.com

CSS Forms

Styling Content 1 min read Updated Dec 1, 2026 Topic 29 of 39

Good form styles make forms easier to fill in.

Tips

  • Make inputs full width: width: 100%; box-sizing: border-box;
  • Add padding and a soft border with border-radius
  • Show focus clearly with :focus or :focus-visible
  • Target input types with attribute selectors: input[type="email"]
  • Use :invalid to highlight wrong values
  • Inputs do not inherit fonts by default — add font: inherit;

Example

Example — Styled contact form HTML
<style>
  form { max-width: 340px; font-family: Arial, sans-serif; }
  label { display: block; margin: 10px 0 4px; font-weight: bold; }
  input, textarea { width: 100%; box-sizing: border-box; padding: 10px; border: 1px solid #bbb; border-radius: 6px; font: inherit; }
  input:focus, textarea:focus { outline: none; border-color: #4a90e2; box-shadow: 0 0 0 3px rgba(74,144,226,.3); }
  input:invalid:not(:placeholder-shown) { border-color: crimson; }
  button { margin-top: 12px; width: 100%; padding: 10px; background: #1f386b; color: white; border: none; border-radius: 6px; cursor: pointer; }
  button:hover { background: #2d4f94; }
</style>

<form>
  <label for="n">Name</label>
  <input id="n" placeholder="Your name">
  <label for="e">Email</label>
  <input id="e" type="email" placeholder="you@example.com">
  <label for="m">Message</label>
  <textarea id="m" rows="3"></textarea>
  <button type="button">Send</button>
</form>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which selector targets only email inputs?

Exercise 2 Fill in the Blank

The :___ pseudo-class styles an input while the user is typing in it.

Exercise 3 True / False

Form inputs always inherit the page font automatically.

Frequently Asked Questions

Use cursor: pointer; to show the hand icon.