A lesser known feature of the HTML5 specification is context menus. These allow you to add custom items to the menu that appears when a user right-clicks on the page. Using JavaScript, you can then control the actions these items will perform when clicked.

In this blog post, you’re going to learn how to create your own context menus using the <menu> and <menuitem> elements.

Browser Support for HTML5 Context Menus

HTML5 context menus are currently only supported in Firefox (8+). This feature has been around for some time now, but no other browsers have implemented it. However, there is a polyfill available that will add support for context menus in other web browsers.

The menu and menuitem Elements

HTML5 context menus are constructed using two elements, <menu> and <menuitem>.

The <menu> element has a number of attributes that control how it will be displayed in the browser.

<menu type="context" id="menu">...</menu>

The type attribute is used to specify that we’re creating a context menu. This is important as the <menu> element can also be used for marking up other kinds of menus.

It’s important that you give your <menu> element an id, otherwise you won’t be able to use the menu on your page (more on this later).

The label attribute can be specified on nested <menu> elements to set the text that will be displayed in the main context menu.

Similarly the icon attribute can be used to specify the location of an icon image that will be displayed alongside the label text for nested menus.

Creating Menu Items

The <menuitem> element is used to create a new menu item. The label attribute should contain the text that you wish to display in the menu. You can also optionally specify an icon attribute with a path to an image to display alongside the label text.

The action a menu item will perform is defined using JavaScript. You can use the onclick attribute to add this JavaScript directly to the <menuitem> element.

Note: I’m not a huge fan of using the onclick attribute to define event handlers, but I wasn’t able to get this working using an unobtrusive JavaScript approach.

Assigning Context Menus

Once you’ve created your context menu, you need to assign the menu to an element on the page. Your menu will only be visible in the context menu if the user right-clicks on this particular element.

You can assign a menu by specifying the contextmenu attribute on the desired element and setting its value to the id of your <menu> element.

<div contextmenu="menu-id">...</div>

If you want your menu to appear regardless of where the user clicks on the page, you could assign the menu to the <body> tag.

Nested Menus

Nested Context Menus

You can nest <menu> elements within one another to create more complex menu structures. This can be particularly handy if you want to provide a large number of controls but don’t wish to clutter up the main context menu.

Nested <menu> elements should include a label attribute containing the text that will be displayed in the parent menu. You can also specify an icon attribute with a path to an image that will be displayed alongside the label.

Context Menu Demo

HTML5 Context Menus

Putting this all together, here is a demo of an example context menu that provides options to share the current page. Here, we’re just going to use some simple JavaScript that will redirect the user to the appropriate sharing URL when the menu item is clicked.

Final Thoughts

Whilst I can think of a number of legitimate uses for HTML5 context menus (in-game controls being one — pause, restart, etc.), I also worry that the power to invade the browser UI like this could be exploited. Take some time to think whether the controls you plan to provide through the context menu could not be delivered better through a different interface. We run the risk of polluting the context menu and confusing users if we don’t use this feature responsibly.

What are your thoughts about HTML5 context menus? Do you wish other browsers would hurry up and implement this feature, or are you worried that it will just be exploited? Let us know in the comments below.

From a usability point of view, who will actually know that you can right-click to access these menu items, you would need a space explaining this, which you could have populated with the required links in the first place.

As you mention, use within html5 games could work nicely, a bit like how you would interact with Flash (sorry only example I could think of)

Maybe also instead of links it could be used to place a short message, if right clicking on a copyrighted image, a kind reminder “not for personal use” etc?