contextmenu
The historical contextmenu attribute was intended to attach a custom menu to an element's context-menu action, usually a right-click. It is obsolete and not reliably supported.
What you will learn
- What the
contextmenuattribute was designed to do - Why it should not be used in new projects
- How to design accessible alternatives that also work from a keyboard
Historical syntax
<!-- Historical example: do not use for new work. -->
<div contextmenu="file-menu">Document</div>
<menu id="file-menu" type="context">...</menu>
Browsers do not consistently implement this feature, so a page cannot depend on it to provide a menu. It can also conflict with the browser's own context menu and with touch or keyboard interaction.
Prefer explicit controls
For actions such as Rename or Delete, provide a visible button and show a menu from that button. A native button gives users a predictable focus target and can be operated without a mouse.
- Use
buttonfor the action that opens a menu. - Keep menu items keyboard reachable and clearly labeled.
- Do not remove the browser's context menu unless there is a compelling, accessible reason.