One thing which has been very important when it comes to creating special end user experiences have been the ability to show something fullscreen, effectively hiding all the other content etc.

Remember when web sites gave you instructions how to configure your web browser with hiding toolbars and more, just to get a slightly better user experience? Or maybe it’s just me… :-)

Either way, some time ago we got fullscreen support in web browsers where the user could choose to view the current web site in fullscreen. That’s all good and well, but as an extension to that, as web developers we want to be able to trigger that. Either for the entire web site or just a specific element.

And now we can!

Requesting fullscreen

We now have access to a method called requestFullScreen, so far implemented in Firefox, Google Chrome and Safari. Therefore, to make it work at the moment, we need this code:


Please note that the Fullscreen standard in the W3C specification uses a lowercase ‘s’ in all methods, whereas Firefox, Google Chrome and Safari use an uppercase one.

What the code above does is just getting a reference to the documentElement and request for it to be displayed fullscreen. Naturally, you could also make just a certain element fullscreen, for instance, a video, with the same method called for the element you wish.

Cancelling fullscreen

If you want to cancel the fullscreen state, you need to call it on the document element:


Note here that W3C has decided to call it exitFullscreen, but in all existing web browser implementations it’s about cancelling the state.

Detecting fullscreen state change

The user could, for instance, exit fullscreen, something that might be good for you to know. For that we have a fullscreenchange event, that you can apply both to the element that requested fullscreen, but also to the document. Then we just detect the fullscreen state and take act accordingly, like this:


document.addEventListener("mozfullscreenchange", function () {
fullscreenState.innerHTML = (document.mozFullScreen)? "" : "not ";
}, false);

document.addEventListener("webkitfullscreenchange", function () {
fullscreenState.innerHTML = (document.webkitIsFullScreen)? "" : "not ";
}, false);

Styling fullscreen

In CSS, we get a number of pseudo-classes for styling fullscreen elements. The most reliable one is for full-screen and automatically gets triggered when the document/element is in fullscreen mode:


html:-webkit-full-screen {
background: red;
}

html:fullscreen {
background: red;
}

Notice here that the W3C approach doesn’t use a hyphen between the word ‘full’ and the word ‘screen’.

It should also be added that Firefox is the only web browser that applies a width and height of 100% to the element that is requesting fullscreen, since we believe that is the desired behavior. This can of course be overridden with the above CSS.

Full screen with key input

For security reasons, most keyboard inputs have been blocked in the fullscreen mode. However, in Google Chrome you can request keyboard support by calling the method with a flag:

docElm.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);

This does not work in Safari, and the method won’t be called.

With Firefox, we are discussing and looking into various ways of how we we could add keyboard input support without jeopardizing the end user’s security. One suggestion, that no one has implemented yet, is the requestFullscreenWithKeys method, which in turn would trigger certain notifications for the user.

Web browser support

This feature is currently available in Firefox beta, but it’s due to land in the official release of Firefox, version 10, tomorrow! It has also been available in Google Chrome since version 15 and Safari since 5.1.

Play with fullscreen!

I have a Fullscreen API demo available for you to play with, and all the code is available in the Fullscreen repository on GitHub.

27 comments

