
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
Retrieve the position (X,Y) of an HTML element
The X, Y position of an HTML document represents X as horizontal whereas Y vertical position. Here, we will see two examples to get the position i.e.
- Get the position of a specific text on a web page.
- Get the position of buttons on a web page.
Get the position of a specific text on a web page
To get the position of a specific text on a web page, we will use the getBoundingClientRect() method. It provides information about the size of an element and its position relative to the viewport. This will give us the X and Y coordinates on mouseover −
var rect = element.getBoundingClientRect(); document.getElementById('text').innerHTML = 'X Coordinate= ' + rect.x + '<br>' + 'Y Coordinate = ' + rect.y;
The values are displayed in the div on mouseover −
<div onmouseover="getPositionXY(this)"> X,Y Position = <p id='text'></p> </div>
Example
Let us now see the example −
<!DOCTYPE html> <html> <head> <title>Example X,Y Coordinate</title> <script> function getPositionXY(element) { var rect = element.getBoundingClientRect(); document.getElementById('text').innerHTML = 'X Coordinate= ' + rect.x + '<br>' + 'Y Coordinate = ' + rect.y; } </script> </head> <body> <h1>Position</h1> <p>To get the position, move the mouse cursor over the below text.</p> <div onmouseover="getPositionXY(this)"> X,Y Position = <p id='text'></p> </div> </body> </html>
Output

On keeping the cursor on the text, the X, Y coordinates will be visible −

Get the position of buttons on a web page
In this example, we will get the position of buttons on a web page. To get the position, we will use the getBoundingClientRect() method. It provides information about the size of an element and its position relative to the viewport. This will give us the X and Y coordinates on click −
function getPositionXY(element) { var rect = element.getBoundingClientRect(); document.getElementById('demo').innerHTML = 'X Coordinates = ' + rect.x + ', ' + 'Y Coordinates = ' + rect.y }
The values are displayed in the div on mouseover −
<p>Get the Coordinates of any of the button positioned below:</p> <button id='button1' onclick="getPositionXY(this)">Button 1</button> <button id='button1' onclick="getPositionXY(this)">Button 2</button> <button id='button1' onclick="getPositionXY(this)">Button 3</button> <button id='button1' onclick="getPositionXY(this)">Button 4</button> <br><br> <button id='button1' onclick="getPositionXY(this)">Button 5</button> <button id='button1' onclick="getPositionXY(this)">Button 6</button> <button id='button1' onclick="getPositionXY(this)">Button 7</button> <button id='button1' onclick="getPositionXY(this)">Button 8</button>
Example
Let us now see the example −
<!DOCTYPE html> <html> <head> <title>Coordinates of Buttons</title> <script> function getPositionXY(element) { var rect = element.getBoundingClientRect(); document.getElementById('demo').innerHTML = 'X Coordinates = ' + rect.x + ', ' + 'Y Coordinates = ' + rect.y } </script> </head> <body> <h1>Coordinates</h1> <p>Get the Coordinates of any of the button positioned below:</p> <button id='button1' onclick="getPositionXY(this)">Button 1</button> <button id='button1' onclick="getPositionXY(this)">Button 2</button> <button id='button1' onclick="getPositionXY(this)">Button 3</button> <button id='button1' onclick="getPositionXY(this)">Button 4</button> <br><br> <button id='button1' onclick="getPositionXY(this)">Button 5</button> <button id='button1' onclick="getPositionXY(this)">Button 6</button> <button id='button1' onclick="getPositionXY(this)">Button 7</button> <button id='button1' onclick="getPositionXY(this)">Button 8</button> <p id='demo'></p> </body> </html>
Output

Click on any of the Button to get the position −

- Related Articles
- Retrieve the position in an Array in MongoDB?
- Position your HTML element with CSS
- Retrieve an element from ArrayList in Java
- An element X has a valency of 4 whereas another element Y has a valency of 1. What will be the formula of the compound formed between X and Y?
- How get the (x,y) position pointing with mouse in an interactive plot (Python Matplotlib)?
- An element X of valency 3 combines with another element Y of valency 2. What will be the formula of the compound formed?
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- How to set the top position of an element with JavaScript?
- Retrieve only the queried element in an object array in MongoDB collection?
- Adding an element at a given position of the array in Javascript
- Removing an element from a given position of the array in Javascript
- Count of pairs (x, y) in an array such that x < y in C++
- Find the sum of the following arithmetic progressions:\( \frac{x-y}{x+y}, \frac{3 x-2 y}{x+y}, \frac{5 x-3 y}{x+y}, \ldots \) to \( n \) terms
- An element X (atomic number 17) reacts with an element Y (atomic number 20) to form a compound.(a) Write the position of these elements in the modern periodic table.(b) Write the formula of the compound formed. Justify your answer in each case.
- An element X of atomic number 12 combines with an element Y of atomic number 17 to form a compound XY2. State the nature of chemical bond in XY2 and show how the electron configurations of X and Y change in the formation of this compound.
