External CSS images won't load - css

Good day.
My structure is like this:
/
index.html
style/
main.css
images/
test/
test.html
/style/main.css says something like this:
body {
background-image: url('/images/SomeImage.png');
background-color: #000;
}
/index.html has a link to this CSS file, but, as the title says, no image will load. But it's connected though, cause the background is actually black, so the rest of the style (but images) does work.
Also, if I write the same style internally into /index.html the background will load.
Also, I created /test/test.html which says nothing but
<img src="/images/SomeImage.png" />
and the image is displayed on that page.
So, obviously, for some reason my /style/main.css can't reach files, that any other file from any other location reaches. Why does this happen? There's clearly nothing wrong with the syntax. I'm lost.

add ../ to the beginning of /images so it read ../images/imagename.jpg
Here's what your code should be:
body {
background-image: url('../images/SomeImage.png');
background-color: #000;
}

Because your image is in another folder (thats a level up than your style sheet), you need to start with "../" for a level up folder in hierarchy relative to the style sheet. So you need a relative URL:
background-image: url('../images/SomeImage.png');

Try to copy webpage, css and example image in one folder temporarily. Then use only image name for url a see what happens. If it works, it will be the image path, if not something else.. possibly position.. is this complete css you are posting?

Initially, it looks like your code is fine.
So how do you know the image isn't loading? Look in your browser's developer tools to see if the image is loading, or returning an error, or not even being referenced. My guess here would be that it is loading, but not display because of something in your CSS.

if you are in /styles/style.css you need to add:
../ 2 levels back to get to the root folder.
So as Rokin answered :
background-image: url('../images/SomeImage.png');
is the way to do it.
To link your CSS within your index file use the following:
<link href="./style/style.css" rel='stylesheet' type='text/css'>
./ 1 level back within the index.html to reach the root folder.
In addition your problem might also be a file permission problem, I always face this issue when i download images from my email and use them directly.
If you are working locally on a mac:
- Right click on the selected image
- click on **get info**
- In sharing and permissions, make sure that the **everyone** has the **Read only** permission instead of **No access**
If you are working directly on a live server:
- login using FTP (with any ftp client such as File Zilla)
- Go to the selected image
- Right click and select file permissions
- set permissions to : **664**

Ok, so basically, I replaced the not-working /style/main.css with the copy of it (test.css - described in post comments) and now it works. Why is still the question, but the problem is kinda solved I guess.

Same with me, I guess images that used in css must be in the same folder as css file. I tried every possible solution while checking with the browser tool and the only thing that works is when I put the image and stylesheet in the same folder.

I am having the same problem. Working with Visual Studio Community.
I went inspect elements in browser and found that the file directory "automatically" (i did not set it this way) says that my image folder is nested inside my css folder. dont know why yet... so I then went and moved my image folder into my css folder seeing that this is what my browser showed me in the dev tools...
so maybe for some reason when working with css your images inside your image folder should be located in your css folder and not the complete Webpage Folder..it worked.

Related

Can't find resource from css

I can't find solution to load image linked in css. Chrome inspector shows me message:
404 (not found)
and shows me this link: http://127.0.0.1:5500/components/css/img.png
even if I have in my css
background-image: url(../src/splash_1/img.png)
Could anyone please help me with appropriate linking... I can't find answer how to correctly link that image in that folder structure.
UPDATE:
Below in the second picture attached, I added print screen with real data from Chrome Inspector and code and the element. Maybe that will be more helpful. I want to link correctly
ccbackground.jpg
Thank you!
You just need to pass the path as a string in single quotes with the same path you tried first. The .. is needed to step one folder up from the css folder.
background-image: url('../src/splash_1/img.png');
Note: it can be that the path is cached locally on the client side, you may need to clear browser cache to make it work.
Eventually it turned out I had 2 pairs of the same css files in different location. One pair in correct location and the other in main folder. I was changing css files in editor located in main folder, but in index.html css files were linked to specified folder. So even I changed css it didn't reflect changes in index.html, because the changes were done in the same name css files but in other location. I wonder how it happened I had 2 pairs of css files in different location, this is something really unclear for me. Thank you All for your help and your time.
Try this
background-image: url('/src/splash_1/img.png');
Please try below code...
background-image: url('components/src/splash_1/img.png');

how do i solve issues regarding external style sheet link which is not in the same directory of the programming file,but works?

I programmed a page (read index.php) including front end and back end operations,and in between i included a link -<link rel="stylesheet" href="style.css" type="text/css">, but there is no effect on the front end application whether I put this css file in the same directory of index.php or delete it. But when I remove the link from the programming page then the front end page is affected. So, how do I conclude this situation? Please anybody help me. I don't understand how the css file works when it is not in the directory where programming page lies.
Based on the information given it sounds like the file is being included from a cache.
Deleting the file on the server wouldn't affect the page render since there is a locally cached version of the file but deleting the link would prevent the browser from loading the cached version.
Sometimes when working on your localhost you have to put the entire path of the CSS file, not just it's normal location as you do when on a live website. Copy your entire path in the href on the link tag and see if it works.
Normally, if the file is in the same place as your index, you put the href the same way it is written in your example above. If your CSS file is in another folder, you would put ../style.css instead. Writing like that, will move up one folder.

Multiple Folder Organising and Referencing CSS

OK so I am Trying To Organize a file directory for a website. It looks like this.
- My Blog Site
- HTML-CSS-DOCUMENTS
- CSS-MAIN.css
- BACKGROUND-IMAGES
- Background.png
But in the CSS document I cant seem to get it to use the image.
Here is the code I have been using
background-image:url("My Blog Site\BACKGROUND-IMAGES\Background.png");
Can someone please fix it or tell me what I'm doing wrong, thanks!
You need to go back in the path and get your image. something like this:
background-image: url('../BACKGROUND-IMAGES/Background.png');
Here ../ means going one step back in the path. And use / instead of \.
You would use in your CSS:
background-image: url('../BACKGROUND-IMAGES/Background.png');
You go back one directory using the ../ and then link from that directory.
This should work fine:
background:url('http://mywebsite.com/background-images/background.png');

fix file path according to iis

i have a website #local and lots of images in site, but after I deploy site to server images paths are broken(exception given in css file), and I need to fix this as soon as possible.
sample path:
imageurl = #"/Images/sample.gif";
how can i fix this?
thank you.
you can set a appsetting in web.config file with server url like
and get this path on code and add image name with it.
Images (like background-url) in CSS are always referenced relative to the css file.
you need to set in css file like...
background-image: url( '../../Images/image.gif' );
.. this will bring out one folder from current folder hierarchy

Can't view image from webroot in CakePHP

I am trying to display an image from the /webroot/img/ folder using the following CSS syntax: .template-mainbg{background:#B8D9EA url('../img/bg_top.png') repeat-x left top;}
The image never loads and if I try http://site.com/img/bg_top.png it gives me a Missing Img controller error.
This very wierd since the default Cake icon loads perfectly. Both from CSS and direct link.
What could be the problem?
Thank you!
EDIT I solved this problem. Unfortunately I spent 2 hours to figure out that Dreamweaver didn't sync the image with the remote server. Thank you anyway for all your help!
paths are relative to the CSS document, so it depends where the CSS file is being served from.
In the above example, ../img/bg_top.png your CSS file would need to be in the site root I believe.
The image never loads and if I try http://site.com/img/bg_top.png it gives me a Missing Img controller error.
my understanding is that you can't access anything from "above" the webroot from a browser, hence the message.

Resources