Hide the Cursor in a Webpage using CSS and JavaScript
Last Updated :
17 Oct, 2024
Hiding the cursor on a webpage means making the mouse pointer invisible while interacting with certain elements. This can be achieved using CSS and JavaScript. In CSS, you can use cursor: none; and in JavaScript, dynamically manipulate the element's style for more control.
Hiding the Cursor Using cursor Property
In this approach, CSS hides the cursor with the cursor: none;. JavaScript dynamically adds this class to specific elements (like a div or body). When the button is clicked, it triggers cursor invisibility and updates a message.
Examples of Hiding the Cursor from Webpage
Example 1: Here we hides the cursor over a div element using CSS (cursor: none;). When a button is clicked, JavaScript adds the class to hide the cursor and updates a message.
html
<!DOCTYPE html>
<html lang="en">
<head>
<title>
Hide the cursor in a webpage
using CSS and JavaScript
</title>
<style>
body {
text-align: center;
}
#GFG_DIV {
background: green;
height: 100px;
width: 200px;
margin: auto;
color: white;
}
/* CSS style to hide cursor element */
.newClass {
cursor: none;
}
</style>
</head>
<body>
<h1 style="color:green;">
GeeksforGeeks
</h1>
<h3>
Click on Button to Hide the Cursor from DIV
</h3>
<div id="GFG_DIV"></div>
<br>
<button onClick="GFG_Fun()">
Click Here
</button>
<p id="GFG"></p>
<!-- Script to hide cursor element -->
<script>
let elm = document.getElementById('GFG');
let div = document.getElementById('GFG_DIV');
/* Function to add class name to hide
cursor element */
function GFG_Fun() {
div.classList.add("newClass");
down.innerHTML = "Cursor is removed from DIV!";
}
</script>
</body>
</html>
Output:
hides the cursor over a div elementExample 2: Here we hides the cursor across the entire webpage by adding the cursor: none; class to the body when a button is clicked. A message updates to confirm the action.
html
<!DOCTYPE html>
<html lang="en">
<head>
<title>
Hide the cursor in a webpage
using CSS and JavaScript
</title>
<style>
body {
text-align: center;
}
#GFG_DIV {
background: green;
height: 100px;
width: 200px;
margin: 0 auto;
color: white;
}
/* CSS style to hide cursor element */
.newClass {
cursor: none;
}
</style>
</head>
<body id="body">
<h1 style="color:green;">
GeeksforGeeks
</h1>
<h3>
Click on Button to Hide Cursor from Body
</h3>
<div id="GFG_DIV"></div>
<br>
<button onClick="GFG_Fun()">
Click Here
</button>
<p id="GFG"></p>
<!-- Script to hide cursor element -->
<script>
let elm = document.getElementById('GFG');
let body = document.getElementById('body');
/* Function to add class name to
hide cursor element */
function GFG_Fun() {
body.classList.add("newClass");
elm.innerHTML = "Cursor is removed from body!";
}
</script>
</body>
</html>
Output:
hides the cursor across the entire webpage
Explore
JavaScript Basics
Array & String
Function & Object
OOP
Asynchronous JavaScript
Exception Handling
DOM
Advanced Topics