【问题标题】:Why CSS File doesn't apply to the html in my project?为什么 CSS 文件不适用于我项目中的 html?
【发布时间】:2019-02-02 21:25:00
【问题描述】:

我不知道为什么外部 CSS 文件不适用于 html 文件。

我现在在 Cloud IDE(goormIDE - 它类似于 C9)上进行编程。我想使用 node.js 将外部 CSS 文件链接到 Web 服务器上的 HTML 文件。但它不适用于 HTML。我研究了很多,但无法解决问题。

它有 3 个主文件(index.html、style.css、main.js)并且它们在同一个文件夹中。

index.html

<!doctype html>
<html>
    <head>
        <title>YOONJONG</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css" type="text/css" >

    </head>
    <body>
    <a href="intro.html">INTRO</a>
    <a href="portfolio.html">PORTFOLIO</a>
    <a href="board.html">BOARD</a>
    <a href="contact.html">CONTACT</a>
    <p>blahblah</p>

    </body>
</html>

style.css

a {
    font-size: 36px;
    font-weight: 800;
}
p {
    font-size: 20px;
}

main.js

var http = require('http');
var fs = require('fs');
var app = http.createServer(function(request,response){
var url = request.url;
if(request.url == '/'){
  url = '/index.html';
}
if(request.url == '/favicon.ico'){
  response.writeHead(404);
    response.end();
    return;
}
response.writeHead(200);
response.end(fs.readFileSync(__dirname + url));

});
app.listen(80);

【问题讨论】:

  • 是style.css和index.html在同一个文件夹中,还是在子文件夹中?
  • @zack6849 — 问题是“它有 3 个主文件(index.html、style.css、main.js)并且它们在同一个文件夹中”
  • 我无法重现该问题。当我复制/粘贴您的代码并运行它时,样式就会显示出来。
  • 您的 style.css 是否在开发者工具中返回 404?你能在你的页面源代码中看到你的样式表吗?和 Quentin 一样,当我复制粘贴你的代码时它也可以工作。
  • @Quentin - 当我尝试在我的桌面上(不是在 Cloud IDE 上)制作 3 个文件时,它运行良好。

标签: html css node.js


【解决方案1】:

尝试改变:

<meta charset="utf-8"> <link rel="stylesheet" href="style.css" type="text/css" >

到:

<meta charset="utf-8" /> <link rel="stylesheet" href="style.css" type="text/css" />

【讨论】:

  • 如果他们使用 XHTML(他们不是)并且他们使用的浏览器不能从非格式良好的 XHTML 中恢复(我不这样做),这将解决黄屏死机错误'认为这些都不再是关于了)。
猜你喜欢
  • 2019-01-17
  • 1970-01-01
  • 2020-06-09
  • 2021-01-15
  • 1970-01-01
  • 2021-12-22
  • 2019-09-07
  • 2016-11-30
  • 2019-12-08
相关资源
最近更新 更多