Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to disable right-click option using the jQuery ?
Next article icon

How to Add a Custom Right-Click Menu to a Webpage?

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
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.

Approach

  • Create 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:

https://media.geeksforgeeks.org/wp-content/uploads/20240724234456/ezgifcom-gif-to-mp4-converter(24).mp4

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.



Next Article
How to disable right-click option using the jQuery ?
author
devansh07
Improve
Article Tags :
  • CSS
  • HTML
  • JavaScript
  • Web Technologies
  • CSS-Misc
  • HTML-Misc
  • HTML-Questions
  • JavaScript-Misc

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 toggle background color using right click in jQuery ?
    In this article, we will learn to toggle the background color using the right-click in jQuery. Approach 1: The approach is by using the contextmenu event. The contextmenu() method is used to bind the contextmenu event to the element being used. This can be used to perform the color toggling action o
    4 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 create Right Aligned Menu Links using HTML and CSS ?
    The right-aligned menu links are used on many websites. Like hotels website that contains lots of options in the menu section but in case of emergency to make contact with them need specific attention. In that case, you can put all the menu options on the left side of the navigation bar and display
    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 Add Link to HTML Button?
    A link in HTML connects one webpage to another, allow smooth navigation. In HTML , links are created using the <a> tag. The href attribute within the tag specifies the URL or path to the destination. We are given an HTML button, to add a link to the HTML button, we can wrap the button inside t
    3 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 add WhatsApp share button in a website using JavaScript ?
    WhatsApp is the most popular messaging app. This article describes how you can add the WhatsApp share button to your website. In this example we make a simple website that uses a Whatsapp share button when you click on it will open on the Whatsapp app on your system and your message was already prin
    2 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences