157

Is it possible to center a background image in a div? I have tried just about everything - but no success:

<div id="doit">
  Lorem Ipsum ...
</div>

//CSS
#doit { background-image: url(images/pic.png); background-repeat: none; text-align: center; }

Is this possible?

Hemerson Varela
  • 24,034
  • 16
  • 68
  • 69
oompahloompah
  • 9,087
  • 19
  • 62
  • 90

9 Answers9

312
#doit {
    background: url(url) no-repeat center;
}
Lucas
  • 10,476
  • 7
  • 39
  • 40
  • 29
    Thanks. The specific CSS for centering is "background-position: center;" – pmont Jun 10 '15 at 17:11
  • @Green: See my answer for sprite background images. – Clayton Sep 14 '15 at 15:09
  • 5
    If you want the entire div to be filled with the image and no extra space you should use `background-size: cover;` If you want the entire image to show without any part of the image being cut off or stretched you want to use `background-size: contain;` – Zlerp Feb 28 '18 at 22:47
87

try background-position:center;

EDIT: since this question is getting lots of views, its worth adding some extra info:

text-align: center will not work because the background image is not part of the document and is therefore not part of the flow.

background-position:center is shorthand for background-position: center center; (background-position: horizontal vertical);

TimCodes.NET
  • 4,601
  • 1
  • 25
  • 36
  • 1
    actually you just need `background-position:center;` – Dave Sag Jul 11 '12 at 09:23
  • The selected answer cause my whole background to disappear for some reason (to be fair I'm doing some jquery stuff that may be causing it?), but this answer works. Upvote. – bwoogie Jul 18 '17 at 20:15
16

For center or positioning the background image you should use background-position property . The background-position property sets the starting position of a background image from top and left sides of the element . The CSS Syntax is background-position : xvalue yvalue; .

"xvalue" and "yvalue" supported values are length units like px and percentage and direction names like left, right and etc .

The "xvalue" is the horizontal position of the background and starts from top of the element . It means if you use 50px it will be "50px" away from top of the elements . And "yvalue" is the vertical position that has the same condition .

So if you use background-position: center; your background image will be centered .

But I always use this code :

.yourclass {
  background: url(image.png) no-repeat center /cover;
 }

I know it is so confusing but it means :

.yourclass {
  background-image: url(image.png);
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}

And I know that too the background-size is a new property and in the compressed code what is /cover but these codes means fill background sizing and positioning in windows desktop background .

You can see more details about background-position in here and background-size in here .

Morteza Sadri
  • 695
  • 9
  • 18
16

Use background-position:

background-position: 50% 50%;
Capsule
  • 6,118
  • 1
  • 20
  • 27
7

If your background image is a vertically aligned sprite sheet, you can horizontally center each sprite like this:

#doit {
    background-image: url('images/pic.png'); 
    background-repeat: none;
    background-position: 50% [y position of sprite];
}

If your background image is a horizontally aligned sprite sheet, you can vertically center each sprite like this:

#doit {
    background-image: url('images/pic.png'); 
    background-repeat: none;
    background-position: [x position of sprite] 50%;
}

If your sprite sheet is compact, or you are not trying to center your background image in one of the aforementioned scenarios, these solutions do not apply.

Clayton
  • 392
  • 1
  • 3
  • 17
6

This works for me:

#doit{
    background-image: url('images/pic.png');
    background-repeat: no-repeat;
    background-position: center;  
}  
Hemerson Varela
  • 24,034
  • 16
  • 68
  • 69
4

This works for me:

.network-connections-icon {
  background-image: url(url);
  background-size: 100%;
  width: 56px;
  height: 56px;
  margin: 0 auto;
}
4

This works for me for aligning the image to center of div.

.yourclass {
  background-image: url(image.png);
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}
1

This works for me :

<style>
   .WidgetBody
        {   
            background: #F0F0F0;
            background-image:url('images/mini-loader.gif');
            background-position: 50% 50%;            
            background-repeat:no-repeat;            
        }
</style>        
László Papp
  • 51,870
  • 39
  • 111
  • 135