CSS Forms
In this page:
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
<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
<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
<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
<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
<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>
- Forgetting to set box-sizing: border-box on form inputs, causing full-width inputs to overflow.
- Hiding browser focus outlines completely using CSS (outline: none) without replacing them with custom focus styles.
- Using non-semantic divs to design buttons, breaking keyboard focus and tab navigation.
- 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.
Standard form styling properties and basic focus indicators are natively supported by all modern web browsers.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline