HTML5 Development Center

Developed for you in part by
 
653-html5-fullscreen-api

How to Use the HTML5 Full-Screen API

By | | CSS3 | HTML | HTML5 | JavaScript

Flash has offered a full-screen mode for many years but, until now, browser vendors have resisted the feature. The main reason: security. If you can force an app to run full-screen, the user loses their browser, taskbar and standard operating system controls. They may not be able to close the window or, worse, nefarious developers could emulate the OS and trick users into handing over passwords, credit card details, etc.

At the time of writing, the HTML5 full-screen API has been implemented in Firefox, Chrome and Safari. Mozilla provide good cross-browser details but it’s worth noting that the specifications and implementation details are likely to change.

Unlike pressing F11 to make your browser go full-screen, the API sets a single element full-screen. It’s intended for images, videos and games using the canvas element. Once an element goes full-screen, a message appears temporarily to inform the user that they can press ESC at any time to return to windowed mode.

The main properties, methods and styles are:

element.requestFullScreen()
Makes an individual element full-screen, e.g. document.getElementById(“myvideo”).requestFullScreen().

document.cancelFullScreen()
Exits full-screen mode and returns to the document view.

document.fullScreen
Returns true if the browser is in full-screen mode.

:full-screen
A CSS pseudo-class which applies to an element when it’s in full-screen mode.

Vexing Vendor Prefixes

Don’t bother trying to use these API names. You’ll require vendor prefixes for BOTH the CSS and JavaScript properties:

StandardChrome/SafariFirefox
.requestFullScreen().webkitRequestFullScreen().mozRequestFullScreen()
.cancelFullScreen().webkitCancelFullScreen().mozCancelFullScreen()
.fullScreen.webkitIsFullScreen.mozfullScreen
:full-screen:-webkit-full-screen:-moz-full-screen

There’s no support in Internet Explorer or Opera yet, but I would suggest you use the ‘ms’ and ‘o’ prefixes for future proofing.

I’ve developed a function in the demonstration page which handles the prefix shenanigans for you:


var pfx = ["webkit", "moz", "ms", "o", ""];
function RunPrefixMethod(obj, method) {
	var p = 0, m, t;
	while (p < pfx.length && !obj[m]) {
		m = method;
		if (pfx[p] == "") {
			m = m.substr(0,1).toLowerCase() + m.substr(1);
		}
		m = pfx[p] + m;
		t = typeof obj[m];
		if (t != "undefined") {
			pfx = [pfx[p]];
			return (t == "function" ? obj[m]() : obj[m]);
		}
		p++;
	}
}

We can then make any element viewable full screen by attaching a handler function which determines whether it’s in full-screen mode already and acts accordingly:


var e = document.getElementById("fullscreen");
e.onclick = function() {
	if (RunPrefixMethod(document, "FullScreen") || RunPrefixMethod(document, "IsFullScreen")) {
		RunPrefixMethod(document, "CancelFullScreen");
	}
	else {
		RunPrefixMethod(e, "RequestFullScreen");
	}
}

The CSS

Once the browser enters full-screen mode you’ll almost certainly want to modify the styles for the element and it’s children. For example, if your element normally has a width of 500px, you’ll want to change that to 100% so it uses the space available, e.g.


#myelement
{
	width: 500px;
}
#myelement:full-screen
{
	width: 100%;
}
#myelement:full-screen img
{
	width: 100%;
}

However, you cannot use a list of vendor prefixed selectors:


/* THIS DOES NOT WORK */
#myelement:-webkit-full-screen,
#myelement:-moz-full-screen,
#myelement:-ms-full-screen,
#myelement:-o-full-screen,
#myelement:full-screen
{
	width: 100%;
}

For some bizarre reason, you must repeat the styles in their own blocks or they won’t be applied:


/* this works */
#myelement:-webkit-full-screen	{ width: 100% }
#myelement:-moz-full-screen		{ width: 100% }
#myelement:-ms-full-screen		{ width: 100% }
#myelement:-o-full-screen		{ width: 100% }
#myelement:full-screen			{ width: 100% }

Weird.

View the demonstration page in Firefox, Chrome or Safari…

The technique works well. The only issue I’ve discovered concerns Safari on a two-monitor desktop — it insists on using the first monitor for full-screen mode even if the browser is running on the second screen?

While it’s possibly a little early to use full-screen mode, games developers and video producers should keep an eye on progress.

If you enjoyed reading this post, you’ll love Learnable; the place to learn fresh skills and techniques from the masters. Members get instant access to all of SitePoint’s ebooks and interactive online courses, like HTML5 & CSS3 For the Real World.

The Ultimate JavaScript Bundle: 2 books + 1 course

Buy now $39 Normally $117 - save 66%

Or get access to all SitePoint's Premium Content with a Learnable membership

Craig Buckler

Craig is a Director of OptimalWorks, a UK consultancy dedicated to building award-winning websites implementing standards, accessibility, SEO, and best-practice techniques.

More Posts - Website

{ 19 comments }

noypiscripter April 26, 2012 at 11:21 am

