2

I have a HTML file and a CSS file located in different directories in my PC. in the head tag I use a link tag to reference my css file

<link href="C:\Users\UserName\Desktop\Web_testing\BGsample SS.css"
  type="text/css" rel="stylesheet" />

this doesn't work

4
  • 1
    Please give us some code. Commented Jun 22, 2017 at 8:26
  • You dont need a whole path just go from the point where your html file is. To go back in the path use ../ Commented Jun 22, 2017 at 8:27
  • may be due to space in css name. try to rename css file as <link href="C:\Users\UserName\Desktop\Web_testing\BGsampleSS.css" type="text/css" rel="stylesheet" /> Commented Jun 22, 2017 at 8:28
  • If you have little Problems its also always good to look at w3schools. There are a lot of good informations. Here the link for how to use the Link tag: w3schools.com/tags/tag_link.asp Commented Jun 22, 2017 at 8:33

4 Answers 4

3

Check your paths, Best practices is to always use a relative paths. Ex

If your stylesheet is called style.css the link should be:

<link rel="stylesheet" type="text/css" href="style.css" />

If you have the css file in a subdirectoy (and the subdirectory is called styledirectory) the link becomes:

<link rel="stylesheet" type="text/css" href="styledirectory/style.css" />

If the css file is in the parent directory of the html file the links becomes:

<link rel="stylesheet" type="text/css" href="../style.css" />

.. goes up 1 directory, if you need two you could do: ../../

Sample Folder structure

enter image description here

Assuming structure is like that then,

<link rel="stylesheet" type="text/css" href="styles/BGsampleSS.css" />

Name files without spaces too.

Sign up to request clarification or add additional context in comments.

Comments

2

try using the file:/// protocol.

href='file:///C:\Users\UserName\Desktop\Web_testing\BGsample SS.css'

If that doesn't work (and you say it doesnt), then my guess is that it won't work at all due to it being prevented by security features in the browser. It depends on where you are loading the main HTML content from, but if the main HTML is loaded from the internet then I can understand why the browser might object to loading the CSS from the client machine's local file system.

One final thing to try: You might try setting up a web server on your machine, putting the mystyles.css file into the web folder, and loading it into the page using:

href='http://localhost/mystyles.css'

I can't really suggest much else, I don't think.

Comments

0

You should be using '../' to move back in your path, and then into the directory with the CSS file.

Comments

0

Please always follow the CSS External Style Sheet way. In your case the CSS file cannot be located. Try adding \ before SS.css

<link rel="stylesheet" type="text/css" href="C:\Users\UserName\Desktop\Web_testing\BGsample\SS.css">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.