SmartCodingTips

Input & Button Styling in CSS

Inputs and buttons are essential UI elements in any form or interface. With CSS, you can transform their appearance to match your design while enhancing accessibility and interactivity.

1. Styling Input Fields

Basic input styling includes padding, borders, and focus states:


input[type="text"],
input[type="email"],
input[type="password"] {
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 6px;
    width: 100%;
    font-size: 1rem;
}

input:focus {
    outline: none;
    border-color: #2563eb;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.3);
}
            

2. Styling Buttons

Customize colors, padding, border-radius, and hover/focus effects:


button {
    background-color: #2563eb;
    color: white;
    padding: 10px 20px;
    border: none;
    border-radius: 6px;
    font-size: 1rem;
    cursor: pointer;
    transition: background-color 0.2s ease;
}

button:hover {
    background-color: #1d4ed8;
}

button:focus {
    outline: none;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.4);
}
            

3. Disabled States

Style disabled inputs and buttons clearly to indicate interactivity:


input:disabled,
button:disabled {
    background-color: #f3f4f6;
    color: #9ca3af;
    cursor: not-allowed;
    border-color: #d1d5db;
}
            

4. Input with Icons (Optional)

You can style inputs with icons using relative/absolute positioning:


.input-wrapper {
    position: relative;
}

.input-wrapper input {
    padding-left: 2.5rem;
}

.input-wrapper .icon {
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: #9ca3af;
}
            

5. Best Practices

  • Ensure sufficient contrast for accessibility.
  • Use focus styles for keyboard navigation support.
  • Minimize reliance on JavaScript for basic styling.
  • Test on mobile devices for usability.

Conclusion

Well-styled inputs and buttons improve the user experience significantly. With just a few lines of CSS, you can create clean, modern, and accessible form components for any interface.