If you would like to support CSSPortal, please consider making a small donation.
☕ Buy a Coffee
Web forms are integral parts of websites — they're used for user signups, contact forms, search bars, and more. However, the default styling of form elements can be quite plain. This tutorial will guide you through the process of styling form elements using CSS to create more appealing and interactive forms.
Firstly, let's create a basic HTML form:
<form>
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email"><br>
<input type="submit" value="Submit">
</form>
This form includes two fields: one for the user's name and one for their email. The label element is used to provide a text description for each field, and the input element is used to create the actual fields users can type into. The for attribute in the label element should match the id attribute in the corresponding input element. Lastly, there's a submit button that users can click to send the form data.
Now that we have our form, let's style it using CSS.
First, let's give our form some basic styles:
form {
width: 300px;
margin: 0 auto;
padding: 20px;
background-color: #f4f4f4;
border: 1px solid #e5e5e5;
border-radius: 5px;
}
These styles center the form on the page, give it some padding, and apply a background color and a border. The border-radius property is used to round the corners of the form.
Now let's style the text fields:
input[type="text"], input[type="email"] {
width: 100%;
padding: 10px;
margin: 5px 0 15px 0;
border: 1px solid #ccc;
border-radius: 3px;
}
This CSS rule selects all input elements with a type of "text" or "email". It makes them take up the full width of the form, gives them some padding and margin, and applies a border. The border-radius property is again used to round the corners.
Lastly, we'll style the submit button:
input[type="submit"] {
width: 100%;
padding: 10px;
color: white;
background-color: #007BFF;
border: none;
border-radius: 3px;
cursor: pointer;
}
This rule selects the input element with a type of "submit". It makes the button take up the full width of the form, gives it some padding, and applies a background color. The border property is set to "none" to remove the default border, and the cursor property is set to "pointer" to change the mouse cursor to a hand icon when hovering over the button.
To make our form more interactive, we can use CSS to change the appearance of form elements when users interact with them.
For example, we can change the background color of text fields when they're focused (i.e., when the user is typing in them):
input[type="text"]:focus, input[type="email"]:focus {
background-color: #e6edf7;
}
We can also change the background color of the submit button when it's hovered over:
input[type="submit"]:hover {
background-color: #0056b3;
}
With the techniques described in this tutorial, you can style form elements in your web pages to be more visually appealing and interactive. Remember that these are just basic examples — with CSS, the possibilities are endless. Happy coding!