Creating and styling HTML forms
![Creating and styling HTML forms](https://i0.wp.com/shallout.com/wp-content/uploads/2022/12/html-1.jpg?fit=845%2C527&ssl=1)
HTML forms are an essential element in web development. It allows users to interact with a website by entering and submitting data. In this tutorial, we will go over the process of creating and styling HTML forms step-by-step.
Creating an HTML Form:
Creating an HTML form is easdy, all you need do is to use the <form>
element. This element acts as a container for different form controls, such as text fields, radio buttons, etc. Within the form element, you can add different form controls using their corresponding HTML elements. For example, to create a text field, use the <input>
element with the type
attribute set to “text”. The <label>
element can be used to provide a description for the form control, and the for
attribute of the label should match the id
attribute of the form control.
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name">
</form>
In the above example, the name
attribute specifies a name for the form control, and the id
attribute provides a unique identifier for the form control. The name
attribute is used to identify the form control when it is submitted, while the id
attribute is used to style the form control using CSS.
Adding Form Controls:
Using the appropriate HTML elements, you can add various form controls to your form. Here is an example of a form with multiple form controls.
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<label for="gender">Gender:</label>
<select id="gender" name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
<label for="terms">Accept Terms:</label>
<input type="checkbox" id="terms" name="terms">
</form>
In this example, we have used different form controls such as text fields, email fields, password fields, select menus and checkboxes. The <input>
element with the type
attribute set to “email” and “password” are used to create email and password fields respectively. The <select>
element is used to create a drop-down list, and the <option>
element is used to create options within the drop-down list. The <input>
element with the type
attribute set to “checkbox” is used to create a checkbox.
READ ALSO: USING CSS SELECTORS TO TARGET SPECIFIC ELEMENTS
Styling the Form:
You can style your HTML form using CSS. Below are few eamples of how to style various parts of the form.
- To change the font size and color of the labels:
label {
font-size: 16px;
color: black;
}
- To change the background color of the input fields:
input {
background-color: #ccc;
}
- To change the border of the input fields
input {
border: 1px solid #ccc;
}
- To change the color of the text inside the input fields:
input {
color: #333;
}
- To change the width of the form controls:
input, select {
width: 100%;
}
- To add padding to the form controls:
input, select {
padding: 12px 20px;
}
- To change the hover effect of the form controls:
input:hover, select:hover {
background-color: #f2f2f2;
}
- To center align the form:
form {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
- To add a background color to the form:
form {
background-color: #f9f9f9;
padding: 20px;
}
Note that you can customize your form as you wish. The above examples are just a starting point for a better understand of how to style HTML forms. Shallout!!!