Styling forms with CSS helps improve user experience, accessibility, and the overall look of your website. HTML forms contain elements like inputs, textareas, selects, buttons, and labels. With CSS, you can customize their appearance to match your site’s design.
You can style text fields using CSS to add padding, borders, colors, and transitions.
input[type="text"],
input[type="email"],
input[type="password"] {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 6px;
font-size: 16px;
transition: border-color 0.3s ease;
}
input:focus {
border-color: #0066ff;
outline: none;
}
textarea {
width: 100%;
min-height: 120px;
padding: 10px;
border: 1px solid #bbb;
border-radius: 6px;
resize: vertical;
}
select {
width: 100%;
padding: 10px;
border-radius: 6px;
border: 1px solid #ccc;
background-color: #fff;
cursor: pointer;
}
Modern web design often hides the default input and uses a custom style.
input[type="checkbox"],
input[type="radio"] {
accent-color: #007bff;
}
Buttons should be clear, bold, and noticeable.
button,
input[type="submit"] {
background: #007bff;
color: #fff;
padding: 12px 18px;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 16px;
transition: background 0.3s ease;
}
button:hover {
background: #0056cc;
}
Use CSS Grid or Flexbox to organize form elements neatly.
Flexbox Example:
.form-group {
display: flex;
flex-direction: column;
margin-bottom: 15px;
}
::placeholder {
color: #999;
font-style: italic;
}
input.error {
border-color: #e63946;
background-color: #ffe5e5;
}
input.success {
border-color: #2a9d8f;
background-color: #e8fff7;
}
<form class="styled-form">
<div class="form-group">
<label>Name</label>
<input type="text" placeholder="Enter your name" />
</div>
<div class="form-group">
<label>Email</label>
<input type="email" placeholder="Enter your email" />
</div>
<div class="form-group">
<label>Message</label>
<textarea placeholder="Write your message..."></textarea>
</div>
<button type="submit">Submit</button>
</form>