5

This is quite embarrasing. I am learning new things but I am surprised I can't figure this out on my own.

On my desktop, I have a folder called Test and in that I have index.html and a folder called CSS, which contains index.css and another folder called images with an image called logo.png.

In my index.css file, I have the following line:

background: url("images/logo.png") no-repeat;

Using the above line I can't display the image locally. I tried using /images and test/images but it doesn't work. How can I display this if I am viewing it locally?

Joe
  • 29,416
  • 12
  • 68
  • 88
ariel
  • 2,962
  • 7
  • 27
  • 31
  • 1
    See http://stackoverflow.com/questions/9384299/css-relative-url-to-images .. look up relative URL paths.. you will want `"../images/logo.png"` – Josh Crozier Oct 06 '13 at 02:08

4 Answers4

5

As you mentioned, you have different folders for CSS and images inside your root folder Test. Since you are writing code for background in your CSS file:

Case 1:

background:url("logo.png");

Will search for your image inside CSS folder.


Case2:

background:url("images/logo.png");

Will search for images folder first inside CSS folder and then will search for logo.png inside that images folder. (But there is no images folder inside your CSS folder).


Case3:

background: url("../images/logo.png") no-repeat;


In this case .. will take you back to the main directory (i.e. from css folder to your root forder Test. Then /images will take you inside images folder and it will find /logo.png as a path to your image.)

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
vishalkin
  • 1,175
  • 2
  • 12
  • 24
3

Change img to the name of your class. Use content instead of background or background-image, and Set some width.

img {
     content: url("../img/Wizard-of-os-black.png");
     width: 90px;
}
misterzik
  • 1,740
  • 1
  • 16
  • 20
  • how is this a bad negative answer? – misterzik Nov 27 '15 at 16:06
  • 1
    Maybe because you've misspelt 'Oz'? – Neil T Sep 19 '17 at 15:17
  • Very nice. I fiddled with background and background-image, had to set width and height and had a thin border even with border=0 padding=0 0 0 0 and margin=0 0 0 0. Now I just need a class, display block and margin 0 auto 0 auto to display a logo centered. – jamacoe Mar 17 '20 at 20:28
0

Try changing this to

background-image: url("images/logo.png") no-repeat;
Ron_B
  • 66
  • 7
0

Basically, you would have to think logically. If your in a CSS file in contained within a folder, all links in the file are searched in the folder. If you want to link to an image in a folder called /img in the root of your site, you would have to move up a level by using

../img/pic.extension