Dofactory.com
Dofactory.com

HTML <img> style Attribute

A style attribute on an <img> tag assigns a unique style to the image.

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

Example

#

A style attribute on an <img> element.

Van Gogh self-portrait
<img src="/img/html/vangogh-lg.jpg"
     alt="Van Gogh self-portrait"
     style="border:4px solid #1b6b6f; padding:15px;">



Using style

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


Syntax

<img style="CSS-styles">

Values

#

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

More Examples

A style attribute on an <img> element.
Clicking the button toggles the border color.

Van Gogh self-portrait

<img id="myimg"
     src="/img/html/vangogh-lg.jpg"
     alt="Van Gogh self-portrait"
     style="border:4px solid teal; padding:15px;">

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

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

    if (element.style.borderColor === "teal") {
       element.style.borderColor = "brown";
    } else {
       element.style.borderColor = "teal";
    }
  }
</script>

Code explanation

The style attribute assigns a border to the <img> element.

Clicking the button calls JavaScript that changes the border 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 <img>

Author: Jack Poorte
Published: Jun 20 2021
Last Reviewed: Sep 30 2023


What's your favorite/least favorite part of Dofactory?


Guides