Dofactory.com
Dofactory.com

HTML <picture> class Attribute

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

Classnames are defined in a stylesheet or in a local <style> element.

Classes, i.e. classnames, are used to style elements.

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

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

Multiple classnames are separated by a space.

JavaScript uses classes to access 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>

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


Quick question: what's your favorite/least favorite part of Dofactory?


Guides