How to Build a Custom Right-Click Context Menu in CSS & JavaScript
To build a custom right-click context menu, intercept the contextmenu event with e.preventDefault(), position the menu using transform: translate(x, y) based on e.clientX and e.clientY, and clamp coordinates so the menu never overflows viewport edges. Style the menu with modern frosted glass and elevation shadows. For accessibility, listen for the Escape key to close the menu and return focus to the trigger element.
Step 1: Intercepting the Native contextmenu Event
To display your custom menu instead of the default browser context menu, attach an event listener to the window or target element and call e.preventDefault().
const contextMenu = document.getElementById('custom-context-menu');
window.addEventListener('contextmenu', (e) => {
e.preventDefault();
// Position menu based on mouse cursor coordinates
const { clientX: x, clientY: y } = e;
contextMenu.style.transform = `translate(${x}px, ${y}px)`;
contextMenu.classList.add('visible');
});
Step 2: Viewport Boundary Clamping: Preventing Off-Screen Clipping
If a user right-clicks near the bottom-right corner of their screen, a standard offset places the menu outside the viewport. Clamp coordinates using window.innerWidth and window.innerHeight to flip the menu upward and leftward when space runs out.
function positionMenu(x, y, menu) {
const menuWidth = menu.offsetWidth;
const menuHeight = menu.offsetHeight;
const padding = 8;
// Clamp X coordinate
const clampedX = (x + menuWidth > window.innerWidth)
? x - menuWidth - padding
: x + padding;
// Clamp Y coordinate
const clampedY = (y + menuHeight > window.innerHeight)
? y - menuHeight - padding
: y + padding;
menu.style.transform = `translate(${clampedX}px, ${clampedY}px)`;
}
Step 3: Glassmorphism & Elevation Styling in Pure CSS
A custom menu must look elevated above all page content. Use Level 4 or Level 5 elevation box-shadows, subtle border highlights, and backdrop blur.
.context-menu {
position: fixed;
top: 0;
left: 0;
z-index: 10000;
background: rgba(22, 19, 36, 0.85);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 8px;
box-shadow: 0 12px 32px rgba(0, 0, 0, 0.6);
opacity: 0;
pointer-events: none;
transition: opacity 0.15s ease, transform 0.1s ease;
}
.context-menu.visible {
opacity: 1;
pointer-events: auto;
}
Step 4: Keyboard Accessibility & Escape Key Dismissal
Accessible web applications must dismiss open context menus when the user clicks anywhere outside or presses the Escape key, returning keyboard focus to the triggering element.
// Dismiss on outside click or Escape key
window.addEventListener('click', () => contextMenu.classList.remove('visible'));
window.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
contextMenu.classList.remove('visible');
}
});
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
A trick I use in almost every project: don't fight specificity battles with !important. Leverage modern CSS cascade layers (@layer) and CSS variables as design tokens—it makes dark/light theme switching and responsive tuning effortless.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
How do I prevent the default browser right-click menu from appearing?
Attach an event listener to the contextmenu event and call e.preventDefault() inside the event handler.
How do I prevent a custom context menu from clipping off the screen?
Compare the mouse coordinates plus the menu’s width and height against window.innerWidth and window.innerHeight. If it exceeds the screen boundary, flip the menu so it renders on the opposite side of the cursor.
What is the best way to close the context menu when clicking outside?
Add a click event listener on the global window object that removes the active/visible class from the context menu container.
How can keyboard users open custom context menus accessibly?
Listen for the standard keyboard shortcut Shift + F10 or the physical Menu key on keyboards, and position the context menu at the focused element’s bounding coordinates.
Should custom context menus be used on regular informational websites?
Custom context menus are generally reserved for rich web applications (code editors, spreadsheets, canvas designers). Replacing the browser’s native right-click menu on regular articles can frustrate users trying to copy text or open links.