Search code examples
javascriptcsshref

Include external css/js file into html page


I'm trying to link manually my CSS and Javascript files to my HTML scripts, but I've never really done it with files from a different folder/directory. This is what I have so far:

<link rel="stylesheet" type="text/css" href="/Desktop/Script/CSS/tempeststyle.css"/>
<link rel=script" type="text/javascript" href="/Desktop/Script/Javascript/tempestscript.js"/>

I've read from a few sources that you don't have to start all the way at C:/Users or whatever the case may be for different systems, but I'm not sure if these links are acceptable the way they are (i.e., them starting at "/Desktop"). The files are in separate folders within the same folder on the desktop. So how to include them the best way? Thanks.


Solution

  • If you want to add scripts from local path you can use a relative path:

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

    or relative path from your web root folder:

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

    or filesystem absolute path:

    <link rel="stylesheet" type="text/css" href="file:///C:/Path/To/Scripts/styles/CSS/tempeststyle.css"/>
    

    It is rather better if you use an absolute path from your web server:

    <link rel="stylesheet" type="text/css" href="http://www.example.com/styles/CSS/tempeststyle.css"/>