How to play video with HTML5

Wdd Logo.
October 16, 2013
How to play video with HTML5.

thumbnailPlacing a video in HTML5 markup is simple, no more complex for any given browser than placing an image. In this article we'll take full advantage of the built-in browser support to build the simplest possible video player.

We'll lay the application's basic framework and then use the <video> element to add a video to the web page without the need of any plugins at all.

Prerequisites

Use Chrome, Safari or Internet Explorer 9+. For the time-being you'll have to avoid Firefox and Opera because of the cross-browser video file format issues. Although support for the video element is consistent across all modern browsers, the MP4 format trips up Firefox and Opera. You can check for compatibility here.

Before you begin you'll need to find a .mp4 that you can use, if you don't have one, you'll find lots of free mp4 files online.

Building the basic framework

The following code is the framework around which you build the player. It creates a simple layout and has a placeholder for the video itself.

You need to create a new HTML file in your working directory and name it index.html, then add this code:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5 Video Player</title>
<style>
body {
font-family: sans-serif;
border: 0;
margin: 0;
padding: 0;
}
header {
text-align: center;
}
#player {
display: table;
width: 100%;
padding: 4px;
}
#player > div {
display: table-cell;
vertical-align: top;
}
</style>
</head>
<body>
<header>
<h1>HTML5 Video Player</h1>
</header>
<section id="player">
<div>
<!-- The video will appear here-->
</div>
</section>
</body>
</html>

Now, with the foundation laid, let's get to the fun part of the player by adding a video to the page.

Using the video element to add videos to web pages

The goal in designing HTML5's <video> element was to make the embedding of video within a web page as straightforward as embedding an image. Although you'll encounter additional complexities because video file formats are more feature-rich than image formats, the design goal has been attained.

Here's what an HTML5 video looks like in Chrome:

html5_action_002

The next listing shows all of the code required to display the video. As you can see, it's not complicated.

Insert this code in place of the "<!-- The video will appear here-->" comment in the code above, make sure you replace [ YOUR VIDEO ] with the path to your .mp4, and refresh the page.

<video src="[ YOUR VIDEO ].mp4"
controls
width="720" height="480">
Your browser does not support the video element, please #D try <a href="videos/VID_20120122_133036.mp4">downloading the video instead</a>
</video>

In this code the src attribute is the path and file name of your .mp4 file, the controls attribute indicates that you would like the video to have the standard controls (omit it if you don't want the controls) and the width and height are self-explanatory. The text inside the element is back up text to be displayed if the video tag isn't recognized by the browser.

Summary

The web is increasingly being used as a replacement for traditional broadcast media. Services like Netflix, YouTube, Spotify, last.fm, and Google Music seek to replace your DVD and CD collections with online players. With HTML5, video and audio become first-class citizens of web content. Rather than handing responsibility for playing media to a third-party application, it's played within the browser, allowing you to control and manipulate media from within your web application.

As you can see HTML5 makes it as straightforward a process to add video and audio to web pages as it is to add images.

How do you prefer to handle video? Is HTML5's <video> element robust enough? Let us know your thoughts in the comments.

Featured image/thumbnail, video image via Shutterstock.

WDD Staff

WDD staff are proud to be able to bring you this daily blog about web design and development. If there's something you think we should be talking about let us know @DesignerDepot.

Read Next

15 Best New Fonts, July 2024

Welcome to our monthly roundup of the best fonts we’ve found online in the last four weeks. This month, there are fewer…

20 Best New Websites, July 2024

Welcome to July’s round up of websites to inspire you. This month’s collection ranges from the most stripped-back…

Top 7 WordPress Plugins for 2024: Enhance Your Site's Performance

WordPress is a hands-down favorite of website designers and developers. Renowned for its flexibility and ease of use,…

Exciting New Tools for Designers, July 2024

Welcome to this July’s collection of tools, gathered from around the web over the past month. We hope you’ll find…

3 Essential Design Trends, July 2024

Add some summer sizzle to your design projects with trendy website elements. Learn what's trending and how to use these…

15 Best New Fonts, June 2024

Welcome to our roundup of the best new fonts we’ve found online in the last month. This month, there are notably fewer…

20 Best New Websites, June 2024

Arranging content in an easily accessible way is the backbone of any user-friendly website. A good website will present…

Exciting New Tools for Designers, June 2024

In this month’s roundup of the best tools for web designers and developers, we’ll explore a range of new and noteworthy…

3 Essential Design Trends, June 2024

Summer is off to a fun start with some highly dramatic website design trends showing up in projects. Let's dive in!

15 Best New Fonts, May 2024

In this month’s edition, there are lots of historically-inspired typefaces, more of the growing trend for French…

How to Reduce The Carbon Footprint of Your Website

On average, a web page produces 4.61 grams of CO2 for every page view; for whole sites, that amounts to hundreds of KG…

20 Best New Websites, May 2024

Welcome to May’s compilation of the best sites on the web. This month we’re focused on color for younger humans,…