CSS Background Image Not Populating - html

Having trouble finding the relative path to my image in relation to my CSS folder. Folder structure for image, CSS, and html is below (absolute path shown).
CSS - C:\Users\user\mpi\personal\static\personal\css\home.css
Image location - C:\Users\user\mpi\personal\static\personal\img\ex.jpg
HTML - CSS - C:\Users\user\mpi\personal\templates\personal\home.html
body {
height: 100%;
margin: 0;
padding: 0;
background-image:url(img/Ex.jpg);
}
The above code will not produce my background image. What's the correct relative path to get the background image to populate? I can't even get the absolute file path to populate the image correctly. I've looked at other question and answers with no luck. Is the image location supposed to be relative to the CSS path, the HTML path, or both?

When you have CSS in an external file all paths within that CSS use the relative path of the external file.
So when you have...
background-image:url(img/Ex.jpg);
It is effectively looking in...
background-image:url(css/img/Ex.jpg);
Instead, use the following...
background-image:url(../img/Ex.jpg);

You have to write background image's path as if you are in the folder of the css file included, because it's like you are in C:\Users\user\mpi\personal\static\personal\css\ folder.
So in this case you have to write background:url(../img/ex.jpg);
Moreover i see that image name doesn't correspond; in your css class you have written Ex.jpg and in the complete image path you've written ex.jpg. Css detect uppercase and lowercase letters.

Just do this
Image
background-image:url(img/Ex.jpg);
Or
Starting with ../../ moves two directories
background-image:url(../../Ex.jpg);
Css
background-image:url(css/Ex.jpg);
Or
Starting with ../ moves one directory
background-image:url(../Ex.jpg);

Related

background image not loading in external CSS ( relative path used)

what the CSS file looks like:
h1{
.....
}
body{
background-image: url(/img/back.png) ;
}
what the files hierarchy looks like:
img(file containing the images)
-back.png(the background image i want to keep as background)
.css
.html
i am not sure why the image is not loading even though i have used relative path to the css file as suggested by the asnwers in past questions similar to this. if anyone can help thank you.
The rest of CSS is working just a problem with this background-image issue

why does my background image doesn't show?

I have a problem which is my background image doesn't show up. I am sure that my url is correct (Because I have used this url in other aspects).
This is my css file:
body{
background : url(assets/image/main.jpg);
background-size: cover;
}
so what happened?
This is my folder's structure:
/assets/
css/
style.css
image/
main.jpg
/index.html
Remove the space between background : and add quotes to your URL string so it looks like background: url("assets/image/main.jpg");
Beyond that just ensure that the filepath from this css file still maps to that folder correctly, just because it works in one css file doesn't mean it works in another if the folder structure differs.
Your code is correct.
You should know that the path is relative to the location of the style (and not the html).
The fact of having used this code elsewhere does not mean that the path is correct here.
After that may be the image which is corrupt.

CSS relative path to image

I am trying to set a DIV's background with an image in "Images" folder using CSS.
What should be the relative path to my image in CSS. My site's directory structure is as shown in this image
I tried:
background: url("/Images/img1.jpg");
But this didn't work.
Any help would be greatly appreciated.
Thanks.
I believe, when setting path of images in a CSS file, you can :
1 - use absolute path :
http://yourdomain.ext/Images/img1.jpg
2 - Use relative path :
BUT in that case, the path should be relative to the CSS file position.
In your case:
../../../Images/img1.jpg

How do I display an image via CSS in my local environment?

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?
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.)
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;
}
Try changing this to
background-image: url("images/logo.png") no-repeat;
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

Background image referenced in external css is not loading

I have this in an external css file, but its not working at all. I would assume I'd written it wrong, but when placed internally, it works fine. Any ideas as to what might be going on?
html
{
background-image:url(images/background-grid.jpg);
background-repeat:repeat;
}
I assume your CSS file is in a folder like this.
/
css/
yourcss.css
images/
background-grid.jpg
index.html
The paths given in your CSS file are relative to the path of the CSS file (in the example I given, the css folder). So you should use ..:
background-image: url(../images/background-grid.jpg);
I think you didn't write it completely wrong
But it's better to use body instead of html.
Explanation why to use body
It allows you to use an overlay on top of that body tag. Like a grid-ish background on the body and a shade on the side. But both are correct. Depending on what you are trying to do ofc.
If you don't repeat your background there is a possibility that your picture doesn't use the whole page and then you should use the html tag. But in this case it gives the same solution because of that repeat.
SO replay: tnx to attronics
Explanation of your 'error'
If your images are in a different folder than your html page (which should be the case).
You should use .. as relative path to your css file.
Without the .. it would mean that you are going to look for that image in the same folder as your html page.
body{
background-image:url(../images/background-grid.jpg);
background-repeat:repeat;
}
Here is a site that gives some Basics of CSS. Didn't check the source though.
It may be your folder structure, try
html
{
background-image:url(../images/background-grid.jpg);
background-repeat:repeat;
}
When we have these folders
css
img
and index.html
when we write css in style tag in index page which is called internal css then it works right but when we right css in styles.css in css folder then background img does not work.
because you move with in folder and path of background img does not work. so you have to write like this:
background-image: url('../img/bg.jpg');
then it will work properly
.. this mean go one level up.
So, if your folder structure is like this:
image/
background.png
css/
stylesheet.css
then your css rule should be like this:
html {
background-image: url('../image/background.png');
}
If your folder structure is like this:
style/
image/
bgr/
background.png
css/
deeper/
stylesheet.css
then, go up two levels to the image directory like below:
html {
background-image: url('../../image/bgr/background.png');
}
And so on. Just add up the .. followed by trailing slash /.