Centering a background image, using CSS

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

    Try this background-position: center top;

    This will do the trick for you.

    0 讨论(0)
  • 2020-11-28 04:25

    if your not satisfied with the answers above then use this

        * {margin: 0;padding: 0;}
    
        html
        {
            height: 100%;
        }
    
        body
        {
            background-image: url("background.png");
            background-repeat: no-repeat;
            background-position: center center;
            background-size: 80%;
        }
    
    0 讨论(0)
  • 2020-11-28 04:27

    The only thing that worked for me is..

    margin: 0 auto
    
    0 讨论(0)
  • 2020-11-28 04:33

    Had the same problem. Used display and margin properties and it worked.

    .background-image {
      background: url('yourimage.jpg') no-repeat;
      display: block;
      margin-left: auto;
      margin-right: auto;
      height: whateveryouwantpx;
      width: whateveryouwantpx;
    }
    
    0 讨论(0)
  • 2020-11-28 04:38

    Try

    background-position: center center;
    
    0 讨论(0)
  • 2020-11-28 04:39

    I think this is what is wanted:

    body
    {
        background-image:url('smiley.gif');
        background-repeat:no-repeat;
        background-attachment:fixed;
        background-position:center;
    } 
    
    0 讨论(0)
提交回复
热议问题