Is it possible to center a background image in a div? I have tried just about everything - but no success:
Lorem Ipsum ...
This works for me:
.network-connections-icon {
background-image: url(url);
background-size: 100%;
width: 56px;
height: 56px;
margin: 0 auto;
}
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;
}
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
);
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.
#doit {
background: url(url) no-repeat center;
}
This works for me:
#doit{
background-image: url('images/pic.png');
background-repeat: no-repeat;
background-position: center;
}