Computer >> Computer tutorials >  >> Programming >> CSS

Setting Background Position in CSS


The CSS background-position property is used to set an initial position for a background image relative to the origin.

Syntax

The syntax of CSS background-position property is as follows −

Selector {
   background-position: /*value*/
}

The following examples illustrate CSS background-position property −

Example

<!DOCTYPE html>
<html>
<head>
<style>
div {
   margin: 30px;
   padding: 100px;
   background-image: url("https://www.tutorialspoint.com/images/C-programming.png"), url("https://www.tutorialspoint.com/images/Swift.png"), url("https://www.tutorialspoint.com/images/xamarian.png");
   background-repeat: no-repeat;
   background-position: 15% 20%, top, right;
   border: 1px solid;
}
</style>
</head>
<body>
<div>
</div>
</body>
</html>

Output

This gives the following output −

Setting Background Position in CSS

Example

<!DOCTYPE html>
<html>
<head>
<style>
p {
   margin: 20px;
   padding: 80px;
   background-image: url("https://www.tutorialspoint.com/images/dbms.png"), url("https://www.tutorialspoint.com/images/Operating-System.png");
   background-repeat: no-repeat;
   background-position: 0 0, bottom right;
   background-color: lemonchiffon;
}
</style>
</head>
<body>
<p>This is demo paragraph. This is demo paragraph. This is demo paragraph. This is demo paragraph. This is demo paragraph. This is demo paragraph. This is demo paragraph. This is demo paragraph. This is demo paragraph. This is demo paragraph. This is demo paragraph. This is demo paragraph. This is demo paragraph. This is demo paragraph. </p>
</body>
</html>

Output

This gives the following output −

Setting Background Position in CSS