Fieldset Tag in HTML

WBOY
Release: 2024-09-04 16:28:40
Original
814 people have browsed it

A fieldset tag in HTML is something that helps us get certain elements, some related elements together inside one place, normally a Form. The

tag groups our related elements based on logic. The fieldset tag, when used, creates a block or a boundary around our related elements said above. This tag helps break down form sections into several logical ones.

In other words, the

tag helps to define a layout, thus organizing and sectioning a form into logical fields without the usage of tables or other means like divisions. The fieldset tag has a tag that is used to define a text, like a caption for the fragmented section.

What is the Syntax for using
Element?

It’s actually simple to use to

tag or element. As with all the other HTML tags, the
tag too has an opening tag and a close tag. The opening tag is written as
and the closing tag has the usual front slash before the tag name, such as
. Check out the syntax for the
tag.

Syntax:

<form>
<fieldset>
</fieldset>
</form>
Copy after login

Observe a simple code excerpt providing an example of

and below:

<form>
<fieldset style="background:#e1eff2;">
<legend>Personal Information:</legend>
<div><label>Name:</label><input type="text"></div>
<div><label>Email:</label><input type="text"></div>
<div><label>DOB:</label><input type="text"></div>
<div><label>Place:</label><input type="text"></div>
</fieldset>
</form>
Copy after login

Output:

Fieldset Tag in HTML

As discussed above, the

element used in the above code is providing a grouping in the HTML form. Note the caption at the top of the fieldset, created using element.

Another good usage of

and element is along with input type ‘checkboxes’ or ‘radio’, especially when it becomes difficult to provide a collective context for different options and is associated with each one of them.

Attributes of Fieldset Tag in HTML

The

element supports many global attributes that support the additional attributes mentioned below.

1. disabled

The ‘disabled’ attribute uses ‘disabled’ as its value. When this attribute is used, it indicates that the group of elements for which this attribute is used is to be disabled.

Below is an example code excerpt using the fieldset element with one of its attributes, ‘disabled’.

Code:

<fieldset style="background:#e1eff2;" disabled="disabled">
<legend>Personal Information:</legend>
<div><label>Name:</label><input type="text"></div>
<div><label>Email:</label><input type="text"></div>
<div><label>DOB:</label><input type="text"></div>
<div><label>Place:</label><input type="text"></div>
</fieldset>
Copy after login

Output:

Fieldset Tag in HTML

Observe that since we set the attribute’s value for ‘disabled’ as ‘disabled’, all the child elements are disabled that is, not editable.

2. form

The ‘form’ attribute is used to include one or more identifiers of different forms. It defines these identifiers to which our group of related elements will belong or belongs to. The ‘form’ attribute uses form IDs to define the forms. If there is more than one form identifier, they are all included and separated by spaces.

In other words, this is an attribute that takes its value from the ‘id’ attribute of form/forms elements to make the fieldset their parts, even when the fieldset is defined outside the form.

The form attribute is only supported in Opera 12 and earlier versions.

3. name

The ‘name’ attribute defines a name for the grouped items or joined a group of elements. This attribute uses ‘text’ as its value. This ‘name’ does not get displayed in the browser; its usage is limited to the work of scripts.

Below is an example of a fieldset with a ‘name’ attribute.

Code:

<form>
<fieldset style="background:#e1eff2;" name="PersonalInformation" >
<legend>Personal Information:</legend>
<div><label>Name:</label><input type="text"></div>
<div><label>Email:</label><input type="text"></div>
<div><label>DOB:</label><input type="text"></div>
<div><label>Place:</label><input type="text"></div>
</fieldset>
<br />
<button type = "button" onclick="form.PersonalInformation.style.backgroundColor='LIGHTSALMON'"> Change background color of fieldset</button>
</form>
Copy after login

Output:

Fieldset Tag in HTML

Observe, while we gave our fieldset an attribute, ‘name’ and value as ‘Personal Information; It does not get displayed in the browser. But we used the name attribute of the

element at the background for script work.

When we click the button saying “Change background color of fieldset”, the form responds as follows:

Fieldset Tag in HTML

HTML Fieldset Tag in CSS Styling

Like any other HTML element,

element can also be altered for visual effects. The HTML tags or elements can contain one or more properties. These added attributes or properties provide the browsers with more data or information about the elements’ effects. The attributes generally consist of a property name and its assigned value, for example;

<strong>style="color:black";</strong>
Copy after login

The properties that can be used to alter the look and feel of the

include font styles, font families, size, weight, among others.

Below is another example using the above-mentioned properties. CSS can do wonders. Observe the before and after-effects.

Without CSS

Code:

<form>
<fieldset style="background:#e1eff2"; name="PersonalInformation" >
<legend>Personal Information:</legend>
<div><label>Name:</label><input type="text"></div>
<div><label>Email:</label><input type="text"></div>
<div><label>DOB:</label><input type="text"></div>
<div><label>Place:</label><input type="text"></div>
</fieldset>
<br />
<button type = "button" onclick="form.PersonalInformation.style.backgroundColor='LIGHTSALMON'"> Change background color of fieldset</button>
</form>
Copy after login

Output:

Fieldset Tag in HTML

With CSS

Code:


<form>
<fieldset style="background:#e1eff2"; name="PersonalInformation" >
<legend>Personal Information:</legend>
<div><label>Name:</label><input type="text"></div>
<div><label>Email:</label><input type="text"></div>
<div><label>DOB:</label><input type="text"></div>
<div><label>Place:</label><input type="text"></div>
</fieldset>
<br />
<button type = "button" onclick="form.PersonalInformation.style.backgroundColor='LIGHTSALMON'"> Change background color of fieldset</button>
</form>
Copy after login

Output:

Fieldset Tag in HTML

Conclusion

The and tags are the most underused tags while creating web forms. The tag allows every radio or checkbox or text box grouped together to be labeled as a whole while also being separately labeled. The

and elements are helpful when assisting technology like JAWS is being used. It’s known that many screen readers read legend text first and then the label texts.

The above is the detailed content of Fieldset Tag in HTML. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!