首页 文章

CSS与url一起正常工作,不适用于本地

提问于
浏览
-2

我有这个奇怪的问题,当我把这个.css文件的链接放到我的html的头部时,页面工作正常,从该在线.css文件中获取样式 . 但是,当我复制该.css文件的整个内容并将其保存在我的资源中时,它不会获得样式并且不起作用 . 有没有人能解决这种奇怪的行为?

这个工作正常:

<link rel =“stylesheet”type =“text / css”href =“https://www.example.com/global.css”>

这个没有:

<link rel =“stylesheet”type =“text / css”href =“resources / css / global.css”>

这是我包含的另一个css文件,并且在相同的路径目录下工作正常:

<link rel =“stylesheet”href =“resources / css / scrolling.css”type =“text / css”>

3 回答

  • 0

    你必须确定css文件的正确位置是正确的位置尝试添加/之前的href如下:

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

    同一时间如果文件夹中的位置必须添加两个这样的点

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

    i suggest you to be sure first if the location of css file is correct

  • 1

    我想你的结构是这样的:

    - main-folder
      - resources
        - css
          global.css
      index.html
    

    所以如果你想从 index.html 引用 global.css ,最好像这样编写你的 link 标签:

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

    我的意思是把 ./ 放在 href 的开头,你告诉浏览器 ***file is located in here (dot), after that, go to resources and finally in the css folder there is a global.css file ***

  • 1

    在路径前添加 / . 如果路径的开头没有斜杠,它将继承URL的路径,并附加文件的路径 . 添加斜杠使它成为"absolute"

    你希望它是:

    /resources/css/global.css
    

    该路径应基于您的根目录 .

相关问题