w3resource

CSS Properties: How to set the a background-image in the right-center of a document?

Go to Exercise page

Solution:

HTML Code:

<!DOCTYPE html><!-- Declaration of HTML5 document type -->
<html>
<head>
<title>How to set the a background-image in the right-center of a document</title><!-- Title of the HTML document -->
<style type="text/css">/* CSS style start*/
#xyz1 {
border: 8px solid blue; /* Setting border properties for the element with id "xyz1" */
padding: 40px; /* Setting padding properties for the element with id "xyz1" */
background: url(https://www.w3resource.com/images/w3resource-logo.png); /* Setting background image for the element with id "xyz1" */
background-repeat: no-repeat; /* Preventing the background image from repeating */
background-position: right center; /* Positioning the background image to the right center of the element with id "xyz1" */
}
</style>
<body>
<p>No background-origin (padding-box is default):</p><!-- Paragraph explaining the absence of background-origin -->
<div id="xyz1"><!-- Division element with id "xyz1" -->
<h2>Background image origin</h2><!-- Heading inside the division element -->
<p>The background-origin CSS property determines the background positioning area, that is the position of the origin of an image specified using the background-image CSS property.</p><!-- Paragraph explaining background-origin CSS property -->
</div>
</body>
</html>

Explanation:

  • This HTML document demonstrates how to set a background image to the right center of a specific element.
  • The CSS style block defines styling for an element with the id "xyz1".
  • The background image used in this example is the w3resource logo.
  • The background image is set not to repeat using background-repeat: no-repeat.
  • The position of the background image is set to the right center of the element using background-position: right center.
  • The HTML content within the div with id "xyz1" explains the concept of background-origin CSS property.

Live Demo:

See the Pen background-position-right-center-answer by w3resource (@w3resource) on CodePen.


See the solution in the browser

Supported browser

Firefox logo Chrome logo Opera logo Safari logo Internet Explorer logo
Yes Yes Yes Yes Yes

Go to Exercise page

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/html-css-exercise/css-properties/background-position-right-center-answer.php