Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest self-service freelancing marketplace for people like you.

HTML <fieldset> class Attribute

The class attribute assigns one or more classnames to the <fieldset> tag.

Classnames are defined in a stylesheet or in a local <style> element.

Classes, i.e. classnames, are used to style elements.

Example

#

A class attribute styling a <fieldset> element.

Customer Information





<style>
  .fields {padding:30px; border:3px solid #808cf8;background: #fafcff;}
</style>

<form action="/tutorial/action.html">
  <fieldset class="fields">
    <legend>Customer Information</legend>
  
    <input type="text" placeholder="First name" name="firstname"><br /><br />
    <input type="text" placeholder="Last name" name="lastname"><br /><br />
    <input type="text" placeholder="City" name="city"><br /><br />
    <div>
      <input type="checkbox" id="insurance" name="hasinsurance" value="hasinsurance">
      <label for="insurance">Has Insurance</label>
    </div>
    
    <input type="submit" value="Submit" />
  </fieldset>
</form>

Using class

Classes (i.e. classnames) are used for styling the fieldset element.

Multiple classnames are separated by a space.

JavaScript uses classes to access elements by classname.

Tip:  class is a global attribute that can be applied to any HTML element.


Syntax

<fieldset class="classnames">

Values

#

Value Description
classnames One or more space-separated class names.

More Examples

A class attribute styles a <fieldset> element.
Clicking the button toggles a classname that changes the background color.

Customer Information




<style>
  .fieldset {padding:30px; border:3px solid #808cf8; background: #f6f8ff;}
  .fieldset-bg {background-color: #fff;}
</style>

<form action="/tutorial/action.html">
  <fieldset id="myfieldset" class="fieldset">
    <legend>Customer Information</legend>
    
    <input type="text" placeholder="First Name" name="firstname"><br /><br />
    <input type="text" placeholder="Last Name" name="lastname"><br /><br />
    <div>
      <input type="checkbox" id="insurance" name="hasinsurance" value="hasinsurance">
      <label for="insurance">Has Insurance</label>
    </div>

    <input type="submit" value="Submit" />
  </fieldset>
</form>

<br/>
<button onclick="toggle();">Toggle class</button>

<script>
  let toggle = () => {
    let element = document.getElementById("myfieldset");
    element.classList.toggle("fieldset-bg");
  }
</script>

Code explanation

Two CSS classes are defined in the <style> element.

The class attribute in <fieldset> element assigns one classname.

Repeatedly clicking the toggles another class, changing the background color of the element.


Browser support

Here is when class support started for each browser:

Chrome
1.0 Sep 2008
Firefox
1.0 Sep 2002
IE/Edge
1.0 Aug 1995
Opera
1.0 Jan 2006
Safari
1.0 Jan 2003

You may also like

 Back to <fieldset>
Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest self-service freelancing marketplace for people like you.

Guides