Given an HTML document, The task is to position a <div> at specific coordinates on the web page using JavaScript. we’re going to discuss few techniques.
Approach:
- First setting the style.position property of the element.
- Then set the style.top, style.left properties of the element, which we want to position.
Example 1: In this example, the DIV is positioned at the end of the document.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Position a DIV in a specific coordinates. </title> <style> #GFG_DIV { background: green; height: 100px; width: 200px; margin: 0 auto; color: white; } </style> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 19px; font-weight: bold;"> </p> <div id="GFG_DIV"> This is Div box. </div> <br> <button onClick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color: green; font-size: 24px; font-weight: bold;"> </p> <script> var el_up = document.getElementById( "GFG_UP"); var el_down = document.getElementById( "GFG_DOWN"); el_up.innerHTML = "Click on button to change"+ " the position of the DIV."; function GFG_Fun() { var x = 370; var y = 250; var el = document.getElementById('GFG_DIV'); el.style.position = "absolute"; el.style.left = x + 'px'; el.style.top = y + 'px'; el_down.innerHTML = "Position of element is changed."; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: In this example, the DIV is positioned at the top-left corner of the document.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Position a DIV in a specific coordinates. </title> <style> #GFG_DIV { background: green; height: 50px; width: 80px; margin: 0 auto; color: white; } </style> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 19px; font-weight: bold;"> </p> <div id="GFG_DIV"> This is Div box. </div> <br> <button onClick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color: green; font-size: 24px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = "Click on button to change the position of the DIV."; function GFG_Fun() { var x = 0; var y = 0; var el = document.getElementById('GFG_DIV'); el.style.position = "absolute"; el.style.left = x + 'px'; el.style.top = y + 'px'; el_down.innerHTML = "Position of element is changed."; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to overlay one div over another div using CSS
- How to copy the content of a div into another div using jQuery ?
- How to center a div within another div?
- How to clear all div's content inside a parent div ?
- How to remove gutter space for a specific div in Bootstrap ?
- How to remove specific div element by using JavaScript?
- How to position a div at the bottom of its container using CSS?
- How to move mouse pointer to a specific position using JavaScript ?
- JavaScript | Coordinates of mouse
- HTML | DOM Geolocation coordinates Property
- How to get relative click coordinates on the target element using JQuery?
- How to get the coordinates of a mouse click on a canvas element ?
- Making a div vertically scrollable using CSS
- How to set the div height to auto-adjust to background size?
- Div Tag | HTML
- How to align content of a div to the bottom using CSS ?
- How to horizontally center a div using CSS?
- How to auto-resize an image to fit a div container using CSS?
- How to give a div tag 100% height of the browser window using CSS
- How to place two div side-by-side of the same height using CSS?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