Post a comment
  1. Rodrigo Ayala wrote on January 30th, 2012 at 10:57 am:

    Excellent!, I didn’t know anything about this feature, a great functionality to show fullscreen videos or play cool games using the whole screen.

    Note: the demo doesn’t work for me, but I didn’t read the part where you say that this will be supported since Firefox 10 (and I have FF9) xD…

    Great post Robert!, thanks a lot :)

    Reply

    1. Robert Nyman wrote on January 30th, 2012 at 10:59 am:

      Indeed, it opens up a lot of interesting options! And thanks!
      Now go install a web browser version that you can try these in. :-)

      Reply

  2. James wrote on January 30th, 2012 at 11:23 am:

    Two questions:

    1) Does the browser treat Fullscreen that came via a web page trigger any differently than if a user just went to the toolbar View > Full Screen?

    2) I’m not sure I understand the limiting of key strokes while in full screen mode. If you fullscreen a web page that has a text area in it, does that mean I can’t type in it? According to the MDN documentation for full screen (https://developer.mozilla.org/en/DOM/Using_full-screen_mode) “any alphanumeric keyboard input while in full-screen mode causes a warning message to appear”. I just full screened this web page (View > Full Screen) and am typing this message, yet do not see any ‘warning message’. Should I see one? Perhaps the answer to this is related to my first question.

    Thanks!

    Reply

    1. Robert Nyman wrote on January 31st, 2012 at 2:41 am:

      Hi,

      1. Like Chris Pearce mentioned below, it treats keyboard input differently, and if you choose View > Full Screen in Firefox, you will see that the address bar is at the top, auto-hiding, whereas it’s not when called through code. Additionally, when used through the Fullscreen API, the user gets a notification it happened and that they can use the Esc to exit it.

      2. Right now, the warning that is referred to is the notification that you are in fullscreen mode and need to press Esc to leave it.

      Reply

  3. marcusklaas wrote on January 30th, 2012 at 11:39 am:

    Cool! It works nicely in Chrome indeed. Question: does this also work on devices like iPad with iOS 5?

    Reply

    1. marcusklaas wrote on January 30th, 2012 at 1:44 pm:

      Also, is it required that this fullscreen code is excecuted in an eventListener? Does not seem to work when I execute it outside without a user generated event..

      Reply

    2. Robert Nyman wrote on January 31st, 2012 at 2:42 am:

      No, it doesn’t work with iOS 5 yet. Maybe in the next update?

      Reply

  4. André Luís wrote on January 30th, 2012 at 3:00 pm:

    Cool writeup, Rob! And great to follow your writings here too, on the Mozilla site of things.

    Keep it up! And thanks. The support is already maturing nicely… so in the next few months this should become more common place.

    Reply

    1. Robert Nyman wrote on January 31st, 2012 at 2:42 am:

      Thank you! :-)
      Good to see you here, and enjoy the API!

      Reply

  5. Chris Pearce wrote on January 30th, 2012 at 6:17 pm:

    @James: View > Fullscreen mode and HTML fullscreen mode treat key input differently in order to guard against phishing attacks; if evilsite.com goes fullscreen and renders something that looks like a Firefox UI with a paypal login screen, it could trick users into entering their username and password.

    @marcusklaas: fullscreen requests are only granted in user generated event handlers (mouse click and key press handlers). This way the user can be in control of when the site enters fullscreen, also to help guard against phishing attacks.

    Reply

  6. Oliver Caldwell wrote on January 31st, 2012 at 8:19 am:

    Wrote a little wrapper class for the API. Did not want to go writing those if statements every time. https://gist.github.com/1710727

    Reply

    1. Robert Nyman wrote on January 31st, 2012 at 9:53 am:

      Nice, thank you!

      Reply

  7. John Dyer wrote on January 31st, 2012 at 3:15 pm:

    A few people asked about iOS, so here’s the answer. On an iPad with a [video] element, you can call myvideo.webkitEnterFullScreen() to make the video exit the browser and play in iOS’s native player. Not that it’s “enter” not “request”.

    Robert, could you also cover the details about fullscreenEnabled in Firefox and how it different from Firefox 9 to 10?

    Reply

    1. Robert Nyman wrote on February 1st, 2012 at 6:28 am:

      That is an option for iOS, yes, but not really as encompassing as the above API/approach is.

      With fullscreenEnabled, I’m not really aware of any differences since this was first properly initiated in Firefox 10. For further information, I recommend the MDN article on Fullscreen and Chris Pearce article on Fullscreen.

      Reply

  8. Joe Shelby wrote on February 2nd, 2012 at 8:22 pm:

    I’m not so sure “full screen” really makes sense in an IOS device (well, of the current crop – when the mac runs IOS directly, that may change things). The browser in an ipad/itouch/iphone (as well as in Android devices), is inherently already in full screen mode all the time. You don’t see borders. You don’t see windows, except through special UI elements to simulate them (such as Firefox for android’s off-screen left sidebar where everything is a ‘tab’). The most you could get out of it is an explicit request to hide the address bar which is normally shown whenever the window is smaller than the screen.

    Reply

    1. Robert Nyman wrote on February 3rd, 2012 at 1:20 am:

      Yes, the benefit from it is not nearly as much as on desktop, but granted, it could still make the user experience better in some cases.

      Reply

  9. kris wrote on February 9th, 2012 at 4:53 am:

    hey robert, great post. thanks for sharing!

    i m recently working on the same issue. does anyone know, if our lovely internet explorer will have something similiar?

    if you press “F11″ even internet explorer switches to fullscreen modus like every other browser. unfortunately you cannot fire a F11-keypress event via javascript due security reasons.

    Reply

    1. John Dyer wrote on February 9th, 2012 at 6:58 am:

      I’ve spoken with the IE team and all they will say is that they don’t comment on features unless they are complete. In the mean time they want folks to know that windows 8 is fullscreen by default in Metro mode.

      Reply

    2. Robert Nyman wrote on February 9th, 2012 at 12:16 pm:

      Thanks! I’ve heard the same: it’s not clear if Microsoft will do it in IE10 or not.

      Reply

  10. 40kg wrote on February 19th, 2012 at 11:16 pm:

    It dosn’t work when I embed a flash in the html.
    Could you please explain me why?
    Thanks a lot!!

    Reply

    1. Robert Nyman wrote on February 20th, 2012 at 1:43 am:

      In what way doesn’t it work? Nothing happens? Error message?
      And is this behavior consistent across web browsers with Fullscreen API support?

      Reply

  11. 40kg wrote on February 20th, 2012 at 6:16 pm:

    when I embed a flashgame into html like above, the fullscreen Api failed to work. If I set wmode=”opaque”, the API works while each time the browsers turn to fullscreen the flashgame restart unwanted…
    Could you please help me how to fullscreen browsers without restart flashgame? Thanks!O(∩_∩)O

    Reply

    1. Robert Nyman wrote on February 21st, 2012 at 12:20 am:

      I’m not sure what the connection would be. However, if this problem persists, please file a bug.

      Reply

  12. cpearce wrote on February 21st, 2012 at 1:58 pm:

    @40kg: In Firefox10 we deny requests for fullscreen mode when a windowed plugin is present in the page on all platforms except MacOSX. This is for security reasons. In Firefox11 we switch to exit fullscreen when a windowed plugin is focused instead. So HTML fullscreen + flash are never going to work well. Flash already has a fullscreen mode, can you use that instead?

    Reply

    1. Robert Nyman wrote on February 22nd, 2012 at 1:10 am:

      Interesting! Does that apply to just making the Flash fullscreen, or any element in a page that also contains Flash?

      Reply

    2. 40kg wrote on February 22nd, 2012 at 2:24 am:

      When I use flash’s fullscreen mode, the keyboard event is disabled as security reasons. While my flash game need keyboard to play, so I wish a fullscreen by browsers. Is there any way to enable keyboard in fullscreen mode?Thank you( ⊙ _ ⊙ ).

      Reply

      1. Robert Nyman wrote on February 22nd, 2012 at 2:26 am:

        This is covered ib the blog post under the title “Full screen with key input”.

        Reply

Add your comment

  1.