EN
CSS - custom background image position
0 points
In this article, we would like to show you how to set custom background image position using CSS.
Quick solution:
xxxxxxxxxx
1
div {
2
background-position: top 10px left 20%; /* example value */
3
}
Note:
The default value of
background-position
property istop 0 left 0
(top-left corner).
To set the background image position, you can use custom values and various units (e.g 10px
, 15em
or 25%
).
Below we present example cases of custom background-image position.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
body {
6
display: flex;
7
}
8
9
div {
10
height: 100px;
11
width: 100px;
12
border: 1px solid black;
13
margin: 5px;
14
/* background properties */
15
background-image: url('https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png');
16
background-size: 50px;
17
background-repeat: no-repeat;
18
}
19
20
.custom-1 {
21
background-position: top 10px left 30px;
22
}
23
24
.custom-2 {
25
background-position: bottom 5% right 1em;
26
}
27
28
.custom-3 {
29
background-position: bottom 25px left 30%;
30
}
31
32
.custom-4 {
33
background-position: bottom 1em right 30%;
34
}
35
36
</style>
37
</head>
38
<body>
39
<div class="custom-1"></div>
40
<div class="custom-2"></div>
41
<div class="custom-3"></div>
42
<div class="custom-4"></div>
43
</body>
44
</html>