HTML <picture> class Attribute

The class attribute on a <picture> tag assigns one or more classnames to the picture element. Classes are used to style elements. Classnames are defined in a stylesheet or in a local <style> element.

Example

#

A class attribute styling a <picture> element.

Vincent Van Gogh
<style>
  .picture { border: 3px solid teal;padding: 10px; display:inline-block; }
</style>

<picture class="picture">
  <source media="(max-width: 520px)" srcset="/img/html/vangogh-sm.jpg">
  <source media="(max-width: 768px)" srcset="/img/html/vangogh.jpg">
  <img src="/img/html/vangogh-lg.jpg" alt="Vincent Van Gogh">
</picture>

Using class

The class attribute assigns one or more classnames to the <picture> tag.

Multiple classnames are separated by a space.

Classes (i.e. classnames) are used for styling the picture element.

JavaScript uses classes to access one or more elements by classname.

Tip:  class is a global attribute that can be applied to any HTML element.


Syntax

<picture class="classnames">

Values

#

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

More Examples

A class attribute styling a <picture> element.
Clicking the button toggles a classname that adds a box shadow.

Vincent Van Gogh

<style>
  .picture-bordered {border:3px solid teal;padding:10px;display:inline-block;}
  .picture-shadowed {box-shadow: 0 0 15px 2px rgba(0,0,0,0.5);}
</style>

<picture id="mypicture" class="picture-bordered">
  <source media="(max-width: 520px)" srcset="/img/html/vangogh-sm.jpg">
  <source media="(max-width: 768px)" srcset="/img/html/vangogh.jpg">
  <img src="/img/html/vangogh-lg.jpg" alt="Vincent Van Gogh">
</picture>

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

<script>
  let toggle = () => {
    let element = document.getElementById("mypicture");
    element.classList.toggle("picture-shadowed");
  }
</script>

Code explanation

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

The class attribute in <picture> assigns one classname.

Repeatedly clicking the button toggles the second class, adding box shadow to the <picture>.


Browser support

Here is when class support started for each browser:

Chrome
38.0 Oct 2014
Firefox
38.0 May 2015
IE/Edge
13.0 Nov 2015
Opera
25.0 Oct 2014
Safari
9.1 Mar 2016

You may also like

 Back to <picture>
Guides