Get Mouse Position using HTML, CSS & JavaScript

Get Mouse Position using HTML, CSS & JavaScript

Get Mouse Position using HTML, CSS & JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Get Mouse Position which will give the mouse position to the user in the (x,y) format. This project will is good for beginners and help them to build their front-end development skills. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Get Mouse Position Project.

Project Description

Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Get Mouse Position Project.

Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Get Mouse Position Project.

Step 3
At last we will use JS (JavaScript) which will add a logic to make the Get Mouse Position Project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for Get Mouse Position

First we’ll start with creating the structure of the Get Mouse Position project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Get Mouse Position</title>
    <!-- Google Font -->
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div id="output">Move the mouse to get the coordinates</div>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>

Feedback Form Using HTML Code Only

CSS Code for Get Mouse Position

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Get Mouse Position project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now lets code the JavaScript part to make responsive.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body {
  height: 100vh;
  background: linear-gradient(#ab9dff 50%, #ffffff 50%);
}
#output {
  background-color: #ffffff;
  width: 80vmin;
  max-width: 500px;
  padding: 50px 30px;
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  text-align: center;
  border-radius: 8px;
  box-shadow: 0 20px 50px rgba(4, 1, 22, 0.12);
  font-size: 2.2em;
}
#output div {
  margin: 16px 0;
}
span {
  color: #7861f8;
}

JavaScript Code for Get Mouse Position

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. Let us see the Final Output of the project Get Mouse Position using HTML, CSS & JavaScript (Source Code).

let output = document.getElementById("output");
window.addEventListener("mousemove", (e) => {
  let xPos = e.clientX;
  let yPos = e.clientY;
  output.innerHTML = `<div><span>X: </span>${xPos}px</div><div><span>Y: </span>${yPos}px</div>`;
});

50+ HTML, CSS & JavaScript Projects With Source Code

Final Output


We have Successfully created our Get Mouse Position using HTML, CSS & JavaScript . You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply