
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to set the positioning area of the background image with JavaScript?
In this tutorial, we will learn how to set the positioning area of the background image using JavaScript
The background image’s positioning area in a program can be changed using the HTML DOM Style background-origin property.
Background-origin is a feature that allows you to modify the background images of a webpage. The origin of the picture in the background is set using this property. The background image origin is set to the upper-left corner of the screen/webpage by default.
The background-origin property specifies the origin of the background: from the border start, within the border, or the padding. When the background-attachment value is set to "fixed," this property will not operate. The background-origin property is identical to the background-clip property, except instead of cutting the background, it resizes it. The top-left corner of the screen is the element's default origin.
We may use commas to separate the values of the background-origin attribute for each background image if the element contains more than one background picture. Every picture will correspond to the value of the background-origin attribute.
Following are the methods to set the positioning area of the background image with JavaScript.
Using the backgroundOrigin content-box property
The backgroundOrigin property provides the location of the background picture, and the background is positioned relative to the content box using the content-box property. The backdrop begins at the upper left corner of the content. The content placed inside the box is covered in the content-box property.
The padding box is the default value and places the background relative to the box. The padding edge's top left corner serves as the starting point for the background.
Syntax
document.getElementById("box").style.backgroundOrigin = "content-box";
The box is fetched using the getElementbyId() method, and the background origin of the box is specified to the content box property.
Example
In the example, we have created a box using parameters such as border, padding, width, height, and background color. The box contains some content in the form of text inside.
The box accommodates a picture whose positioning area is specified as its default value of the padding box. This property is changed to the content box property of the background-origin attribute. This image was placed in the padding area of the box and is shifted to the area which contains the text.
<html> <head> <style> #box { border: 3px dashed red; padding: 35px; width: 500px; height: 260px; background: url('https://www.tutorialspoint.com/videotutorials/images/coding_ground_home.jpg') no-repeat; background-color: yellow; background-origin: padding-box; } </style> </head> <body> <h3> Set the positioning area of the background image using <i> content-box property </i> </h3> <button onclick="myFunction()"> Set area of background image</button> <div id="box"> This is demo text. </div> <script> function myFunction() { document.getElementById("box").style.backgroundOrigin="content-box"; } </script> </body> </html>
Using the backgroundOrigin padding-box property
The backgroundOrigin property indicates the background's positioning area, which is the position of the origin of an image supplied by the background-image property.
The padding box property specifies that the background extends to the padding's outer edge. Below the border, no background is drawn. This is the default option. The borderbox property specifies that the background extends to the borders outside the edge. The background is drawn underneath the border.
This background-origin property's initial value is set to its default value. The "inherit" property specifies that the related element inherits the background-origin property of its parent element.
Syntax
document.getElementById("box").style.backgroundOrigin = "padding-box";
The box is acquired using the getElementbyId() method, and the padding box property provides the box's background-origin.
Example
In this example, we've constructed a box including border, padding, width, height, and background color. Inside the box, there is some data in the form of text. The box contains an image whose placement area is specified as the border box attribute. This property is modified to the background-origin attribute's padding box property. This picture was placed such that it covered the box's borders. However, after applying the padding box property, the image is relocated to the region that encloses the box's padding, omitting the borders.
<html> <head> <style> #box { border: 3px dashed red; padding: 35px; width: 500px; height: 260px; background: url('https://www.tutorialspoint.com/videotutorials/images/coding_ground_home.jpg') no-repeat; background-color: yellow; background-origin: border-box; } </style> </head> <body> <h3> Set the positioning area of the background image using <i> padding-box property </i> </h3> <button onclick="myFunction()"> Set area of background image</button> <div id="box"> This is a demo text. </div> <script> function myFunction() { document.getElementById("box").style.backgroundOrigin="padding-box"; } </script> </body> </html>
In this tutorial, we learned how to set the positioning area of the background image with JavaScript using the background-origin content-box property and backgroundorigin padding-box property.
- Related Articles
- How to set the size of the background image with JavaScript?
- How to set the painting area of the background with JavaScript?
- How to set a background image to be fixed with JavaScript DOM?
- Set the Background Image Position with CSS
- How to set the starting position of a background image in JavaScript DOM?
- Set the background image of an element with CSS
- How to set the type of positioning method used for an element with JavaScript?
- How to set the widths of the image border with JavaScript?
- How to set background image of a webpage?
- How to set a background Image With React Inline Styles?
- FabricJS – How to set the background colour of selection of an Image?
- How to set the inward offsets of the image border with JavaScript?
- How to set the amount by which the border image area extends beyond the border box with JavaScript?
- Set background image as fixed with CSS
- Set a linear gradient as the background image with CSS
