I have a css folder at the root of my Java Web Application. My import statement looks like this:
<link rel="stylesheet" type="text/css" href="/css/styles.css"/>
The style is not being applied, so I assume that the path to the css directory is not being specified correctly. How do I specify that the css directory is at the root of the Project folder?
My project folder contains:
build
css
dist
nbproject
src
web
build.xml
The html page that I am viewing is index.html, and the URL shown is localhost:8080/ServletApp/
Absolute:
The browser will always interpret /
as the root of the hostname. For example, if my site was http://google.com/
and I specified /css/images.css
then it would search for that at http://google.com/css/images.css
. If your project root was actually at /myproject/
it would not find the css file. Therefore, you need to determine where your project folder root is relative to the hostname, and specify that in your href
notation.
Relative: If you want to reference something you know is in the same path on the url - that is, if it is in the same folder, for example http://mysite.com/myUrlPath/index.html
and http://mysite.com/myUrlPath/css/style.css
, and you know that it will always be this way, you can go against convention and specify a relative path by not putting a leading /
in front of your path, for example, css/style.css
.
Filesystem Notations: Additionally, you can use standard filesystem notations like ..
. If you do http://google.com/images/../images/../images/myImage.png
it would be the same as http://google.com/images/myImage.png
. If you want to reference something that is one directory up from your file, use ../myFile.css
.
In your case, you have two options:
<link rel="stylesheet" type="text/css" href="/ServletApp/css/styles.css"/>
<link rel="stylesheet" type="text/css" href="css/styles.css"/>
The first will be more concrete and compatible if you move things around, however if you are planning to keep the file in the same location, and you are planning to remove the /ServletApp/ part of the URL, then the second solution is better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With