HTML contextmenu Attribute

The obsolete HTML contextmenu attribute was intended to associate an element with a custom context menu that could be displayed when the user opened the element's context menu.

Historical Syntax

The contextmenu attribute contained the id of a <menu> element that defined the custom context menu:

<div contextmenu="example-menu">
  Open the context menu on this content.
</div>

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

The value of contextmenu connected the element to the menu whose id matched that value.

Historical Elements

The contextmenu attribute was defined as a global attribute, meaning it could historically be associated with HTML elements generally rather than being limited to one particular element.

Element Historical Purpose
HTML elements Associated an element with a custom context menu identified by a corresponding <menu> element.

Historical Use

The contextmenu attribute was intended to let authors add commands to the context menu associated with particular webpage content. A user could open the context menu, commonly with a right-click or another platform-specific action, and access commands defined by the webpage.

The attribute itself did not contain the menu commands. Instead, it referenced a separate <menu> element by its id. The browser was responsible for presenting the custom menu when the context menu was requested.

This feature did not achieve consistent implementation across major browsers, making it unreliable for general web development.

Why It Became Obsolete

The contextmenu attribute and the related declarative context-menu mechanism did not gain sufficient cross-browser support. Web authors therefore could not depend on the feature working consistently for their visitors.

Modern webpages that require custom contextual commands generally implement their own interface behavior with JavaScript rather than relying on the obsolete HTML attribute.

Modern Alternative

There is no direct modern HTML replacement for the contextmenu attribute. In many cases, the best approach is to keep the browser's standard context menu and provide important page commands through visible HTML controls such as buttons or links.

<div>
  <p>Select an action for this item:</p>
  <button type="button">Edit</button>
  <button type="button">Duplicate</button>
  <button type="button">Delete</button>
</div>

Visible controls make functionality easier to discover and can be used without depending on a particular mouse or pointing-device action.

When an application genuinely requires a custom context menu, JavaScript can listen for the contextmenu event and display an application-defined interface. Such a menu should also provide appropriate keyboard access, focus management, and other accessibility support.

Because JavaScript would demonstrate a custom interface rather than a direct replacement for the obsolete attribute, a live editor example is not necessary for this page.

Legacy Browser Support

The contextmenu attribute had limited historical browser support and was never a dependable cross-browser method for creating custom context menus.

Modern browsers should not be expected to support the obsolete declarative context-menu mechanism. Existing pages that depend on it should provide their commands through current HTML and, when necessary, JavaScript.

For additional information about historical browser compatibility, visit Can I Use? . Search for the HTML element or attribute you want to check. Search results can include related HTML features, element attributes, APIs, and other technologies, so select the result that most closely matches the legacy feature you are researching.

Other obsolete HTML attributes associated with older interactive or event-related features include:

  • onshow - Historically associated with an event fired when a context menu was shown.
  • event - Historically associated with an older mechanism for binding scripts to events.

Specifications

The contextmenu attribute is obsolete and should not be used in modern HTML. See the WHATWG HTML Living Standard for information about obsolete and non-conforming features.