This tutorial covers styling HTML forms with CSS to create attractive, user-friendly, and responsive designs. By styling elements like <input>, <select>, <textarea>, and <button>, you can enhance user experience and align forms with your website’s look. This guide provides real-time previews and clear examples for beginners.
Unstyled forms look basic and may not match your website’s design. Styling improves usability, ensures accessibility, and creates a professional appearance. CSS allows customization of colors, spacing, and responsiveness.
Here’s a simple form with styled <input>, <label>, and <button> elements. Styles are scoped to avoid conflicts.
<form action="/submit" method="post" class="basic-form">
<label for="name1">Name:</label><br>
<input type="text" id="name1" name="name" placeholder="Enter your name"><br>
<label for="email1">Email:</label><br>
<input type="email" id="email1" name="email" placeholder="Enter your email"><br>
<button type="submit">Submit</button>
</form>
<style>
.basic-form {
max-width: 400px !important;
margin: 20px auto !important;
font-family: Arial, sans-serif !important;
padding: 10px !important;
}
.basic-form label {
display: block !important;
margin-bottom: 5px !important;
font-weight: bold !important;
}
.basic-form input[type="text"], .basic-form input[type="email"] {
width: 100% !important;
padding: 8px !important;
margin-bottom: 15px !important;
border: 1px solid #ccc !important;
border-radius: 4px !important;
}
.basic-form button {
background-color: #38bdf8 !important;
color: white !important;
padding: 10px 20px !important;
border: none !important;
border-radius: 4px !important;
cursor: pointer !important;
}
.basic-form button:hover {
background-color: #2196f3 !important;
}
</style>
Preview:
Style <select> and <textarea> to match other form elements, using specific classes for isolation.
<form action="/submit" method="post" class="select-textarea-form">
<label for="department1">Department:</label>
<select id="department1" name="department">
<option value="hr">HR</option>
<option value="it">IT</option>
<option value="sales">Sales</option>
</select><br>
<label for="feedback1">Feedback:</label><br>
<textarea id="feedback1" name="feedback" rows="4" cols="50" placeholder="Enter your feedback"></textarea><br>
<button type="submit">Submit</button>
</form>
<style>
.select-textarea-form {
max-width: 400px !important;
margin: 20px auto !important;
padding: 10px !important;
}
.select-textarea-form select, .select-textarea-form textarea {
width: 100% !important;
padding: 8px !important;
margin-bottom: 15px !important;
border: 1px solid #ccc !important;
border-radius: 4px !important;
font-family: Arial, sans-serif !important;
}
.select-textarea-form select:focus, .select-textarea-form textarea:focus {
outline: none !important;
border-color: #38bdf8 !important;
box-shadow: 0 0 5px rgba(56, 189, 248, 0.5) !important;
}
.select-textarea-form button {
background-color: #38bdf8 !important;
color: white !important;
padding: 10px 20px !important;
border: none !important;
border-radius: 4px !important;
cursor: pointer !important;
}
</style>
Preview:
Responsive forms adapt to different screen sizes using percentage-based widths and media queries.
<style>
.responsive-form {
max-width: 600px !important;
margin: 20px auto !important;
padding: 10px !important;
}
.responsive-form input, .responsive-form select, .responsive-form textarea {
width: 100% !important;
box-sizing: border-box !important;
}
@media (max-width: 600px) {
.responsive-form {
padding: 10px !important;
}
.responsive-form button {
width: 100% !important;
}
}
</style>
Use high-contrast colors, readable fonts, and proper <label> associations for accessibility.
Create a styled form with CSS. Experiment with colors, padding, and responsiveness.
<form action="/submit" method="post" class="try-it-form">
<label for="username1">Username:</label><br>
<input type="text" id="username1" name="username" placeholder="Enter your username"><br>
<label for="message1">Message:</label><br>
<textarea id="message1" name="message" rows="4" cols="50" placeholder="Enter your message"></textarea><br>
<button type="submit">Send</button>
</form>
<style>
.try-it-form {
max-width: 500px !important;
margin: 20px auto !important;
font-family: Arial, sans-serif !important;
padding: 10px !important;
}
.try-it-form label {
font-weight: bold !important;
color: #333 !important;
}
.try-it-form input, .try-it-form textarea {
width: 100% !important;
padding: 10px !important;
margin: 10px 0 !important;
border: 1px solid #ddd !important;
border-radius: 5px !important;
}
.try-it-form button {
background-color: #4caf50 !important;
color: white !important;
padding: 12px 20px !important;
border: none !important;
border-radius: 5px !important;
cursor: pointer !important;
}
.try-it-form button:hover {
background-color: #45a049 !important;
}
@media (max-width: 600px) {
.try-it-form input, .try-it-form textarea, .try-it-form button {
font-size: 16px !important;
}
}
</style>
Preview:
By mastering CSS form styling, you can create attractive, responsive, and accessible forms. Practice these techniques to build professional forms!