Spring add CSS to different pages - html

I have 2 pages:
http://local.host:8080/test/login
http://local.host:8080/test/admin/manage
Added css in login.jsp using c:url:
<c:url value="css/style.css"/>
When I open 1st link - everything works well. I tried to add the same style.css file to manage.jsp (2nd URL), but when I open 2nd link - css is not included. In Page Source I have:
<link rel="stylesheet" type="text/css" href="css/style.css"/>
How to define to take style.css from the root of URL (http://local.host:8080/test) ?

I think it is, because the specified path is relative to the current page (login is at an other level of path-nesting* than admin/manage).
A trivial but bad solution would be adding ../ for the css of admin/manage. But this soultion has the drawback, that you always need to adjust the ../ when you change the level of path-nesting* for an page.
To overcome this problem, make the path in the c:url-tag start with an /! (this make the tag aware that the url is context relative (relative to http://local.host:8080/test/), and the tag will automatically addhttp://local.host:8080/test` in front.
<c:url value="/css/style.css"/>
will be rendered to : http://local.host:8080/test/css/style.css
For the link use this way
<c:url value="/css/style.css" var="cssUrl"/>
<link rel="stylesheet" type="text/css" href="${cssUrl}"/>
*I do not know the correct term for "path nesting" at the moment - fell free to change it

Order of attribute might be the problem. Correct the order
<link href="css/style.css" rel="stylesheet" type="text/css" />

Related

Problems in Link tag

hii all i am currently learning external css so i used link tag which helps me out with external css. So basically guys the format of link tag which it shows to me in atom is
<link rel="stylesheet" href="/css/master.css">
so the first slash in href tells about root which i don't know in detail as have not yet studied js. So i have removed it. to get the external css correctly i have done all the correct steps regarding it but the problem arises here....
<link rel="stylesheet" href="css/style.css">
( i applied this due to that i am not getting desired out put )..
So done some research and i came to point that by appling this layout of linktag i am getting desired out put
<link rel="stylesheet" href="C:\Users\KUSH\Desktop\UDEMY WEEB DEVELOPMENT 2022\css\styles.css">
this is the path of my external css folder.
but my prof is using this 👇
<link rel="stylesheet" href="css/style.css">
and she is getting desired outputs
so pls help me out with that.....
Try this:
<link rel="stylesheet" href="./css/style.css">
the ./ means that the path start from the current folder where your Html file is located
You need to put your HTML file and CSS file in same folder which make your work easierhere is the example if you I am not understandable

Cannot link CSS to HTML

Please help to check why I cannot link club.css to index.html.
index.html (Please note: I have to use the direct local path as the CSS link, or it will show that "some content has been disabled in this document.")
<head>
    <meta charset="utf-8">
    <title>The California Corgi Dog Club</title>
    <link rel="stylesheet" type="text/css" href="C:\Users\leeson\Desktop\CS651 Web Systems\assignment\assignmentfall2021-Rijutady\clubProject\club.css" />
</head>
club.css (Please note that the css file is under Encoding utf-8)
h1 {
    background-color: skyblue
}
The Simple Answer
Using a relative path should resolve your issue for most situations.
<link rel="stylesheet" type="text/css" href="./club.css"/>
The Alternative
You could also include the CSS directly into the HTML document, however this is not always ideal or recommended. Here's an article on that.
<style>
h1 {
background-color: skyblue
}
</style>
#NullPointDev's answer is correct. Use Relative path to your projects. It will make your work easier without errors. I just put some additional information for make your project easier.
If both HTML and CSS files in same directory, You can link your CSS like this,
<link rel="stylesheet" type="text/css" href="club.css"/>
If the CSS file is located in another directory in the directory which the HTML file located, you can use this method.
<link rel="stylesheet" type="text/css" href="name_of_the_subdirectory/club.css"/>
If the CSS file located in out of the directory which the HTML file located, Use ../ to go back.
<link rel="stylesheet" type="text/css" href="../club.css"/>
Read this Article for more info. Relative Path | W3 Schools
Now you know all the basics of Relative path. Wish you all the best.
no ,
it will be <link rel="stylesheet" type="text/css" href="club.css" />
if its under the clubproject
The solution code for your question is:
<link rel="stylesheet" type ="text/css" href = "club.css">
Your mistake : You have written the complete folder path instead of relative file path.
Advice: Have a look to the below link to clear concept.
https://www.w3schools.com/html/html_filepaths.asp
I prefer simple solution which is using relative path.
club.css
<link rel="stylesheet" type="text/css" href=".\club.css" />
Where is the problem:
URLs cannot contain spaces

css not working properly in masterpages

I'm having trouble setting up the href of my CSS.
My projected is located at
www.mysite.com/myproject/
My css is located at
www.mysite.com/myproject/styles/css/css.css
When I'm at www.mysite.com/myproject/, everything works fine. but when i go into a directory, (www.mysite.com/myproject/dir1/) the css is no longer found. My guess is that it's looking for the css at www.mysite.com/myproject/dir1/styles/css/css.css.
I'm currently using master pages. How do I properly reference the css?
Edit:
This is currently how i reference my CSS.
link rel="stylesheet" type="text/css" href="/content/css/bootmetro.css"
but it doesn't work properly because the project is not located at the root (www.mysite.com). it is located at www.mysite.com/myproject/. So having the "/" causes the css not to load at all.
Set the css path like this, starting slash means root of the site.
/styles/css/css.css
Have you tried:
link rel="stylesheet" type="text/css" href="../styles/css/bootmetro.css"
Assuming you are using ASP.NET, try like this:
<link rel="Stylesheet" type="text/css" href="<%= ResolveURL("~/content/css/bootmetro.css")%>" />

Specify a Root Path of your HTML directory for script links?

I'm writing a template for dreamweaver, and don't want to change the scripts for subfolder pages.
Is there a way to make the path relative to the root directory?
for example:
<link type="text/css" rel="stylesheet" href="**root**/style.css" />
Instead of **root** above, I want a default path there. Is there any way to do anything like this?
To be relative to the root directory, just start the URI with a /
<link type="text/css" rel="stylesheet" href="/style.css" />
<script src="/script.js" type="text/javascript"></script>
I recommend using the HTML <base> element:
<head>
<base href="http://www.example.com/default/">
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
In this example, the stylesheet is located in http://www.example.com/default/style.css, the script in http://www.example.com/default/script.js. The advantage of <base> over / is that it is more flexible. Your whole website can be located in a subdirectory of a domain, and you can easily alter the default directory of your website.
/ means the root of the current drive;
./ means the current directory;
../ means the parent of the current directory.
Just start it with a slash? This means root. As long as you're testing on a web server (e.g. localhost) and not a file system (e.g. C:) then that should be all you need to do.
This is oddly confusing to me. I know it shouldn't be. To check my understanding, I'd like to use a family relations model to compare. Assuming "You" is the current webpage, is the following correct?
<img src="picture.jpg"> In your folder with you, like a sibling
<img src="images/picture.jpg"> In your child's folder, under you
<img src="../picture.jpg"> In your parent's folder, above you
<img src="/images/picture.jpg"> In your cousin's folder
So, up to parent, over to sibling, down to their child = your cousin, named "images".
As Alexander Jank mentioned <base href="http://www.example.com/default/"> is great. When using sub-domains e.g. default.example.com base works great, because the JS and CSS loads from the said sub-domain and is accessible to both default.example.com and example.com/default
When using the root path, and your JS and CSS files are located in example.com/css, or example.com/js, then the subdomain has no access and the root of the subdomain is not accessible, except using the base.
Use two periods before /, example:
../style.css
You can use ResolveUrl
<link type="text/css" rel="stylesheet" href="<%=Page.ResolveUrl("~/Content/table-sorter.css")%>" />

CSS file not linking in sub-directory

Here's my directory structure:
-root
--docs
---doc1.php
--includes
---header.php
---footer.php
---css.css
--index.php
In my header, I link to my CSS file like so:
<link href="includes/styling.css" type="text/css" rel="stylesheet" />
That works for index.php, because it's the correct path (root/includes/css.css).
But for doc1.php, it's not the right path. (root/docs/includes/css.css).
How do I fix this while keeping one header.php file with that line of code in it? Is there a way to force the path to start in the root directory?
Use an absolute instead of a relative path.
<link rel="stylesheet" type="text/css" href="/root/includes/css.css" />
You can use the base tag to tell the browser where all links are relative to (including s and s), you will need to make sure all your links are relative to that, but it will allow you to use the same relative path from any document.
Or, you could use an absolute path.
Or this will remove all doubt for any page, at any level:
<link rel="stylesheet" type="text/css" href="http://www.mysite.com/includes/css.css" />