HTML <fieldset> class Attribute

The class attribute on a <fieldset> tag assigns one or more classnames to the fieldset element. Classes are used to style elements. Classnames are defined in a stylesheet or in a local <style> element.

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

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

Multiple classnames are separated by a space.

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

JavaScript uses classes to access one or more 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>
Guides