DEV Community

Cover image for Adding video and audio to your website using HTML5 tags
Gracechinelo
Gracechinelo

Posted on • Edited on

Adding video and audio to your website using HTML5 tags

Introduction

Multimedia elements, like video and audio, improve user experience and convey info dynamically on websites. HTML5's <video> and <audio> tags make it easy to add multimedia without plug-ins, widely supported by modern browsers. This blog post teaches intermediate web developers and designers about adding multimedia to websites using HTML5 tags, covering basics to advanced topics like optimization, accessibility, and compatibility. After reading, you will be able to add multimedia to your website.

HTML5 Video Tag

The HTML5 <video> tag is a powerful tool for adding video content to a website. It allows developers to embed video files directly into their HTML code, making it easy to display videos on any web page. You can control the size and layout of the video, add custom controls, and even provide multiple video formats for cross-browser compatibility.
In this section, let’s take a closer look at the <video> tag and its attributes and learn how to use it to embed video files on a website; explore how to customize the video player controls using CSS and provide examples of how this tag can be used in a website.

The video tag and its attributes

The HTML5 <video> tag embeds video files on a website. It supports several attributes that can be used to control the behavior and appearance of the video, such as:
src: This attribute is used to specify the source of the video file. The value should be the URL of the video file.
controls: This attribute adds default controls to the video player, such as the play/pause button, volume control, and progress bar.
width and height: These attributes are used to specify the width and height of the video player. The values are in pixels.
poster: This attribute specifies an image that will be displayed before the video starts playing. The value should be the URL of the image file.
autoplay: This attribute causes the video to start playing automatically as soon as it is loaded.
loop: With this attribute, the video loops automatically once it reaches the end.
muted: Causes the video to start playing with the sound muted.
preload: Used to specify how the video should be loaded. The value can be none, meta or auto.
playsinline: This attribute specifies that the video should be played in line with the page.

It's also important to note that you can include multiple <source> tags inside the <video> tag to provide different video formats for cross-browser compatibility. For example, you might include an MP4 and WEBM version of the same video.

Embed a video file on a website using the video tag

Embedding a video file on a website using the HTML5 <video> tag is relatively straightforward. Here is an example of how to do it:

<video width="320" height="240" controls>
    <source src="path/to/video.mp4" type="video/mp4">
    Your browser does not support the video tag.
</video>
Enter fullscreen mode Exit fullscreen mode

In the above example, the <video> tag creates a video player with a width of 320 pixels and a height of 240 pixels. The controls attribute is added to display default controls for the video player.
Inside the <video> tag is a <source> tag. This tag specifies the source of the video file format, in this case, mp4.
The src attribute specifies the path to the video file, and the type attribute specifies the type of the video file. This way, the browser can choose the right format to play the video.
The text between the <video> tag is displayed if the browser does not support the <video> tag; this is an optional fallback text.
It's important to note that the path to the video file must be a valid URL. If the video file is hosted on your server, you can use a relative URL such as path/to/video.mp4. If the video file is hosted on a third-party server, you must use an absolute URL such as https://www.example.com/path/to/video.mp4.

Add multiple video formats for cross-browser compatibility

To add multiple video formats for cross-browser compatibility, use the <source> tag inside the <video> tag. The <source> tag allows you to specify different video files in different formats, and the browser automatically chooses the best format to play the video.
Here is an example of how to use this tag to provide multiple video formats:

<video width="320" height="240" controls>
    <source src="path/to/video.mp4" type="video/mp4">
    <source src="path/to/video.webm" type="video/webm">
    <source src="path/to/video.ogv" type="video/ogv">
    Your browser does not support the video tag.
</video>
Enter fullscreen mode Exit fullscreen mode

In this example, three <source> tags are used, each specifying a different video format: MP4, WEBM, and OGV. The browser will try to play the first video format, and if it's not compatible, it will try the next one, and so on. This ensures that the video will play on most modern browsers.
It's important to note that some video formats may not be supported by all browser versions.
It's also important to ensure that the video files specified in each <source> tag are valid and accessible, as the browser will only play the video if it is accessible.
Also, include a fallback text if the browser does not support the video tag; it will be displayed instead of the video.
You can use a service like Cloudinary or Video.js that handles the video format conversion and makes it easier to handle cross-browser compatibility.

Customize video player controls using CSS

The video player controls are contained within a <div> element with the class .vjs-control-bar that can be targeted with CSS.
Here is an example of how to change the background color of the control bar:

