
- 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
How to set a background image to be fixed with JavaScript DOM?
To set the background image to be fixed in JavaScript, use the backgroundAttachment property. It allows you to set an image that won’t scroll.
Example
You can try to run the following code to learn how to work with backgroundAttachment property with JavaScript −
<!DOCTYPE html> <html> <body> <button onclick="display()">Click to Set background</button> <p>Demo Text</p> <p>Demo Text</p> <p>Demo Text</p> <p>Demo Text</p> <p>Demo Text</p> <p>Demo Text</p> <p>Demo Text</p> <p>Demo Text</p> <p>Demo Text</p> <p>Demo Text</p> <p>Demo Text</p> <p>Demo Text</p> <p>Demo Text</p> <p>Demo Text</p> <p>Demo Text</p> <p>Demo Text</p> <script> function display() { document.body.style.background = "url('https://www.tutorialspoint.com/html5/images/html5-mini-logo.jpg') no-repeat right top"; document.body.style.backgroundAttachment = "fixed"; } </script> </body> </html>
- Related Articles
- Set background image as fixed with CSS
- How to set the starting position of a background image in JavaScript DOM?
- How to style background image to no repeat with JavaScript DOM?
- How to set the size of the background image with JavaScript?
- How to set the positioning area of the background image with JavaScript?
- How to set a background Image With React Inline Styles?
- How to set all the background properties in one declaration with JavaScript DOM?
- How to set the image to be used as a border with JavaScript?
- How to set background image of a webpage?
- How to set a background image to a JavaFX chart?
- How to set background image in CSS using jQuery?
- Set the Background Image Position with CSS
- How to return the background color of an element with JavaScript DOM?
- How to set the painting area of the background with JavaScript?
- How to create a blurry background image with CSS?

Advertisements