Dofactory.com
Dofactory.com
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 freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

HTML <section> class Attribute

The class attribute assigns one or more classnames to the <section> 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 <section> element.

The Night Watch

The Night Watch (Dutch: De Nachtwacht), is a 1642 painting by Rembrandt van Rijn. It is in the collection of the Amsterdam Museum but is prominently displayed in the Rijksmuseum (State Museum) as the best-known painting in its collection. The Night Watch is the most famous Dutch Golden Age painting.

<style>
  .section-class {
    background: moccasin;
    padding: 20px;
    width: 400px;
    line-height: 1.7em;
  }
</style>

<section class="section-class">
  <h2>The Night Watch</h2>
  <p>The Night Watch (Dutch: De Nachtwacht), is a 1642 
     painting by Rembrandt van Rijn. It is in the collection 
     of the Amsterdam Museum but is prominently displayed 
     in the Rijksmuseum (State Museum) as the best-known 
     painting in its collection. The Night Watch is 
     the most famous Dutch Golden Age painting.
  </p>
</section>

Using class

Classes (i.e. classnames) are used for styling the section 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

<section class="classnames">

Values

#

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

More Examples

A class attribute styling a <section> element.
Clicking the button toggles a classname that adds a rounded border to the section.

The Night Watch

The Night Watch (Dutch: De Nachtwacht), is a 1642 painting by Rembrandt van Rijn. It is in the collection of the Amsterdam Museum but is prominently displayed in the Rijksmuseum (State Museum) as the best-known painting in its collection. The Night Watch is the most famous Dutch Golden Age painting.


<style>
  .section-bg {
    background: moccasin;
    padding: 20px;
    width: 400px;
    line-height: 1.7em;
  }
  .section-border { border-radius: 50px; }
</style>

<section id="mysection" class="section-bg">
  <h2>The Night Watch</h2>
  <p>The Night Watch (Dutch: De Nachtwacht), is a 1642 
     painting by Rembrandt van Rijn. It is in the collection 
     of the Amsterdam Museum but is prominently displayed 
     in the Rijksmuseum (State Museum) as the best-known 
     painting in its collection. The Night Watch is 
     the most famous Dutch Golden Age painting.
  </p>
</section>

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

<script>
  let toggle = () => {
     let element = document.getElementById("mysection");
     element.classList.toggle("section-border");
  }
</script>

Code explanation

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

The class attribute in <section> assigns one classname.

Repeatedly clicking the button toggles another class, toggling the rounded border of the section.


Browser support

Here is when class support started for each browser:

Chrome
6.0 Sep 2010
Firefox
4.0 Mar 2011
IE/Edge
9.0 Mar 2011
Opera
11.1 Mar 2011
Safari
5.0 Jun 2010

You may also like

 Back to <section>

Last updated on Sep 30, 2023

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 freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

Guides