I want to center a background image. There is no div used, this is the CSS style:
body{
background-position:center;
background-image:url(../images/im
I use this code, it works nice
html, body {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
background: black url(back2.png) center center no-repeat;;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
There's an error in your code. You're using a mix of full syntax and shorthand notation on the background-image property.This is causing the no-repeat to be ignored, since it's not a valid value for the background-image property.
body{
background-position:center;
background-image:url(../images/images2.jpg) no-repeat;
}
Should become one of the following:
body{
background:url(../images/images2.jpg) center center no-repeat;
}
or
body
{
background-image: url(path-to-file/img.jpg);
background-repeat:no-repeat;
background-position: center center;
}
EDIT: For your image 'scaling' issue, you might want to look at this question's answer.
background-position: center center;
doesn't work for me without...
background-attachment: fixed;
using both centered my image on x and y axis
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
Like this:
background-image:url(https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Dore_woodcut_Divine_Comedy_01.jpg/481px-Dore_woodcut_Divine_Comedy_01.jpg);
background-repeat:no-repeat;
background-position: center;
background-size: cover;
html{
height:100%
}
body{
background-image:url(https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Dore_woodcut_Divine_Comedy_01.jpg/481px-Dore_woodcut_Divine_Comedy_01.jpg);
background-repeat:no-repeat;
background-position: center;
background-size: cover;
}
background-image: url(path-to-file/img.jpg);
background-repeat:no-repeat;
background-position: center center;
That should work.
If not, why not make a div
with the image and use z-index
to make it the background? This would be much easier to center than a background image on the body.
Other than that try:
background-position: 0 100px;/*use a pixel value that will center it*/
Or I think you can use 50% if you have set your body min-height
to 100%.
body{
background-repeat:no-repeat;
background-position: center center;
background-image:url(../images/images2.jpg);
color:#FFF;
font-family:Arial, Helvetica, sans-serif;
min-height:100%;
}
simply replace
background-image:url(../images/images2.jpg) no-repeat;
with
background:url(../images/images2.jpg) center;