HTML <section> style Attribute

The style attribute on a <section> tag assigns a unique style to the section. A style contains one or more CSS property/value pairs that define the appearance of the section element.

Example

#

A style attribute on 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.

<section style="background: papayawhip;
                padding: 20px;
                width: 500px;
                line-height: 1.7em;">
  <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 style

The style attribute specifies the style, i.e. look and feel, of the <section> element.

A style contains any number of CSS property/value pairs, separated by semicolons (;).

The style attribute overrides any other style that was defined in a <style> tag or an external CSS file.

This inline styling affects the current <section> element only.


Syntax

<section style="CSS-styles">

Values

#

Value Description
CSS-styles One or more CSS property/value pairs separated by semicolons (;).

More Examples

A style attribute on a <section> element.
Clicking the button toggles the background and text color to dark mode.

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.


<section id="mysection"
         style="background: papayawhip;
                padding: 20px;
                width: 500px;
                line-height: 1.7em;">
  <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 style</button>

<script>
  let toggle = () => {
     let element = document.getElementById("mysection");

     if (element.style.backgroundColor === "papayawhip") {
       element.style.backgroundColor = "#222";
       element.style.color = "white";
    } else {
       element.style.backgroundColor = "papayawhip";
       element.style.color = "black";
    }
  }
</script>

Code explanation

The style attribute assigns a background color to the <section> element.

Clicking the button calls JavaScript which toggles the background and text colors to a different value.


Browser support

Here is when style 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>
Guides