How to Use HTML5 Fullscreen Mode

There have been a lot of articles and full pages of documentation written about the HTML5 fullscreen API. This allows developers to control bits of content and force the page into a fullscreen view. Granted this can only be supported in newer browsers where HTML5 is commonplace. But thankfully that is a majority of the current market, and it is growing rapidly every month.

In this HTML5 fullscreen mode tutorial I want to present a brief demonstration of how we can utilize the fullscreen effects. Typically this will require JavaScript as a means to access the framework. HTML5 can support displaying a webpage element in fullscreen but we need a way to access the API. Since we want to save a bit of development time I will be using the jQuery FullScreen plugin which is a free open source project for accessing the HTML5 fullscreen mode.

html5 jquery plugin fullscreen open source tutorial demo

Live DemoDownload Source Code

Getting Started

First we need to create a basic HTML5 template for the page. I will be including a copy of the jQuery library hosted on Google’s servers. Aside from the typical styles.css stylesheet I linked to a copy of the Autour One webfont, also hosted by Google. Then the last thing we need is a copy of the jQuery FullScreen plugin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!doctype html>
<html lang="en-US">
<head>
  <meta charset="utf-8">
  <meta http-equiv="Content-Type" content="text/html">
  <title>HTML5 Fullscreen Live Demo</title>
  <meta name="author" content="Jake Rocheleau">
  <link rel="shortcut icon" href="http://images.templatemonster.com/images/favicon.ico">
  <link rel="icon" href="http://images.templatemonster.com/images/favicon.ico">
  <link rel="stylesheet" type="text/css" media="all" href="styles.css">
  <link rel="stylesheet" type="text/css" media="all" href="http://fonts.googleapis.com/css?family=Autour+One">
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script type="text/javascript" src="jquery.fullscreen.js"></script>
</head>

You can download the necessary fullscreen JS file off the official Github page. This is updated with bug fixes and updates whenever the developer pushes out a new version. Currently it supports all HTML5-compliant browsers including Chrome, Safari, and Firefox 10+. We just need to add this script into the header along with jQuery and it’s good to go.

I will outline some of the common document elements and then show how we can use this plugin to call a fullscreen function on the page wrapper. Similarly there is a callback property for handling a new function call on each screen change event. This means we can update practically anything on the page using the proper logic.

Internal Page Content

Inside the page wrapper div I have added a small button with the ID #fullbtn. This will initially display by itself along with some heading text. Naturally when you click this button an event listener will trigger the fullscreen display. All of this is handled through jQuery, so I have left out any external HREF value so this link will not go anywhere.

1
2
3
4
5
6
7
 <div id="w">
    <h1>HTML5 Fullscreen Basic Demo</h1>
 
    <p><center><a href="javascript:void(0)" id="fullbtn" class="btn">View Fullscreen</a></center></p>
 
    <div class="onlyfull"><center><strong>Look a special image!</strong><br><br><img src="http://www.google.com/logos/2011/robertburns11-hp.jpg" alt="magic image"></center></div>
  </div>

