Search code examples
cssbackgroundsplitsizescale

How to scale background image


I have a site I am working on, where I would like a background, with white in the middle and another color like black on the sites. I have made a code like this.

body { background-color: #000000; background-image: url('BackgroundImg.png'); }

Now my problem is that the background image streches, to fit the whole screen, where I only want it to be in the middle, with a specific width size. How do I scale the background image to do so?


Solution

  • Following code gives you centered image with no repeat , you can adjust position by giving position

     body {
    background-color: #000000;
    background-image: url('BackgroundImg.png');
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: center; }
    

    For size adjustment: replace size-x, size-y with image size manually

     body {
    background-color: #000000;
    background-image: url('BackgroundImg.png')size-x,size-y;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: center; }