View video tutorial
CSS Background Position
CSS
This property sets the starting position of a background image.
CSS background-position property
➔ The background-position property specifies the starting position of a background image.
Syntax
background-position: top | bottom | left | right | center | center center | 50% 50% | 50px 50px; /*etc.*/
Property values
| Value | Description |
|---|---|
| left top left center left bottom right top right center right bottom center top center center center bottom |
If only one keyword value is specified, the other value will be the "center" keyword value. |
| x% y% | The first value is the horizontal position and the second value is the vertical position. The upper left corner is at (0% 0%) position and it is default value. The bottom right corner is (100% 100%). If only one value is specified, the other value will be 50%. Any CSS unit is acceptable. |
| xpos ypos | The first value is the horizontal position and the second value is the vertical position. The upper left corner is at (0 0) position. If only one value is specified, the other value will be 50%. Any CSS unit is acceptable. |
| initial | Sets the property to its default value. |
| inherit | The element inherits the property value from its parent element. |
Example (center)
body {
background-image: url('resources/images/cat-cartoon2.png');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
Click on the "Try it Now" button to see how it works.
Sample Code
Copy each file contents and paste it to your own project and run to see the final output
Example (center top)
body {
background-image: url('resources/images/cat-cartoon2.png');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center top;
}
Click on the "Try it Now" button to see how it works.
Example (bottom right)
body {
background-image: url('resources/images/cat-cartoon2.png');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: bottom right;
}
Click on the "Try it Now" button to see how it works.
Example (50% 50%)
body {
background-image: url('resources/images/dog-cartoon2.png');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50% 50%;
}
Click on the "Try it Now" button to see how it works.
Example(100px 10px)
<!DOCTYPE html>
<html lang="en">
<head>
<title>CSS background-position Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="utf-8" />
<style>
div {
background-image: url('resources/images/animals-cat-t.png');
background-repeat: no-repeat;
background-position: 100px 10px;
min-height: 230px;
border: 2px dashed white;
border-radius: 5px;
padding: 10px;
margin-top: 100px;
}
</style>
</head>
<body>
<div>
<h1>CSS background-position Example</h1>
<p>The CSS background-position is used to set the background position on the element.</p>
<p>The background-position property specifies the starting position of a background image.</p>
</div>
</body>
</html>
Copy the code and click on the "Try it Now" button to see how it works.