Dofactory.com
Dofactory.com

HTML <video> class Attribute

The class attribute assigns one or more classnames to the <video> 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 <video> element.

<style>
  .video {width:300px; height:240px; outline:none;
          border: 5px solid #45c9d0; padding: 10px;}
</style>

<video class="video" controls>
  <source src="/media/movie.mp4" type="video/mp4">
  <source src="/media/movie.ogg" type="video/ogg">
</video>

Using class

Classes (i.e. classnames) are used for styling the video 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

<video class="classnames">

Values

#

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

More Examples

A class attribute styling a <video> element.
Clicking the button toggles a classname that changes the video size.



<style>
  .video-sm { width: 320px; height: 240px; outline: none;}
  .video-lg { width: 480px; height: 360px; outline: none;}
</style>

<video id="myvideo" class="video-sm" controls>
  <source src="/media/movie.mp4" type="video/mp4">
  <source src="/media/movie.ogg" type="video/ogg">
</video>

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

<script>
  let toggle = () => {
     let element = document.getElementById("myvideo");
     element.classList.toggle("video-lg");
  }
</script>

Code explanation

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

The class attribute in <video> assigns one classname.

Repeatedly clicking the button toggles another class, changing the <video> control height and width.


Browser support

Here is when class support started for each browser:

Chrome
4.0 Jan 2010
Firefox
3.5 Jun 2009
IE/Edge
9.0 Sep 2012
Opera
10.5 Mar 2010
Safari
4.0 Jun 2009

You may also like

 Back to <video>

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


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


Guides