
- 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 Style Object backgroundPosition Property
HTML DOM Style Object backgroundPosition property sets or returns the position of background image of an element.
Syntax
Given below are the syntax to get or set the backgroundPosition property.
Set the backgroundPosition property:object.style.backgroundPosition = value;Get the backgroundPosition property:
object.style.backgroundPosition;
Property Values
Value | Description |
---|---|
top left top center top right center left center center center right bottom left bottom center bottom right |
It specifies the position of background image using keywords. If only one value is specified, by default other value is 'center'. |
x% y% | It specifies the position of background image using percentage where x represents horizontal positioning while y represents vertical positioning. 0% 0% represents top left corner while 100% 100% represents bottom right corner. When only one value is specified, by default other value is 50%(center). |
xpos ypos | It specifies the position of background image using pixels or any other CSS measurement units where x represents horizontal positioning while y represents vertical positioning. 0 0 represents top left corner while 100 100 represents bottom right corner. When only one value is specified, by default other value will be center. |
initial | It is used to set this property to it's default value. |
inherit | It is used to inherit the property of it's parent element. |
Return Value
It returns a string value which represents position of background image.
Examples of HTML DOM Style Object 'backgroundPosition' Property
The following examples illustrates Positioning of background using different ways.
Position using Keywords
In the following example, we have used keywords to position the background image.
<!DOCTYPE html> <html lang="en"> <head> <title> HTML DOM Style Object backgroundPosition Property </title> <style> body { background: #f3f3f3 url('/html/images/logo.png') no-repeat; height: 300px; width: 300px; } </style> </head> <body> <p>Click to change positioning.</p> <button onclick="topcen()">Top Center</button> <button onclick="bottomleft()">Bottom Left</button> <button onclick="centerright()">Center Right</button> <script> function topcen(){ document.body.style.backgroundPosition="top"; } function bottomleft(){ document.body.style.backgroundPosition="bottom left"; } function centerright(){ document.body.style.backgroundPosition="center right"; } </script> </body> </html>
Position using Percentage
In the following example, we have used percentage to position the background image.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Style Object backgroundPosition Property</title> <style> body { background: #f3f3f3 url('/html/images/logo.png') no-repeat; height: 300px; width: 300px; } </style> </head> <body> <p>Click to change positioning.</p> <button onclick="fun()">Change</button> <script> function fun(){ document.body.style.backgroundPosition="74% 80%"; } </script> </body> </html>
Position using Pixels
In the following example, we have used pixels to position the background image.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Style Object backgroundPosition Property</title> <style> body { background: #f3f3f3 url('/html/images/logo.png') no-repeat; height: 300px; width: 300px; } </style> </head> <body> <p>Click to change positioning.</p> <button onclick="fun()">Change</button> <script> function fun(){ document.body.style.backgroundPosition="60px 60px"; } </script> </body> </html>
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
backgroundPosition | Yes 1 | Yes 12 | Yes 1 | Yes 1 | Yes 3.5 |
html_dom.htm
Advertisements