Home > other >  "CSS background-image: url()" cant find an image that is present with "HTML img src=&
"CSS background-image: url()" cant find an image that is present with "HTML img src=&

Time:03-04

I would like to set a background image to a div using CSS.
While I can display the image while using HTML <img src="/static/img/fon1.jpg">...
I get a 404 (Not Found) when I try to set the image through CSS: style="background-image: url(/static/img/fon1.jpg)"\

  • I have tried different paths like: ../img/fon1.jpg and many others. Also with and withoud quotes.
  • I am certain that the css and the html are linked correctly as I can set the background to a solid color.
  • If I use in html, the css elements find and display the picture as well.

I feel like I should resort to just using <img> and rescaling it to fit the window, but I'd like to know what's wrong with the sucker. Any kind of help / materials / links / jokes are welcome. Thank you if you take your time. All the best!

Folder structure:

project
└─static
  └─ img
  |    fon1.jpg
  └─ stylesheet
  |    styles.css
  └─ base.html

HTML

    <link rel="stylesheet" type="text/css" href="/static/stylesheet/styles.css">
...
<body>
<div ></div>
<div  style="background-image: url('/static/img/fon1.jpg')"></div>
</body>
...

CSS

.background1 {
    background-image: url(../img/fon1.jpg);
    height: 960px;
    width: 1280px;
}
.background2 {
    background: red;
    height: 960px;
    width: 1280px;
}

CodePudding user response:

You missed the quotes in your code.

.background1 {
  background-image: url('../img/fon1.jpg');
  height: 960px;
  width: 1280px;
}

CodePudding user response:

I don't know from which file you are trying to access the image. I am assuming, You are trying from base.html

you are able to add the image like this.

<img src="./img/fon1.jpg">
  • Related