0

I have an issue that all beginners in CSS seems to have, my background-image doesn't show.
When I try to put background-color it works, and with image nothing shows-up. I have put my image in the same folder than my files css and html. I want to set the image as the background of my <body>. I use the framework meteor.

Here is the code HTML:

<body> 
  <div class="Units" id="UnitsSelector">
    {{>units}}
  </div>
  <h1>Test </h1>
</body>

Here is a part of the CSS file:

body {
 display: block;
 width: 900px;
 margin: auto;
 color: black;
 background-image:url('space.jpg');
}

Also when I use the inspect tool on my browser and I try to open my url I have this error :

Oops, looks like there's no route on the client or the server for url: "http://localhost:3000/space.jpg."

2 Answers2

1

The best approach I see here, is similar to these question: How to serve static content (images, fonts etc.) using iron router

Although you haven't stated that you're using iron router. The answer will be the same.

Instead of doing all this, you can just put the files under your public directory. If you add the file:

app/public/images/space.jpg

You can access it from your templates like:

<img src="/images/space.jpg">

No routes are needed to make that work.

Beware of overlooking the lead slash.

<img src="images/space.jpg">

The above example will work from your top level routes.

Andrew Ice
  • 831
  • 4
  • 16
-1
  1. Open devtools in Chrome
  2. Click to "console"
  3. You can see error space.jpg not found
  4. See link on error to your image
  5. Search difference between link in error and real place your image (solution 1)
  6. If all good - play with width/heigh/background-position (solution 2)
Princeair
  • 19
  • 5
  • I have no error in the console. And I already tried the solution 2. The path is good, and when I try with other images it shows the same error. – Dimitri Sergeant Feb 27 '18 at 11:13