← Back to CSS Course | Chapter 6: Selectors & Styling | Lesson 13 of 22

CSS Forms

Imagine walking up to a bank teller counter. If there are no pens, clean dividers, or designated spaces, the counter will look cluttered. You want neat clipboards with designated boxes, labels, and highlighted action zones to complete your tasks cleanly. CSS forms are those prioritized clipboards. They style raw, clunky browser inputs into friendly, visually engaging, and accessible fields that make submitting data effortless.

Styling Text Input Boxes

By default, browsers render form inputs with rigid, outdated borders. You can easily customize these inputs using CSS properties to adjust their borders, inner padding, background colors, and typography styles.

Note: Add generous internal padding to your input fields to make entering text comfortable and readable.

Warning: Failing to set explicit widths on your text input fields can cause them to overflow on smaller screen sizes.

Example: Styling Text Input Boxes

css
<style>
input[type="text"] {
  border: 1px solid #ccc;
  padding: 8px;
  font-family: sans-serif;
}
</style>
<input type="text" placeholder="Your name">

Accessible Active Focus Indicators

When users tab through your form or click an input, the element enters an 'active focus' state. You can customize the styling of this state using the :focus pseudo-class to ensure active inputs stand out cleanly.

Note: Use a subtle border-color transition and a soft box-shadow glow to style your focus indicators cleanly.

Warning: Never use CSS outline: none to hide browser focus outlines without replacing them with custom focus styles.

Example: Accessible Active Focus Indicators

css
<style>
input:focus {
  border-color: blue;
  box-shadow: 0 0 4px blue;
}
</style>
<input type="text">

Checkbox and Radio Alignments

By default, browsers render checkboxes and radio buttons slightly misaligned with their text labels. You can use Flexbox alignment properties on their parent wrappers to align them perfectly.

Note: Use vertical-align: middle or display: flex to align selectors and text labels side-by-side cleanly.

Warning: Avoid setting complex custom padding directly on native checkboxes, as browsers ignore it.

Example: Checkbox and Radio Alignments

css
<style>
label {
  display: flex;
  align-items: center;
  gap: 6px;
}
</style>
<label><input type="checkbox"> Subscribe</label>

Customizing select Dropdown Boxes

Native select dropdown menus can look blocky and inconsistent across different devices. You can customize them using CSS properties (like appearance: none) to remove browser default designs, adding your own custom borders, padding, and arrow icons.

Note: Use background-image properties to add a clean, custom disclosure arrow icon to your dropdowns.

Warning: Make sure your custom dropdown arrows are positioned safely so they do not overlap with your text.

Example: Customizing select Dropdown Boxes

css
<style>
select {
  appearance: none;
  border: 1px solid #ccc;
  padding: 8px;
}
</style>
<select><option>Choice A</option></select>

Styling Form Action Submit Buttons

The form submit button is the final call-to-action on your page. By styling it with high-contrast colors, custom borders, and smooth hover animations, you can make submitting data intuitive and visually engaging.

Note: Use the active pseudo-class to make buttons look pressed or clicked.

Warning: Ensure your submit buttons have a defined mouse cursor pointer style on hover.

Example: Styling Form Action Submit Buttons

css
<style>
button[type="submit"] {
  background: royalblue;
  color: white;
  border: none;
  padding: 10px 20px;
}
button[type="submit"]:active {
  background: darkblue;
}
</style>
<button type="submit">Submit</button>
Common Mistakes
  1. Forgetting to set box-sizing: border-box on form inputs, causing full-width inputs to overflow.
  2. Hiding browser focus outlines completely using CSS (outline: none) without replacing them with custom focus styles.
  3. Using non-semantic divs to design buttons, breaking keyboard focus and tab navigation.
Chapter Summary
  • Style text inputs with custom padding, borders, and rounded corners, and always use box-sizing: border-box.
  • Highlight active input fields using the :focus pseudo-class to improve user experience.
  • Align checkboxes and labels perfectly using Flexbox, and style submit buttons with high-contrast hover transitions.
Browser Support

Standard form styling properties and basic focus indicators are natively supported by all modern web browsers.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.