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?
If your background image is a vertically aligned sprite sheet, you can horizontally center each sprite like this:
If your background image is a horizontally aligned sprite sheet, you can vertically center each sprite like this:
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.
This works for me :
This works for me:
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 forbackground-position: center center
; (background-position: horizontal vertical
);Use background-position: