在我的硬盘上的同一目录中的 HTML 文档上链接 CSS 文件 (Windows)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26872201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:02:56  来源:igfitidea点击:

Linking CSS File on a HTML Document In Same Directory On my Hard Disk (Windows)

htmlcsswindows

提问by Sahriar Saikat

I am using Windows(7) trying to create an offline html page that should link to a separate CSS file located in same directory. I tried to use

我正在使用Windows(7) 尝试创建一个离线 html 页面,该页面应该链接到位于同一目录中的单独 CSS 文件。我试着用

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

but no luck.

但没有运气。

And I also want to import another css file named styles2.css within style.css. Didn't check yet but putting @import "style2.css";on style.css may be not work as well. I can use absolute links like C:\Users\ELITEBOOK\Desktop\offline\style.cssbut it won't work if I move the folder anywhere else from Desktop. Any help? I mean, any code that calls/adds the link of the folder?

而且我还想在 style.css 中导入另一个名为 styles2.css 的 css 文件。还没有检查,但穿上@import "style2.css";style.css 可能也不起作用。我可以使用绝对链接,C:\Users\ELITEBOOK\Desktop\offline\style.css但如果我将文件夹从桌面移动到其他任何位置,它将无法工作。有什么帮助吗?我的意思是,任何调用/添加文件夹链接的代码?

回答by Damilola Olowookere

Use <link rel="stylesheet" type="text/css" href="./styles.css" />instead. Note: href="/styles.css"changed to href="./styles.css", which is current directory of your script.

使用<link rel="stylesheet" type="text/css" href="./styles.css" />来代替。注意:href="/styles.css"更改为href="./styles.css",这是脚本的当前目录。

回答by iamkingsleyf

try this one, it worked for me.

试试这个,它对我有用。

 <link rel="stylesheet" href="./main.css">

回答by sina

A general complete answer:

一般完整的答案:

to address through existing folder, use:

要通过现有文件夹寻址,请使用:

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

to address through parent folder, use:

要通过父文件夹寻址,请使用:

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

to address through the internet( CDN ) use:

通过互联网(CDN)寻址使用:

<link
  rel="stylesheet"
  href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3./css/bootstrap-theme.min.css"
  >

to address through your hard drive use:

通过您的硬盘驱动器使用来解决:

<link rel="stylesheet" href='file:///C:/mystyles.css'>