Introduction
The HTML <fieldset>
tag is used to group HTML elements together, and it can help to create organized and well-structured documents. And using the
The HTML <fieldset>
tag is used to group HTML elements together, and it can help to create organized and well-structured documents. And using the
Create a file named index.html
and write the basic structure of the HTML page.
<!doctype html>
<html>
<head>
<title>Creating a Form with Fieldset Tag</title>
</head>
<body></body>
</html>
Add a form element to the HTML body, and then use the <fieldset>
tag to group related form fields. We will also add the label
tag to the form elements to add a description to the input field.
<form>
<fieldset>
<legend>User Details</legend>
<label for="fname">First Name:</label>
<input type="text" id="fname" name="firstname" /><br /><br />
<label for="lname">Last Name:</label>
<input type="text" id="lname" name="lastname" /><br /><br />
<label for="email">Email:</label>
<input type="email" id="email" name="email" /><br /><br />
</fieldset>
<fieldset>
<legend>Account Details</legend>
<label for="uname">Username:</label>
<input type="text" id="uname" name="username" /><br /><br />
<label for="pass">Password:</label>
<input type="password" id="pass" name="password" /><br /><br />
</fieldset>
<input type="submit" value="Submit" />
</form>
The code above will create a form that is structured into two fieldsets. The first fieldset groups the user's personal details, and the second fieldset groups the account details. You might notice that we used the label
tag to provide some context for the fields.
We can style the fieldset using CSS to give it a proper design. We can add a border, background-color, and some padding to make the fieldset look better.
<style>
fieldset {
padding: 10px;
border: 1px solid #c0c0c0;
border-radius: 4px;
margin: 5px;
background-color: #f8f8f8;
}
</style>
Open the HTML file on your browser to view the form and test it out.
Fieldset tag creates an organized and well-structured document. It is useful for creating forms. use label tags to provide context for the input fields. Fieldset tag groups related form fields and adds a border around the related fields. Finally, the CSS styling provides design to the fieldset that makes it look better.