Monday, February 18, 2019

How to custom a create context menu in html, css and javascript

This tutorial explains how to custom a create context menu in html, css and javascript. A context menu is a menu in a GUI that appears upon user interaction, such as a right-click mouse operation. A context menu offers a limited set of choices that are available in the current state, or context, of the operating system or application. Usually the available choices are actions related to the selected object.

On your computer, right-clicking on the desktop launches your OS’s native context menu. From here, you can probably create a new folder, get some information, as well as other actions. When using a web browser, right-clicking on the page would launch that browser’s default context menu. You’ll be able to get page information, or view the source. Right-clicking on an image presents its own set of options too — you can save an image, open it in a new tab, copy it and many other option you can add in web browser based application.

Lets see the below complete source code, which display the context menu when user perform right click on browser window.



<title> How to create context menu in html, css and javascript </title>


<div class="menu">
  <ul class="menu-options">
    <li class="menu-option">Back</li>
    <li class="menu-option">Reload</li>
    <li class="menu-option">Save</li>
    <li class="menu-option">Save As</li>
    <li class="menu-option">Delete</li>
 <li class="menu-option">Spy</li>

<h3 class="center">Right click to view custom context menu</h3>


body {
  font-family: "Roboto", san-serif;
.center {
  text-align: center;
.menu {
  width: 120px;
  z-index: 1;
  box-shadow: 0 4px 5px 3px rgba(0, 0, 0, 0.2);
  position: fixed;
  display: none;
  transition: 0.2s display ease-in;
.menu .menu-options {
  list-style: none;
  padding: 10px 0;
  z-index: 1;
.menu .menu-options .menu-option {
  font-weight: 500;
  z-index: 1;
  font-size: 14px;
  padding: 10px 40px 10px 20px;
  cursor: pointer;
.menu .menu-options .menu-option:hover {
  background: rgba(0, 0, 0, 0.2);
button {
  background: grey;
  border: none;
button .next {
  color: green;
button[disabled="false"]:hover .next {
  color: red;
  animation: move 0.5s;
  animation-iteration-count: 2;
@keyframes move {
  from {
    transform: translate(0%);
  50% {
    transform: translate(-40%);
  to {
    transform: transform(0%);


const menu = document.querySelector(".menu");
let menuVisible = false;

const toggleMenu = command => { = command === "show" ? "block" : "none";
  menuVisible = !menuVisible;

const setPosition = ({ top, left }) => { = `${left}px`; = `${top}px`;

window.addEventListener("click", e => {

window.addEventListener("contextmenu", e => {
  const origin = {
    left: e.pageX,
    top: e.pageY
  return false;




How to custom a create context menu in html, css and javascript
Demo Link :

This is all about context menu in html, css and javascriptThank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.

1 comment:

  1. I thank you so much!!! Works absolutely fine!