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 <body> style Attribute

A style attribute on a <body> tag assigns a unique style to the page body.

Its value is CSS that defines the appearance of the body.

Example

#

A style attribute on a <body> tag.

<body style="background-color:oldlace; padding:25px;">
  <h2>The Night Watch</h2>
  <p>
    Militia Company of District II under the Command of 
    Captain Frans Banninck Cocq, also known as The Shooting 
    Company of Frans Banning Cocq and Willem van Ruytenburch, 
    but commonly referred to as 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 as the 
    best-known painting in its collection. The Night Watch 
    is one of the most famous Dutch Golden Age paintings.
  </p>
  <p>
    The painting is famous for three things: its colossal 
    size, the dramatic use of light and shadow (tenebrism),
    and the perception of motion in what would have 
    traditionally been a static military group portrait. 
    The painting was completed in 1642, at the peak of the 
    Dutch Golden Age.
  </p>
  <br />
  <img src="/img/html/nightwatch.jpg">
</body>

Using style

The style attribute specifies the style, i.e. look and feel, of the <body> 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 <body> element only.


Syntax

<body 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 <body> tag.
Clicking the button toggles the background color.

The Night Watch

Militia Company of District II under the Command of Captain Frans Banninck Cocq, also known as The Shooting Company of Frans Banning Cocq and Willem van Ruytenburch, but commonly referred to as 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 as the best-known painting in its collection. The Night Watch is one of the most famous Dutch Golden Age paintings.

The painting is famous for three things: its colossal size, the dramatic use of light and shadow (tenebrism), and the perception of motion in what would have traditionally been a static military group portrait. The painting was completed in 1642, at the peak of the Dutch Golden Age.




<body id="mybody" style="background-color:oldlace; padding:25px;">
  <h2>The Night Watch</h2>
  <p>
    Militia Company of District II under the Command of 
    Captain Frans Banninck Cocq, also known as The Shooting 
    Company of Frans Banning Cocq and Willem van Ruytenburch, 
    but commonly referred to as 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 as the 
    best-known painting in its collection. The Night Watch 
    is one of the most famous Dutch Golden Age paintings.
  </p>
  <p>
    The painting is famous for three things: its colossal 
    size, the dramatic use of light and shadow (tenebrism),
    and the perception of motion in what would have 
    traditionally been a static military group portrait. 
    The painting was completed in 1642, at the peak of the 
    Dutch Golden Age.
  </p>
  <br />
  <img src="/img/html/nightwatch.jpg">

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

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

      if (element.style.backgroundColor === "oldlace") {
         element.style.backgroundColor = "aliceblue";
      } else {
         element.style.backgroundColor = "oldlace";
      }
    }
  </script>

</body>

Code explanation

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

Clicking the button calls JavaScript which toggles the background color.


Browser support

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

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