Can I add image in CSS for background?

Web pages normally add images using the inline HTML "img" tag. CSS coding usually doesn't set an image's source because CSS controls design rather than content. Yet CSS supports properties that plain HTML does not, such as image opacity. For example, if you want a transparent photo of your company headquarters to appear on your website homepage, you can set this using CSS. CSS styles choose image sources using the background image property.

  1. 1.

    Open your website's stylesheet with your HTML editor or a text editor.

  2. 2.

    Paste the following code into the sheet to create a new style:

    styledimg {
    background-image: url(path);
    background-repeat: no-repeat;
    width: 10px;
    height: 20px;
    

    }

  3. 3.

    Replace "path" with the image's URL within the site. For example, if the image "building.jpg" is in your site's "images" folder, change the code to:

    styledimg {
    background-image: url(/images/building.jpg);
    background-repeat: no-repeat;
    width: 10px;
    height: 20px;
    

    }

  4. 4.

    Replace "10" with the image's width and replace "20" with the image's height. For example, if the image measures 200 pixels high and 600 pixels wide, change the code to:

    The background-image property in CSS applies a graphic (e.g. PNG, SVG, JPG, GIF, WEBP) or gradient to the background of an element.

    There are two different types of images you can include with CSS: regular images and gradients.

    Images

    Using an image on a background is pretty simple:

    body {
      background: url(sweettexture.jpg);
    }

    The url() value allows you to provide a file path to any image, and it will show up as the background for that element.

    You can also set a data URI for the url(). That looks like this:

    body {
      /* Base64 encoded transparent gif */
      background: url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7);
    }

    This technique removes one HTTP request, which is a good thing. But, there are a number of downsides, so before you start replacing all of your images make sure you consider all the pros and cons of Data URIs.

    You can also use background-image to sprite images, which is another useful method for reducing HTTP requests.

    Gradients

    Another option when using backgrounds is to tell the browser to create a gradient. Here’s a super-duper simple example of a linear gradient:

    body {
      background: linear-gradient(black, white);
    }

    You can also use radial gradients:

    body {
      background: radial-gradient(circle, black, white);
    }

    Technically speaking, gradients are just another form of background image. The difference is that the browser makes the image for you. Here’s an entire guide on how to make and use them.

    The example above uses only one gradient, but you can also layer multiple gradients on top of each other. There are some pretty amazing patterns you can create using this technique.

    Setting a fallback color

    If a background image fails to load, or your gradient background is viewed on a browser that doesn’t support gradients, the browser will look for a background color as a fallback. You can specify your fallback color like this:

    body {
      background: url(sweettexture.jpg) blue;
    }

    Multiple background images

    You can use multiple images, or a mixture of images and gradients, for your background. Multiple background images are well-supported now (all modern browsers and IE9+ for graphic images, IE10+ for gradients).

    When you’re using multiple background images, be aware that there’s a somewhat counter-intuitive stacking order. List the image that should be at the front first, and the image that should be at the back last, like this:

    body {
      background: url(logo.png), url(background-pattern.png);
    }

    When you’re using multiple background images, you’ll often need to set more values for the background to get everything in the right place. If you want to use the

    body {
      /* Base64 encoded transparent gif */
      background: url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7);
    }
    1 shorthand, you can set the values for each image individually. Your shorthand will look something like this (notice the comma separating the first image and its values from the second image and its values):

    body {
      background:
        url(logo.png) bottom center no-repeat,
        url(background-pattern.png) repeat;
    }
    

    There’s no limit to how many background images you can set, and you can do cool things like animate your background images. There’s a good example of multiple background images with animation on David Walsh’s blog.

    Can you add an image through CSS?

    Using CSS to insert images into your web pages With CSS, all block-level and inline elements (tags) can have background images inserted into them.

    Why can't I set a background image in CSS?

    Make sure the image path is set correctly in the background-image url. Once you have made sure that your CSS file is linked correctly, also check that the image itself is set correctly. Again, you will want to open your code inspector in the browser to check.

    Should I use CSS background image?

    When to use CSS background-image. Use background-image if you need to improve download times, as with CSS sprites. Use background-image if you need for only a portion of the image to be visible, as with CSS sprites.