.vjs-control-bar {
    background-color: blue;
}
Enter fullscreen mode Exit fullscreen mode

You can also target specific control elements within the control bar and customize their appearance. For example, you can change the color of the play/pause button:

.vjs-play-control {
    color: white;
}
Enter fullscreen mode Exit fullscreen mode

You can also use CSS to hide the default controls and create your own custom controls. To do this, you can hide the default controls by setting the display property to none:

.vjs-control-bar {
    display: none;
}
Enter fullscreen mode Exit fullscreen mode

Then, you can create your own custom controls using HTML and CSS. For example, you can create a play/pause button using a <button> element and styling it with CSS:

<button class="custom-play-button">Play</button>
Enter fullscreen mode Exit fullscreen mode
.custom-play-button {
    background-color: blue;
    color: white;
    padding: 10px;
    border-radius: 5px;
}
Enter fullscreen mode Exit fullscreen mode

You can also use JavaScript to make the custom play button interact with the video.

Note: Customizing the video player controls requires a good knowledge of CSS and JavaScript. The design of the controls is based on the video player you are using.
Also, make sure to test your custom controls in different browsers to ensure that they look and function correctly across all platforms. You can use pre-built CSS libraries such as Video.js or Plyr.js that provide a lot of customization options for the video player controls.

Examples of using the video tag on a website

There are many ways to use the HTML5 <video> tag on a website. Here are a few examples:
1. Product demonstration: You can use this tag to embed product demonstration videos on an e-commerce website; this allows users to see the product in action before making a purchase.

<video width="320" height="240" controls>
    <source src="path/to/product-demo.mp4" type="video/mp4">
    <source src="path/to/product-demo.webm" type="video/webm">
    Your browser does not support the video tag.
</video>
Enter fullscreen mode Exit fullscreen mode

2. Tutorial video: You can use the <video> tag to embed tutorial videos on a tutorial website. This allows users to learn a new skill by watching a video instead of reading text.

<video width="320" height="240" controls>
    <source src="path/to/tutorial.mp4" type="video/mp4">
    <source src="path/to/tutorial.webm" type="video/webm">
    Your browser does not support the video tag.
</video>
Enter fullscreen mode Exit fullscreen mode

3. Background video: You can use the <video> tag to set a background video on a landing page. This can be a great way to grab the attention of visitors and make the website more engaging.

<video width="100%" height="100%" autoplay loop muted>
    <source src="path/to/background-video.mp4" type="video/mp4">
    <source src="path/to/background-video.webm" type="video/webm">
    Your browser does not support the video tag.
</video>
Enter fullscreen mode Exit fullscreen mode

4. Video Advertisements: You can use the <video> tag to embed video advertisements on a website. This can be a great way to monetize a website and generate revenue.

<video width="320" height="240" controls>
    <source src="path/to/ad-video.mp4" type="video/mp4">
    <source src="path/to/ad-video.webm" type="video/webm">
    Your browser does not support the video tag.
</video>
Enter fullscreen mode Exit fullscreen mode

These are just a few examples of how the HTML5 <video> tag can be used in a website. With the right approach and design, you can use this tag to create engaging and interactive websites that stand out.

HTML5 Audio Tag

The HTML5 <audio> tag allows developers to easily embed audio files into their HTML code, making it simple to play sound on any web page. With this tag, you can control the layout of the audio player, add custom controls, and even provide multiple audio formats for cross-browser compatibility.
Here, let’s dive into the <audio> tag and its various attributes and learn how to embed audio files on a website. Furthermore, I’ll delve into how to customize the audio player controls using CSS and provide real-life examples of how this tag can be utilized in a website for an enhanced user experience.

Explanation of the audio tag and its attributes

With the <audio> tag, you can control the layout of the audio player, add custom controls, and even provide multiple audio formats for cross-browser compatibility.
Several attributes can be used to control the behavior and appearance of the audio player. The src attribute is used to specify the source of the audio file, and the controls attribute adds default controls to the player such as the play/pause button, volume control, and progress bar.
The preload attribute is used to specify how the audio should be loaded, and the autoplay, loop, and muted attributes can be used to control the behavior of the audio.
It's also possible to include multiple <source> tags inside the <audio> tag to provide different audio formats for cross-browser compatibility. For example, you might include an MP3 and OGG version of the same audio file, allowing the browser to automatically choose and play the right audio format.

Embed an audio file on a website using the audio tag