Inside the .onlyfull div we find a bit of text with a container image. This will only become visible after your first time entering fullscreen mode. Then once you exit it will stay visible until the page is refreshed. I want to show how we can manipulate many various page elements to really customize how the fullscreen mode changes vs typical webpage views.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
.btn {
	position: relative;
	width:220px;
	height:40px;
	text-align:center;
	color:#FFF;
	text-decoration:none;
	line-height: 43px;
	font-size: 16px;
	font-family: Helvetica, Arial, sans-serif;
	display: block;
	margin: 30px;
	text-shadow:-1px -1px 0 #2C7982;
	background: #3EACBA;
	border:1px solid #379AA4;
	-webkit-border-radius:5px;
	-moz-border-radius:5px;
	border-radius:5px;
	background-image:-webkit-linear-gradient(top, #48C6D4, #3EACBA);
	background-image:-moz-linear-gradient(top, #48C6D4, #3EACBA);
	background-image:-ms-linear-gradient(top, #48C6D4, #3EACBA);
	background-image:-o-linear-gradient(top, #48C6D4, #3EACBA);
	background-image:linear-gradient(top, #48C6D4, #3EACBA);
	-webkit-box-shadow:0 1px 0 rgba(255, 255, 255, .5) inset, 0 -1px 0 rgba(255, 255, 255, .1) inset, 0 4px 0 #338A94, 0 4px 2px rgba(0, 0, 0, .5);
	-moz-box-shadow:0 1px 0 rgba(255, 255, 255, .5) inset, 0 -1px 0 rgba(255, 255, 255, .1) inset, 0 4px 0 #338A94, 0 4px 2px rgba(0, 0, 0, .5);
	box-shadow:0 1px 0 rgba(255, 255, 255, .5) inset, 0 -1px 0 rgba(255, 255, 255, .1) inset, 0 4px 0 #338A94, 0 4px 2px rgba(0, 0, 0, .5);
}
.btn:hover {
	background: #48C6D4;
	background-image:-webkit-linear-gradient(top, #3EACBA, #48C6D4);
	background-image:-moz-linear-gradient(top, #3EACBA, #48C6D4);
	background-image:-ms-linear-gradient(top, #3EACBA, #48C6D4);
	background-image:-o-linear-gradient(top, #3EACBA, #48C6D4);
	background-image:linear-gradient(top, #3EACBA, #48C6D4);
	color: #fff;
}
.btn:before {
	background:#f0f0f0;
	background-image:-webkit-gradient(linear, 0% 0%, 0% 100%, from(#D0D0D0), to(#f0f0f0));
	
	-webkit-border-radius:5px;
	-moz-border-radius:5px;
	border-radius:5px;
 
	-webkit-box-shadow:0 1px 2px rgba(0, 0, 0, .5) inset, 0 1px 0 #FFF; 
	-moz-box-shadow:0 1px 2px rgba(0, 0, 0, .5) inset, 0 1px 0 #FFF; 
	box-shadow:0 1px 2px rgba(0, 0, 0, .5) inset, 0 1px 0 #FFF;
	position: absolute;
	content: "";
	left: -6px; right: -6px;
	top: -6px; bottom: -10px;
	z-index: -1;
}
 
.btn:active {
	-webkit-box-shadow:0 1px 0 rgba(255, 255, 255, .5) inset, 0 -1px 0 rgba(255, 255, 255, .1) inset;
	top:5px;
}
.btn:active:before{
	top: -11px;
	bottom: -5px;
	content: "";
}

The code above is my snippet for the CSS3 button style. You can find a copy on this CSS Deck project page which also includes the source code. Now aside from these styles everything else is fairly typical. You can overwrite settings based on the .fullScreen class which I have done on the wrapper container. But otherwise everything else can be managed from within JS code.

Working with jQuery

Typically the HTML5 API works by running over normal JavaScript functions. You can naturally start a fullscreen API access call by using the requestFullScreen() method. But this process is verbose, somewhat confusing, and ultimately unnecessary when working on top of a library such as jQuery. The following code is located at the bottom of the page before my closing </body> tag.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$(function(){
  $('#fullbtn').click(function(){
    $('#w').fullScreen({
      'callback': function(fullScreen){
	  $(this).css('background', '#363a3c');
	  $('div.onlyfull').css('display', 'block');
          if ( !fullScreen ) {
	    // when exiting fullscreen set a new BG color
	    $('body').css('background', '#506b9d');
          }
      }
    });
    return false;
  });
});

This block is all fairly easy to understand once you have worked with the plugin for a while. Initially we are placing a click event handler onto #fullbtn which triggers .fullScreen(). This will force all the wrapped page elements into a fullscreen window, then it will apply the extra class .fullScreen onto the wrapper div. In my CSS stylesheet I can update the position of the content section only when in fullscreen mode:

1
2
3
4
#w.fullScreen  {
  margin-top: 150px;
  padding: 90px 40px;
}

Now the callback parameter can be used any time you run this function. The easiest method is to run another function passing in the fullScreen object. You can see inside I am running the jquery .css() method on a couple page elements. This will force the background to be much darker when in fullscreen while also forcing a hidden image to display.

The logic statement if (!fullScreen) checks against this parameter to see if we are currently in fullscreen or not. If no then I am re-updating the background color to be a darker shade of blue. Realistically this does not provide a lot of usage, however it does offer a blueprint you can follow in any real-world example for a similar detailed callback function.

html5 jquery plugin fullscreen open source tutorial demo

Live DemoDownload Source Code

Final Thoughts

I do hope this HTML5 fullscreen mode tutorial can demonstrate a solid introduction for developers new to the HTML5 trends. Fullscreen properties are much more useful when concerned with image galleries or video players. Over time we may witness a greater change to these technologies which then allows for more support & newer features. Website user interfaces can start applying fullscreen mode into common editing pages or web applications. Feel free to download a sample copy of my source code and toy with these features in your own projects.

Comments are closed.