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

A style attribute on a <div> tag assigns a unique style to the element.

Its value is CSS that defines the appearance of the div element.

Example

#

A style attribute on a <div> element.

Paul Gauguin

Eugène Henri Paul Gauguin, was a French Post-Impressionist artist. Unappreciated until after his death, Gauguin is now recognized for his experimental use of color and Synthetist style that were distinct from Impressionism. Toward the end of his life, he spent ten years in French Polynesia. The paintings from this time depict people or landscapes from that region.

<div style="background-color:aliceblue;padding:25px;">
  <h4>Paul Gauguin</h4>
  <p>
    Eugène Henri Paul Gauguin, was a French Post-Impressionist artist. 
    Unappreciated until after his death, Gauguin is now recognized for 
    his experimental use of color and Synthetist style that were 
    distinct from Impressionism. Toward the end of his life, he 
    spent ten years in French Polynesia. The paintings from this 
    time depict people or landscapes from that region.</p>
</div>

Using style

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


Syntax

<div 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 <div> element.
Clicking the button toggles the background color.

Paul Gauguin

Eugène Henri Paul Gauguin, was a French Post-Impressionist artist. Unappreciated until after his death, Gauguin is now recognized for his experimental use of color and Synthetist style that were distinct from Impressionism. Toward the end of his life, he spent ten years in French Polynesia. The paintings from this time depict people or landscapes from that region.


<div id="mydiv" style="background-color:aliceblue;padding:25px;">
  <h4>Paul Gauguin</h4>
  <p>
    Eugène Henri Paul Gauguin, was a French Post-Impressionist artist. 
    Unappreciated until after his death, Gauguin is now recognized for 
    his experimental use of color and Synthetist style that were 
    distinct from Impressionism. Toward the end of his life, he 
    spent ten years in French Polynesia. The paintings from this 
    time depict people or landscapes from that region.</p>
</div>

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

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

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

Code explanation

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

Clicking the button calls JavaScript which changes the element 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 <div>

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