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.
A class attribute styling a <fieldset> element.
<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>
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.
<fieldset class="classnames">
Value | Description |
---|---|
classnames | One or more space-separated class names. |
A class attribute styles a <fieldset> element.
Clicking the button toggles a classname that changes the background color.
<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>
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.
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 |
Back to <fieldset>