r/HTML • u/Extra_Terrestrial2 • Aug 17 '26
Question Why doesn’t my HTML background work?
body {
background-image: url('background.jpeg');
background-size: auto;
The image is “background.jpeg”. It has the correct spelling, the image is within the same folder. IT used to work but then it suddenly stopped working and i havent even edited the CSS for it to stop working.
Update: i couldnt figure it out for the life of me and no suggestions worked, but on the good side, instead of putting it on CSS, i put it on HTML, then it worked.
<style>
body {
background-image: url('background.jpeg');
background-size: auto;
}
</style>
3
u/scritchz Aug 17 '26
The image must be within the same folder like the HTML. You didn't specify to which other file it is in the same folder, so I'm just making sure. But it would be better if we could see your project structure.
What are the dimensions of the image?
Do other parts of your stylesheet work?
1
u/MineDesperate8982 Aug 17 '26
Open the html page in the browser, inspect and see if the image loads. It usually helps you figure out what's wrong with it - most probably it doesn't like something about the path.
1
u/BazuzuDear Aug 18 '26
It's not the image, it's the CSS file then. Make sure it is included, not misspelled, and readable.
1
u/alphex Aug 21 '26
can you make a codepen.io to show us your code?
0
u/notepad987 Aug 18 '26 edited Aug 18 '26
0
u/notepad987 Aug 18 '26 edited Aug 18 '26
Either with added code seen below:
-4
u/hanssp Aug 17 '26
Rename the file to background.jpg both real and in the code, sounds stupid, but have seen this sometimes works
-1
u/Odd_Philosopher1741 Aug 17 '26
A body by default (if there is nothing else inside it) has a height of 0 pixels. Try to enlarge the body to be the same size as your viewport (browser screen).
Try this:
The BODY element automatically grows vertically whenever you put content in it. This is the default behavior of a "block" element (display: block, which a BODY element is by default).
-2
u/TabbbyWright Aug 17 '26
Ok personally I would put it back in the CSS, and then check and see if the image loads in another browser.
If you're using Firefox or Chrome, right click -> inspect and if you go to I think the network tab, it'll show you all the assets that the browser tried to download. You'll see error numbers too, like 404 for image not found.
Even if you're happy with your current solution, you might find these suggestions useful in the future!