Adding an audio file to a website using the HTML5 <audio> tag is a simple process. Here is an example of how it can be done:

<audio controls>
    <source src="path/to/audio.mp3" type="audio/mpeg">
    Your browser does not support the audio tag.
</audio>
Enter fullscreen mode Exit fullscreen mode

In this example, the <audio> tag is used to create an audio player, and the controls attribute is added to display the default controls for the audio player. Inside this tag is a <source> tag used to specify the source of the audio file in mp3 format.
The src attribute is used to specify the path to the audio file, and the type attribute is used to specify the type of the audio file. This way, the browser can choose the right format to play the audio.
If the browser does not support the <audio> tag, the text between the tag will be displayed.
Also, be aware that the path to the audio file must be a valid URL, whether it is hosted on your server or a third-party server.

Add multiple audio formats for cross-browser compatibility

To ensure that your audio files can be played on most browsers, it's important to provide multiple audio formats. The <source> tag allows you to do this easily. By using multiple <source> tags within the <audio> tag, you can specify different audio files in different formats, and the browser will automatically choose the best format to play the audio.
For example, you can include an MP3, OGG, and WAV version of the same audio file:

<audio controls>
    <source src="path/to/audio.mp3" type="audio/mpeg">
    <source src="path/to/audio.ogg" type="audio/ogg">
    <source src="path/to/audio.wav" type="audio/wav">
    Your browser does not support the audio tag.
</audio>
Enter fullscreen mode Exit fullscreen mode

This way, the browser will try to play the first audio format, and if it's not compatible, it will try the next one, and so on. This ensures that the audio will play on most modern browsers.
It's worth noting that not all audio formats are supported by all browsers, so make sure to include formats that are widely supported.
Ensure that the audio files specified in each <source> tag are valid and accessible, as the browser will only play the audio if the file is accessible.
You can also include a fallback text within the <audio> tags in case the browser does not support the audio tag, it will be displayed instead of the audio.
Furthermore, you can consider using a service like Cloudinary, which can handle the audio format conversion and make it easier to handle cross-browser compatibility.

Customize audio player controls using CSS

The <audio> tag provides basic controls for playing audio on a website, but you can use CSS to customize the appearance of these controls. Here is an example of how to use CSS to change the background color of the default audio controls:

<audio controls>
    <source src="path/to/audio.mp3" type="audio/mpeg">
    Your browser does not support the audio tag.
</audio>
Enter fullscreen mode Exit fullscreen mode
audio::-webkit-media-controls-panel {
    background-color: #ff0000;
}
Enter fullscreen mode Exit fullscreen mode

In this example, we're using the ::-webkit-media-controls-panel pseudo-element to select the panel that contains the default audio controls. We then use the background-color property to change the background color of the panel to red.

Note that the above CSS only works on WebKit-based browsers and you will have to use the vendor-specific pseudo-element for other browsers.
You can also use CSS to change the appearance of individual controls, such as the play button, pause button, and volume control. You can use the ::-webkit-media-controls-play-button, ::-webkit-media-controls-pause-button, and ::-webkit-media-controls-volume-slider pseudo-elements to select these controls and use CSS to change their appearance.

Examples of using the audio tag on a website

Here are a few examples of how the HTML5 <audio> tag can be used on a website:
1. Background music: You can use the <audio> tag to play background music on a website. By setting the autoplay attribute and hiding the controls, you can create a seamless background music experience for your users.

<audio autoplay controls style="display:none;">
    <source src="path/to/audio.mp3" type="audio/mpeg">
    Your browser does not support the audio tag.
</audio>
Enter fullscreen mode Exit fullscreen mode

2. Interview or podcast player: You can use the <audio> tag to create a player for an interview or podcast. By using the controls attribute and customizing the appearance using CSS, you can create a professional-looking player for your audio content.

<audio controls>
    <source src="path/to/audio.mp3" type="audio/mpeg">
    Your browser does not support the audio tag.
</audio>
Enter fullscreen mode Exit fullscreen mode
audio::-webkit-media-controls-play-button,
audio::-webkit-media-controls-pause-button {
   background-color: red;
}
Enter fullscreen mode Exit fullscreen mode

To style the progress bar, you can add this CSS to your stylesheet:

audio::-webkit-media-controls-timeline {
   background: linear-gradient(to right, blue, green);
}
Enter fullscreen mode Exit fullscreen mode

Video and Audio Optimization

