How do I use the CSS background-position property to center a background image to its parent element? I can only get the background image to center to the entire page.
+4
A:
background-position: center;
background-repeat: no-repeat;
works for me. centers horiz and vertically
Scott Evernden
2009-08-26 05:28:55
+1
A:
Keep in mind that block-level elements which are not re-sized in some way (using height/width or float or margins) will always take up the width of the window, even if the content of the element does not. This means that centering the background to the element will center it to the window as well.
To check if that's the issue, I usually add
border: 1px dashed #f00;
to the element.
If the element is re-sized, Scott's answer should work.
Anthony
2009-08-26 05:36:01
You should use the `outline` property for debugging, since it doesn't change the document flow.
DisgruntledGoat
2009-08-26 12:04:53
Good idea. I usually just need to see the general geography.
Anthony
2009-08-27 02:48:51