
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM Element offsetLeft Property
The HTML DOM Element offsetLeft property is used to retrieve the horizontal distance in pixels from the left edge of an element to the left edge of its nearest positioned ancestor element.
The returned distance value includes the element margin, the parent container left padding, the scrollbar (if present), and the border.
Following is the list of similar offset properties:
Syntax
Following is the syntax of the HTML DOM Element offsetLeft property −
element.offsetLeft
Parameters
Since this is a property, it will not accept any parameter.
Return Value
This property returns the total distance in pixels from the left edge of an element to its nearest positioned ancestor left edge.
Example 1: Retrieving the offsetTop Property Value
The following program demonstrates the usage of the HTML DOM Element offsetLeft property. It displays the position from the left edge of the div element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element offsetLeft</title> <style> #ex { position: relative; left: 50px; padding: 10px; width: 200px; height: 100px; background-color: green; color: white; } </style> </head> <body> <h3>HTML DOM Element offsetHeight Property</h3> <div id="ex">Example Element</div> <br> <button onclick="showvalt()">Show OffsetLeft</button> <p id="od">Offset Left: <span id="olv"></span></p> <script> const ex = document.getElementById('ex'); const oldis = document.getElementById('olv'); function showvalt() { const olv = ex.offsetLeft; oldis.textContent = `${olv}px`; } </script> </body> </html>
The above program displays the left offset of the div element.
Example 2: Retrieving Horizontal Offset Using offsetLeft
Here is another example of the HTML DOM Element offsetLeft property. We use this property to get the horizontal position of an element, including the margin, padding, and parent container's border −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element offsetLeft</title> <style> #pc { width: 300px; height: 200px; border: 5px solid #ccc; padding: 20px; } #ex { left: 50px; width: 200px; padding: 10px; height: 100px; background-color: lightblue; } </style> </head> <body> <h3>HTML DOM Element offsetLeft Property</h3> <div id="pc"> <div id="ex">Example Element</div> </div> <br> <button onclick="sol()">Show OffsetLeft</button> <script> const ex = document.getElementById('ex'); function sol() { const setval = ex.offsetLeft; alert(`Offset Left including margin, padding, scrollbar, and parent container's border: ${setval} px`); } </script> </body> </html>
Clicking the displayed button will alert the left offset of an element, including pixels, margin, and border.
Example 3: Using offsetLeft in an Event Handler
In the example below, we use this property with an event handler to change the element position and update the offsetLeft value accordingly −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element offsetLeft</title> <style> #ex { position: relative; left: 50px; width: 200px; height: 100px; background-color: lightblue; transition: left 0.3s ease; padding: 10px; } #ofdis { border: 1px solid black; padding: 10px; } </style> </head> <body> <h3>HTML DOM Element offsetLeft Property</h3> <div id="ex">Example Element</div> <div id="ofdis">Offset Left: <span id="oflt"></span> pixels </div> <br> <button id="mB" onclick="ml()">Move Element</button> <script> const ex = document.getElementById('ex'); const od = document.getElementById('oflt'); const mB = document.getElementById('mB'); function updatelt() { od.textContent = ex.offsetLeft; } function ml() { ex.style.left = '150px'; // Update offsetLeft value after movement updatelt(); mB.textContent = 'Calculate offsetLeft'; mB.setAttribute('onclick', 'cakculateleft()'); } function cakculateleft() { // Update offsetLeft value updatelt(); } //display of offsetLeft updatelt(); </script> </body> </html>
The program displayed a button when it clicked, an element moved to the left side. Clicking the button again calculates the left offset of the moved element.
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
offsetLeft | Yes | Yes | Yes | Yes | Yes |