Video and audio optimization ensures that your multimedia content loads quickly and plays smoothly on a website. This is especially important for large multimedia files that can take a long time to load or may not play correctly on certain devices or browsers. Optimizing videos and audio can involve reducing the file size, using the appropriate file format, and providing multiple formats for cross-browser compatibility.
In this section, I’ll discuss various techniques and best practices for optimizing your video and audio content to ensure that it loads quickly and plays smoothly on a website. This can include compressing video and audio files, using adaptive streaming, and using a content delivery network.

Video and audio file formats and their relative merits

There are several different video and audio file formats that can be used for web multimedia, each with its set of advantages and disadvantages. Here are a few common file formats and their relative merits:
MP4 (H.264/AVC): MP4 is a popular video file format that uses the H.264 video codec and the AAC audio codec. It is supported by most modern browsers and devices and offers good video quality and small file size.
WebM (VP8/VP9): WebM is an open-source video file format that uses the VP8 or VP9 video codec and the Vorbis or Opus audio codec. It is supported by most modern browsers, but not all devices. WebM files tend to be smaller in size than MP4 files, but the video quality may not be as good.
OGG (Theora/Vorbis): OGG is an open-source video and audio file format that uses the Theora video codec and the Vorbis audio codec. It is supported by some modern browsers and devices, but not as widely as MP4 or WebM. OGG files tend to be smaller in size than MP4 or WebM files, but the video and audio quality may not be as good.
MP3: MP3 is a popular audio file format that uses a lossy compression algorithm to reduce file size. It is supported by most modern browsers and devices. It has good audio quality, but it's not an open-source format.
OGG (Vorbis): OGG is an open-source audio file format that uses the Vorbis audio codec. It is supported by some modern browsers and devices but not as widely as MP3. OGG files tend to be smaller in size than MP3 files, and the audio quality may be better. Also, OGG files also have better support for metadata and can handle more channels and sample rates than MP3.
WAV: WAV is a lossless audio file format that uses a large amount of disk space and bandwidth. Most modern browsers and devices support it, and it has very good audio quality.

Note: Not all devices and browsers support all file formats, so it's best practice to provide multiple formats of a video or audio file to ensure that it will play on most devices and browsers.
When choosing a video and audio file format, it's important to consider the quality of the file, the compatibility with different browsers, and the file size. It's also important to consider if the format is open-source or proprietary.

Best practices for optimizing video and audio files for web use

Several best practices can be followed to optimize video and audio files for web use:
Compress the files: Compressing video and audio files can significantly reduce the file size without sacrificing too much quality. Several tools are available for compressing video and audio files, such as Handbrake for videos and Audacity for audio.
Use adaptive streaming: Adaptive streaming is a technique that adjusts the quality of a video or audio stream in real-time based on the viewer's network conditions. This ensures that the video or audio will play smoothly on a wide range of devices and network conditions.
Use a Content Delivery Network (CDN): A CDN is a network of servers distributed worldwide. By using a CDN, you can ensure that your video and audio files are served to viewers from a server that is geographically close to them, which can significantly reduce the load time.
Use web-friendly codecs: Some codecs like H.264 for video or AAC for audio, are more web-friendly than others. Most modern browsers and devices widely support codecs.
Use progressive download instead of streaming: Progressive download is a technique that allows the video or audio file to play as soon as it starts to download. This is a good option for shorter video or audio files, as it eliminates the need for a streaming server.
Compress video and audio files for faster loading times

Compress video and audio files for faster loading times

There are several ways to compress video and audio files for faster loading times:
Video Compression: One way to compress video files is to use a video transcoding tool such as Handbrake. Handbrake can be used to convert video files from one format to another, and it also allows you to adjust various settings such as the video bitrate, frame rate, and resolution to reduce the file size. Other tools such as FFmpeg, Adobe Media Encoder, or Compressor are also popular for video compression.
Audio Compression: You can use an audio editing tool such as Audacity to compress audio files. Audacity allows you to adjust the audio file's bit rate and sample rate to reduce its file size. Other tools such as iTunes, LameXP, and iTunes are also popular for audio compression.
Online Compression Services: There are also several online compression services that can be used to compress video and audio files. These services typically provide a user-friendly interface and allow you to upload and compress your files directly from the web. Some popular online compression services include Cloudinary, Kraken.io, and TinyJPG.

When compressing video and audio files, finding the right balance between file size and quality is important. Lowering the bitrate too much can result in poor video or audio quality while keeping it too high will result in larger file sizes. It's also important to note that different codecs may have different compression efficiency. For example, H.264 codec is more efficient than VP9.
It's best practice to test your compressed video and audio files on different devices and network conditions to make sure that they play smoothly and don't have any issues.