“For some bizarre reason, you must repeat the styles in their own blocks or they won’t be applied:”

I think all browsers behave like that. If there are multiple CSS rules and one of the selectors in a rule are not supported by a browser, the styles will not be applied at all for that browser.

For example, we all know that :last-child is not supported in IE8. If you do the CSS rules like this, the .lastchildforIE8 selector will not be applied in IE8 even if the last li has that class.

ul li.lastchildforIE8, ul li:last-child {
color:red;
}

jno manh April 25, 2012 at 10:19 pm

How can I view the page in fullscreen, not only the photo? Thanks a million!

Sindre Sorhus April 22, 2012 at 8:31 am

I made a simple wrapper for the Fullscreen API to fix some inconsistencies in the different implementations and hide away the prefixes.

https://github.com/sindresorhus/screenfull.js

Let me know if you find it useful :)

Anthony Goodley April 19, 2012 at 7:07 pm

Worked perfectly in Chrome 18.0.1025.162 besides the stretch to fit issues others mentioned.

Fail in Firefox 8.0.

Worked in Firefox 11.0 after upgrading. Also uses the evil stretch to fit method for the image.

Fail in Opera 11.52. and Opera 11.62

Fail in Internet Explorer 9.0.8112.16421.

Worked in Safari 5.1.2 (7534.52.7) BUT Safari did not either ask permission to display the picture in full screen mode or give a message informing that the ESC key can be used to exit full screen mode. Safari also stretched to fit the image. Very bad in my opinion.

Using Windows 7 Home Premium OS.

Until most, if not all, of these browsers can all handle the HTML5 full screen API without resorting to stretch to fit behavior I can’t see using this in the photo gallery script I’m making. Would letterboxing be so hard for the browsers to implement? Makes me sad as this is pretty cool and easy to implement.

Francesco April 19, 2012 at 6:52 am

One interesting thing I noticed while using this in “real life”.

In Firefox, if I use the CancelFullScreen method on an actual link (as in, I’m in full-screen mode and I have a link to a different page that I don’t want to show in full-screen mode so I need to exit *and* change url), the first click will only exit full-screen mode and it needs a second click to actually change page. In Chrome this doesn’t happen and one click is enough.

Adding a simple location.href = $(this).attr(“href”) (if using jQuery) will work, anyway.

It seems that CancelFullScreen, if actually used to exit full-screen mode, doesn’t let the default action propagate.

manoj April 16, 2012 at 6:15 am

i was searching this code and css

thank u very much…..

:)

Mike April 13, 2012 at 12:58 pm

It’s working fine with the photo.
How can I view the page in fullscreen, not only the photo? Thanks a million!

Berra April 12, 2012 at 5:41 pm

The demo image is in 4:3 format.
In fullscreen mode it stretches to fill my 16:9 screen.
That is not very cool.
Why doesn’t it letterbox and keep the w/h ratio?

Berra April 12, 2012 at 5:51 pm

Correction:
The image fills the width of the screen, but it’s not stretched out of proportion.
Instead the bottom part of the image is cropped.
[Firefox 11.0 & latest Chrome]

Is there a way to fit the whole image in fullscreen?

Thx

Craig Rogers April 12, 2012 at 3:53 pm

Didn’t work on my ipad. Anyone else have the same problem?

Brockdin Barr April 12, 2012 at 1:52 pm

I can see this being really awesome on photo galleries, especially if gestures were added for mobile devices.

Nick Ciske April 12, 2012 at 12:15 pm

Safari 5.1.5 handled my dual monitor set-up just fine — the example went full screen on the same monitor the browser was on.

Nick Ciske April 12, 2012 at 12:18 pm

Things got weird when my browser spanned both monitors though… the image appeared full screen on the main monitor, and extended part way onto the second monitor (by as much as the browser extended into that screen).

Luis Alberto Hernandez April 12, 2012 at 11:25 am

Nice !!! working fine in Chrome but in Chromium not working :(

Jason April 12, 2012 at 12:18 am

Do people ever actually use full screen, I mean people, not your weird Linux friend…

Francesco April 10, 2012 at 6:57 am

Love it. I noticed Facebook started to use it a couple of weeks ago, but this article makes it REALLY clear (it’s really very simple, actually).

One thing I think it’s worth it to point out is that the element you click doesn’t necessarily need to be the one that goes fullscreen… it’s kind of obvious, but since in the example it’s the same element somebody might think it MUST be the same! :-)

Muhamad Abdul Hay April 9, 2012 at 7:56 pm

Nice works!

It works on my Safari & Chrome browser. Thank you for sharing the RunPrefixMethod function with us.

Thanks again!

Steve April 5, 2012 at 4:10 pm

Worked in Chrome, but not Firefox or Safari.

Luke Madhanga April 5, 2012 at 3:36 pm

Hey, nice tut mate.

A man once said, a good programmer writes code Humans can read. Although the runPrefixMethod thing looks absolutely awesome and an absolute time saver, Humans won’t understand that. Can I suggest you add some comments or you use long variables.

Nice One Mate, Luke Madhanga – Walk Left Studios

Comments on this entry are closed.