How to Add a Custom Right-Click Menu to a Webpage? Last Updated : 25 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Adding a custom right-click menu to a webpage can enhance user interaction by providing tailored options that improve functionality and user experience. This technique involves overriding the default browser context menu with a custom-designed menu, which can be achieved using JavaScript and CSS. Implementing a custom right-click menu allows you to offer specific actions and features relevant to your website's context, making it a powerful tool for creating more engaging and intuitive web applications.ApproachCreate the HTML structure: Define the custom context menu as a hidden <div> with a list of menu items.Style the menu using CSS: Set the position, appearance, and hover effects for the context menu.Handle right-click events with JavaScript:Override the default context menu with document.oncontextmenu.Display the custom menu at the cursor position.Hide the menu when clicking elsewhere using document.onclick.Example: This example shows the implementation of the above-explained approach. HTML <!DOCTYPE html> <html> <head> <style type="text/css"> .context-menu { position: absolute; text-align: center; background: lightgray; border: 1px solid black; } .context-menu ul { padding: 0px; margin: 0px; min-width: 150px; list-style: none; } .context-menu ul li { padding-bottom: 7px; padding-top: 7px; border: 1px solid black; } .context-menu ul li a { text-decoration: none; color: black; } .context-menu ul li:hover { background: darkgray; } </style> </head> <body> <h1 style="text-align: center;"> Welcome to GeeksforGeeks. </h1> <h1 style="text-align: center;"> Hi, We are creating a custom context menu here. </h1> <div id="contextMenu" class="context-menu" style="display:none"> <ul> <li><a href="#">Element-1</a></li> <li><a href="#">Element-2</a></li> <li><a href="#">Element-3</a></li> <li><a href="#">Element-4</a></li> <li><a href="#">Element-5</a></li> <li><a href="#">Element-6</a></li> <li><a href="#">Element-7</a></li> </ul> </div> <script> document.onclick = hideMenu; document.oncontextmenu = rightClick; function hideMenu() { document.getElementById( "contextMenu").style.display = "none" } function rightClick(e) { e.preventDefault(); if (document.getElementById( "contextMenu").style.display == "block") hideMenu(); else { let menu = document .getElementById("contextMenu") menu.style.display = 'block'; menu.style.left = e.pageX + "px"; menu.style.top = e.pageY + "px"; } } </script> </body> </html> Output:This was a basic context menu that we created. You can do much more by adding some cool hover effects, shadow effects, coloring, borders, etc. Comment More infoAdvertise with us Next Article How to disable right-click option using the jQuery ? D devansh07 Follow Improve Article Tags : JavaScript CSS-Misc HTML-Misc JavaScript-Misc HTML-Questions +1 More Similar Reads How to design menu on right click of webpage using jQuery EasyUI ? EasyUI is a HTML5 framework for using user interface components based on jQuery, React, Angular and, Vue technologies. It helps in building features for interactive web and mobile applications saving a lot of time for developers. It helps in building features for interactive web and mobile applicati 3 min read How to add dbclick() on right click in jQuery? The jQuery dblclick() method is used to detect whenever a particular element is double-clicked. This method only detects left mouse button double clicks and not the right mouse button double clicks. In this article, we will see how to simulate the double right mouse click in jQuery. Approach: There 3 min read How to disable right-click option using the jQuery ? The bind() method in jQuery is used to attach one or more event handlers for selected element and this method specifies a function to run when an event occurs. Syntax: $(selector).bind(event, data, function);Parameters: This method accepts three parameters as mentioned above and described below: eve 2 min read How to disable right click on web page using JavaScript ? Disabling right-click on a web page can be done using the DOM Events. It is a client-side action, and it can be achieved using JavaScript. However, it's important to note that attempting to prevent right-clicking is not a foolproof method for protecting your content, as users can easily bypass it by 2 min read How to show some custom menu on text selection? It would be great if selecting text on a webpage showed some options like copy, paste, or share. A selection menu accomplishes this. Basically, it is a GUI that displays in response to user activity, such as text selection. A selection menu provides a different range of options that you can access j 4 min read How to create a mega menu using HTML and CSS ? HTML is a markup language used to create web pages and CSS is a stylesheet language used to design a document written in a markup language such as HTML. In this article, we are going to know how can we create a mega menu on our web page with the help of only HTML and CSS. Mega menus are a type of ex 4 min read Like