Accessibility

Accessibility is about ensuring everyone can use and enjoy your website, regardless of their abilities. When it comes to videos and audio, that means ensuring that even those who can't see or hear can still access and understand the content.
In this section, I'll discuss ways to ensure that your videos and audio are accessible to everyone. This can include adding captions, subtitles, and audio descriptions and providing alternative ways to access the content. It's important to remember that accessibility is not just a legal requirement, but making sure that everyone can benefit from your website is also the right thing to do.

Importance of accessibility in multimedia elements

Accessibility in multimedia elements is essential to ensure that everyone, including people with disabilities, can access and understand the content. This is important for several reasons:
Legal compliance: There are laws and regulations, such as the Americans with Disabilities Act (ADA) and the Rehabilitation Act, that require websites to be accessible to people with disabilities. Failing to make your multimedia elements accessible can result in legal action and fines.
Inclusion: By making your multimedia elements accessible, you are ensuring that everyone, regardless of their abilities, can access and use your website. This promotes inclusion and helps to create a more diverse and inclusive online community.
Better user experience: Making your multimedia elements accessible improves the user experience for people with disabilities. For example, adding captions to a video can make it accessible to people who are deaf or hard of hearing, while adding audio descriptions can make it accessible to people who are blind or visually impaired.
Search engine optimization: Making your multimedia elements accessible can also improve your search engine optimization (SEO) by making it possible for search engines to understand the content of your multimedia elements.
Wider audience: By making your multimedia elements accessible, you can reach a wider audience, including people with disabilities, who may not have been able to access your content otherwise.

Add closed captions and subtitles to videos

Create captions or subtitles manually:
You can create captions or subtitles manually by adding a text file to your video. This text file contains captions or subtitles in a specific format, such as SRT or VTT. The text file can then be associated with the video using the <track> element in the <video> tag.

<video>
    <source src="path/to/video.mp4" type="video/mp4">
    <track src="path/to/captions.vtt" kind="captions" srclang="en" label="English">
</video>
Enter fullscreen mode Exit fullscreen mode

Add audio descriptions to videos

Create audio descriptions manually:
You can create audio descriptions manually by recording a voiceover of the descriptions and syncing it with the video. This can be done using video editing software or a digital audio recorder. The audio descriptions can then be associated with the video using the <track> element in the <video> tag.

<video>
    <source src="path/to/video.mp4" type="video/mp4">
    <track src="path/to/audio-description.mp3" kind="descriptions" srclang="en" label="English">
</video>
Enter fullscreen mode Exit fullscreen mode

Making videos and audio accessible to users with disabilities

Making sure that your videos and audio are accessible to everyone is important, especially for those with disabilities. Here are some best practices to follow to ensure that your multimedia content is inclusive:
Include captions and subtitles: People who are deaf or hard of hearing rely on captions and subtitles to understand the audio in a video. Captions should include sound effects, and subtitles should include only the dialogue.
Add audio descriptions: For those who are blind or visually impaired, audio descriptions verbally explain what's happening on the screen. Ensure that the descriptions are synced with the video and are easy to understand.
Provide alternative ways to access the content: Some people may not be able to see, hear or interact with traditional forms of multimedia, so providing alternative ways to access the content, such as transcriptions or summaries, is important.
Ensure compatibility with assistive technology: Ensure that your videos and audio work well with assistive technology like screen readers or closed captioning devices.
Include controls and options: Users should have control over the video and audio playback and have options to adjust settings such as closed captioning and audio descriptions.
Test your content: Test your content on different devices and browsers and ensure that it works well with assistive technology.
Keep navigation simple: Make it easy for users to find and access the multimedia content on your website.

Conclusion

In summary, this blog post taught us how to add video and audio to websites using HTML5 tags, customize player controls with CSS, make multimedia accessible, and optimize files for web use. I hope you found it informative and helpful in creating inclusive, engaging, and optimized websites. Remember to follow best practices for accessibility and optimization to give all users a better experience.


Resources

HTML5 Video Tag Guide
Style the HTML 5 Audio
Video Optimization Guide For Your Website
Optimize Your Website for Audio
Design an Accessible Website


@fatimaola and Hackmamba squad, thanks for executing an exhaustive review and contributing insightful feedback on the draft.

Follow me on Twitter @nelo_cg

Top comments (0)