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

3 Essential Design Trends, November 2024

Touchable texture, distinct grids, and two-column designs are some of the most trending website design elements of…

20 Best New Websites, October 2024

Something we’re seeing more and more of is the ‘customizable’ site. Most often, this means a button to swap between…

Exciting New Tools for Designers, October 2024

We’ve got goodies for designers, developers, SEO-ers, content managers, and those of you who wear multiple hats. And,…

15 Best New Fonts, September 2024

Welcome to our roundup of the best new fonts we’ve found on the web in the previous four weeks. In this month’s edition…

3 Essential Design Trends, October 2024

This article is brought to you by Constantino, a renowned company offering premium and affordable website design You…

A Beginner’s Guide to Using BlueSky for Business Success

In today’s fast-paced digital world, businesses are always on the lookout for new ways to connect with their audience.…

The Importance of Title Tags: Tips and Tricks to Optimize for SEO

When it comes to on-page SEO, there’s one element that plays a pivotal role in both search engine rankings and user…

20 Best New Websites, September 2024

We have a mixed bag for you with both minimalist and maximalist designs, and single pagers alongside much bigger, but…

Exciting New Tools for Designers, September 2024

This time around we are aiming to simplify life, with some light and fast analytics, an all-in-one productivity…

3 Essential Design Trends, September 2024

September's web design trends have a fun, fall feeling ... and we love it. See what's trending in website design this…

Crafting Personalized Experiences with AI

Picture this: You open Netflix, and it’s like the platform just knows what you’re in the mood for. Or maybe you’re…

15 Best New Fonts, August 2024

Welcome to August’s roundup of the best fonts we’ve found over the last few weeks. 2024’s trend for flowing curves and…