Centering a background image, using CSS

前端 未结 13 2058
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 03:59

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         


        
13条回答
  •  清酒与你
    2020-11-28 04:23

    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%;
    }
    

提交回复
热议问题