Centering a background image, using CSS

前端 未结 13 2021
佛祖请我去吃肉
佛祖请我去吃肉 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:15

    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;
    }
    
    0 讨论(0)
  • 2020-11-28 04:21

    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.

    0 讨论(0)
  • 2020-11-28 04:21
    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;
    
    0 讨论(0)
  • 2020-11-28 04:22

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

    0 讨论(0)
  • 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%;
    }
    
    0 讨论(0)
  • 2020-11-28 04:23

    simply replace

    background-image:url(../images/images2.jpg) no-repeat;
    

    with

    background:url(../images/images2.jpg)  center;
    
    0 讨论(0)
提交回复
热议问题