HTML <menu> Element

Publish in HTML Tutorial el 24/05/2025 17:24

The <menu> element represents a group of commands that a user can perform or activate. While similar to unordered lists (<ul>), the <menu> element is semantically intended for interactive items and can be used for context menus, toolbar commands, and other lists of user actions.


Key features of the <menu> element:

  • Semantically represents a menu of commands
  • Can be used for context menus (with JavaScript)
  • Works well with keyboard navigation
  • Supports different types of menu items


Basic Syntax

Here's the basic structure of a <menu> element:


Examples

1. Basic Menu with Buttons

A simple menu containing action buttons with Bootstrap styling:

Play Code


2. Context Menu (JavaScript Example)

A right-click context menu that appears at the cursor position:

Play Code


3. Toolbar Menu

A horizontal menu styled as a toolbar with Bootstrap:

Play Code


4. Nested Dropdown Menu (JavaScript Example)

A menu with nested submenus that appear on hover:

Play Code


5. Accessibility-Friendly Menu

A menu with proper ARIA attributes for screen readers:

Play Code


Tips and Tricks

  • Semantic Use: Reserve <menu> for interactive command lists, not for general navigation.
  • Keyboard Navigation: Ensure your menu can be navigated using Tab and arrow keys.
  • Mobile Friendly: Consider using <select> for mobile devices when space is limited.
  • ARIA Attributes: Use aria-haspopup and aria-expanded for dropdown menus.
  • Performance: For large menus, consider virtual scrolling techniques.


Browser Compatibility

The <menu> element is widely supported in modern browsers, but some older browsers may require polyfills for advanced features. Always test your implementation across different browsers.

También te puede interesar

HTML <style> Element
HTML <style> Element

The <style> HTML element contains style information for a document, or part of a document. ...

HTML <strong> Element
HTML <strong> Element

The <strong> element is used to indicate that its content has strong importance, seriousnes...

HTML <span> Element
HTML <span> Element

The <span> element is an inline container used to mark up a part of a text or document. Unl...

HTML <source> Element
HTML <source> Element

The <source> element is used to specify multiple media resources for media elements like &l...

HTML <small> Element
HTML <small> Element

The <small> element is used to represent side-comments and small print, typically for discl...

HTML <section> Element
HTML <section> Element

HTML <section> Element: The Complete Guide The <section> element is a semantic HTML ...