Form Styling
Input styling, validation states, custom controls, and form layouts.
Overview
Well-styled forms improve user experience.
Key Concepts
- Input Styling — Consistent input appearance
- Validation States — Visual feedback
- Custom Controls — Checkboxes, radios, selects
- Form Layout — Responsive form structures
- Focus States — Accessibility styling
Code Examples
/* Base form styles */
.form-group {
margin-bottom: 1.5rem;
}
.form-label {
display: block;
margin-bottom: 0.5rem;
font-weight: 500;
}
.form-input {
width: 100%;
padding: 0.75rem 1rem;
border: 2px solid #ddd;
border-radius: 8px;
font-size: 1rem;
transition: border-color 0.2s, box-shadow 0.2s;
}
.form-input:focus {
outline: none;
border-color: var(--primary);
box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.25);
}
/* Validation states */
.form-input:valid {
border-color: var(--success);
}
.form-input:invalid {
border-color: var(--danger);
}
.form-input.error {
border-color: var(--danger);
}
.form-error {
color: var(--danger);
font-size: 0.875rem;
margin-top: 0.5rem;
}
/* Custom checkbox */
.checkbox-custom {
display: flex;
align-items: center;
gap: 0.5rem;
cursor: pointer;
}
.checkbox-custom input {
display: none;
}
.checkbox-custom span {
width: 20px;
height: 20px;
border: 2px solid #ddd;
border-radius: 4px;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.2s;
}
.checkbox-custom input:checked + span {
background: var(--primary);
border-color: var(--primary);
}
.checkbox-custom input:checked + span::after {
content: '✓';
color: white;
font-size: 12px;
}
/* Custom select */
.select-custom {
appearance: none;
width: 100%;
padding: 0.75rem 2.5rem 0.75rem 1rem;
border: 2px solid #ddd;
border-radius: 8px;
background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23666' d='M6 8L1 3h10z'/%3E%3C/svg%3E") no-repeat right 1rem center;
cursor: pointer;
}
/* Form layout */
.form-row {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
}
/* Input group */
.input-group {
display: flex;
}
.input-group .form-input {
border-radius: 0;
}
.input-group .form-input:first-child {
border-radius: 8px 0 0 8px;
}
.input-group .form-input:last-child {
border-radius: 0 8px 8px 0;
}
.input-group-text {
padding: 0.75rem 1rem;
background: #f8f9fa;
border: 2px solid #ddd;
}
/* Disabled state */
.form-input:disabled {
background: #f5f5f5;
cursor: not-allowed;
opacity: 0.7;
}
/* Textarea */
.form-textarea {
min-height: 150px;
resize: vertical;
}
Practice
Build a styled registration form with validation feedback.