How to give the background-image path in CSS?

HtmlCss

Html Problem Overview


My CSS file is in :

Project/Web/Support/Styles/file.css

My image is in :

Project/Web/images/image.png

I want this image in my CSS file.

I have tried :

1) background-image: url(/images/image.png);
2) background-image: url('/images/image.png');
3) background-image: url("/images/image.png");
4) background-image: url(../images/image.png);
5) background-image: url('../images/image.png');
6) background-image: url("../images/image.png");

But. i'm not getting this image in my page.

What is the correct way to specify the path of image file in the css file ?

Html Solutions


Solution 1 - Html

Your css is here: Project/Web/Support/Styles/file.css

1 time ../ means Project/Web/Support and 2 times ../ i.e. ../../ means Project/Web

Try:

background-image: url('../../images/image.png');

Solution 2 - Html

There are two basic ways:

url(../../images/image.png)

or

url(/Web/images/image.png)

I prefer the latter, as it's easier to work with and works from all locations in the site (so useful for inline image paths too).

Mind you, I wouldn't do so much deep nesting of folders. It seems unnecessary and makes life a bit difficult, as you've found.

Solution 3 - Html

Use the below.

background-image: url("././images/image.png");

This shall work.

Solution 4 - Html

You need to get 2 folders back from your css file.

Try:

background-image: url("../../images/image.png");

Solution 5 - Html

you can also add inline css for adding image as a background as per below example

<div class="item active" style="background-image: url(../../foo.png);">

Solution 6 - Html

The solution (http://expressjs.com/en/starter/static-files.html).

once done this the image folder no longer shalt put it. only be

background-image: url ( "/ image.png");

carpera that the image is already in the static files

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJobinView Question on Stackoverflow
Solution 1 - HtmlcodingroseView Answer on Stackoverflow
Solution 2 - Htmlralph.mView Answer on Stackoverflow
Solution 3 - HtmlNiteshView Answer on Stackoverflow
Solution 4 - HtmlUnderTheSeaView Answer on Stackoverflow
Solution 5 - HtmlYogeshView Answer on Stackoverflow
Solution 6 - HtmllypefView Answer on Stackoverflow