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.
A class attribute styling a <picture> element.
<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>
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.
<picture class="classnames">
Value | Description |
---|---|
classnames | One or more space-separated class names. |
A class attribute styling a <picture> element.
Clicking the button toggles a classname that adds a box shadow.
<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>
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>.
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 |
Back to <picture>