1

I want to call a css from my index.html, and the css is in a CSS folder, also there is an image in a images folder..

I have tried different ways but no luck

The directory looks like this

My_First_Website
  Javascript Resources
  WebContent
     css
        mystyles.css
     images
        mybackgroundImage.png
     index.html

Now mystyles.css looks like this

@CHARSET "ISO-8859-1";

body
{
  background-image:url('/WebContent/images/mybackgroundimage.png');
}

And my HTML page looks this

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="/WebContent/css/mystyles.css" media="screen" />

<title>My Website</title>
</head>
<body>

</body>
</html>

Right now the page shows empty :(. Please help :)

user710502
  • 11,181
  • 29
  • 106
  • 161
  • Simple relative paths problem. Google could've solved your problem, easy. – Nightfirecat Sep 04 '11 at 08:24
  • why the negative vote, this is a forum to ask questions no?, i searched and havent been able to make it work.. There are just some jerks who just want to make themselves look important... I am new doing this in eclipse and if i am asking is because i exhausted options – user710502 Sep 04 '11 at 08:29
  • 1
    I -1'd, because this is a very simple concept that doesn't only apply to HTML/CSS, and has been answered countless times around the internet, and [on StackOverflow as well](http://stackoverflow.com/questions/2718532/is-a-relative-path-in-a-css-file-relative-to-the-css-file). I don't want to sound mean, but this definitely feels like one of those things that is a beginner issue, from not spending the time to learn the language(s)'s basic features correctly, or fully. (I will admit, it wasn't easy to find that question, but the concept is a simple one, taught in any beginner HTML/CSS course/tut) – Nightfirecat Sep 04 '11 at 08:34
  • its 4:35 am,.. not spending the time, give me a break... you should just stay away from making assumptions... I can make this work in Visual studio no problem.. the problem is i cant make it work in eclipse.. so please stay away from this.. and if you already voted go away. I should give you a negative vote for unconstructive comments – user710502 Sep 04 '11 at 08:37
  • 2
    I'm merely explaining myself, like you asked. – Nightfirecat Sep 04 '11 at 08:38
  • there is no need thanks for your help – user710502 Sep 04 '11 at 08:39
  • Hey, I know you got a little discouraged from the negative responses, but I think my answer hits at what you need. More importantly: please don't let this discourage you from web development or SO! – attack Sep 10 '11 at 17:51

5 Answers5

6

In your background image and your stylesheet reference, you're using absolute paths (paths that begin with the forward slash). On a website, an absolute path instructs the browser to go looking for a resource at the root.

So let's imagine I have a website with the following structure:

index.html
css/
    screen.css
images/
    main-back.png
project/
    sample.html

And I'm adding the following HTML to project/sample.html:

<img src="/images/main-back.png" />

To find the image, the browser will first go to the root directory, then look for the images directory, and then look for main-back.png. Alternatively, you can use relative paths:

<img src="images/main-back.png" />

Without the forward slash, the browser will start in the project directory (where sample.html is located) and look in vain for an images folder. This will result in no image displaying. To fix it, we tell the browser to first navigate up a directory:

<img src="../images/main-back.png" />

This is basically the same thing as our first example, except we're using a relative path instead of an absolute path.

Now, the problem you are facing is that you're opening the page up on your own computer. In this case, there is no root web directory, so you'll need to use relative paths instead of absolute paths. So, for your stylesheet reference, you can use:

<link rel="stylesheet" type="text/css" href="css/mystyles.css" media="screen" />

Start from index.html, look for the css directory in the same directory, and then find mystyles.css within that directory.

For your CSS image reference, the key thing to remember is that paths within CSS files are relative to the CSS file itself. So you'll need the following:

background-image:url('../images/mybackgroundimage.png');

Start from mystyles.css, move up a directory, look for the images directory, and then find mybackgroundimage.png in that directory.

attack
  • 1,523
  • 12
  • 17
4

Change /WebContent/css/mystyles.css in your HTML file to /css/mystyles.css, and change /WebContent/images/mybackgroundimage.png in your CSS file to ../images/mybackgroundimage.png.

/WebContent under WebContent means My_First_Website/WebContent/WebContent, not My_First_Website/WebContent.

JiminP
  • 2,136
  • 19
  • 26
  • Thank you, I tried it and no luk.. is just weird not sure why its not seeing it. – user710502 Sep 04 '11 at 08:32
  • Thanks. Anyway, it's not very good thing to avoid people criticizing you. At least they have some reasons. What did you search when you had this problem? Tip : if you are using Google Chrome, you can use console(`Ctrl+Shift+I`) and look at Resource or Network tab to find out what's wrong. – JiminP Sep 04 '11 at 09:26
1

You're using absolute paths (paths starting with a '/'). Using relative paths might help.

Arnout Engelen
  • 6,709
  • 1
  • 25
  • 36
  • Thank you for trying to help.. and please if you ever see a beginner asking a question... keep helping.. this forums in not only for experts.. thank you a vote for your help – user710502 Sep 04 '11 at 08:42
0

This works:

    <img src="../images/picture.png" width="40%"/>

when you have:
WebContent/ and in the subdirectories:
html/index.html
images/picture.png

Laura Liparulo
  • 2,849
  • 26
  • 27
0

For me this worked:

.helpbg {
    background-image:url('../resources/images/aboutbg.jpg');
}

And the element, which is in a JSF and Bootstrap project:

<div class="container-fluid helpbg">
Eddy
  • 3,533
  • 13
  • 59
  • 89