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

20 Best New Websites, April 2024

Welcome to our sites of the month for April. With some websites, the details make all the difference, while in others,…

Exciting New Tools for Designers, April 2024

Welcome to our April tools collection. There are no practical jokes here, just practical gadgets, services, and apps to…

14 Top UX Tools for Designers in 2024

User Experience (UX) is one of the most important fields of design, so it should come as no surprise that there are a…

What Negative Effects Does a Bad Website Design Have On My Business?

Consumer expectations for a responsive, immersive, and visually appealing website experience have never been higher. In…

10+ Best Resources & Tools for Web Designers (2024 update)

Is searching for the best web design tools to suit your needs akin to having a recurring bad dream? Does each…

3 Essential Design Trends, April 2024

Ready to jump into some amazing new design ideas for Spring? Our roundup has everything from UX to color trends…

How to Plan Your First Successful Website

Planning a new website can be exciting and — if you’re anything like me — a little daunting. Whether you’re an…

15 Best New Fonts, March 2024

Welcome to March’s edition of our roundup of the best new fonts for designers. This month’s compilation includes…

LimeWire Developer APIs Herald a New Era of AI Integration

Generative AI is a fascinating technology. Far from the design killer some people feared, it is an empowering and…

20 Best New Websites, March 2024

Welcome to our pick of sites for March. This month’s collection tends towards the simple and clean, which goes to show…

Exciting New Tools for Designers, March 2024

The fast-paced world of design never stops turning, and staying ahead of the curve is essential for creatives. As…

Web Tech Trends to Watch in 2024 and Beyond

It hardly seems possible given the radical transformations we’ve seen over the last few decades, but the web design…