Home > CSS > Positioning Background Image Using CSS

Positioning Background Image Using CSS

In this article, we are going to answer those questions:

  • How to set position of background image in css?
  • How to position background image using css?
  • Can i set position of background image with css?
  • Can css position background-image of a div?


Actually, there is a short method to do this. Look at structure 1.0:
Structure 1.0:

1
2
3
4
#[DIVID] {
background-image:url([image url]);
background-position:[Horizontal position] [Vertical position]
}

Example 1.0: Assume, we have a div named “ourdiv” and an image named “ourimage.jpg”.

1
2
3
4
5
6
#ourdiv {
width: 86px;
height: 30px;
background-image: url(ourimage.jpg);
background-position: right top;
}

Original view of ourimage.jpg:

Original view of ourimage.jpg

It’s size: 259×202px


Output of Example 1.0:


Structure 1.1: We can use this structure instead Structure 1.0. We advice you to use this structure.

1
2
3
#[DIVID] {
background:url([image url]) [Horizontal position] [Vertical position];
}

Let’s designate another position…
Example 1.1:

1
2
3
4
5
#ourdiv {
width: 86px;
height: 30px;
background: url(ourimage.jpg) right center;
}

Output of Example 1.1:


Example 1.2:

1
2
3
4
5
#ourdiv {
width: 86px;
height: 30px;
background: url(ourimage.jpg) center bottom;
}

Output of Example 1.2:


Example 1.3:

1
2
3
4
5
#ourdiv {
width: 259px;
height: 30px;
background: url(ourimage.jpg) center center;
}

Output of Example 1.3:


If we want to designate a start position for background image, then we can designate numeric position instead default values. Look at Example 1.4.
Example 1.4:

1
2
3
4
5
#ourdiv {
width: 200px;
height: 100px;
background: url(ourimage.jpg) 30px 30px;
}

Output of Example 1.4:

As you see, right side of image is seen in left side of div and bottom side of image is seen top side of div. Because, if we don’t define background-repeat property, the background image is repeated on x and y direction. If we don’t want this, then we should just add no-repeat to background properties. Look at example 1.5.


Example 1.5:

1
2
3
4
5
6
#ourdiv {
width: 200px;
height: 100px;
background: url(ourimage.jpg) 30px 30px no-repeat;
/* or add this code: background-repeat: no-repeat; */
}

Output of Example 1.5:


It’s name could be “background image margin” or “background image padding” :P


We tried to show you that the simple method of background image positioning with CSS.
We hope you will find this article helpful…

Bookmark and Share
  1. No comments yet.
  1. No trackbacks yet.
eXTReMe Tracker