html file does not identify CSS

Asked

Viewed 4,397 times

4

I went for a style on the page and HTML does not use the CSS file. First I put it like this:

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

And on the console gave this message:

Resource Interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:4000/main.css".

So I put: <link rel="stylesheet" type="text/css" href="main.css"> and kept on giving the same message.

I switched to <link rel="stylesheet" type="text/html" href="bootstrap.css"> and the message stopped from Dart on the console, but my Stylos don’t work, it’s like I haven’t called anything, and the files are in the same index folder. What can it be?

************ Updates ***********

As you requested, follow server code:

var http = require('http').createServer(server);
var fs = require('fs');

function server(req, response){
    response.writeHead(200);
    response.end(fs.readFileSync('view/index.html'));
};

http.listen(4000, function(){
  console.log("x------------------x");
  console.log("x Servidor On-line x");
  console.log("x------------------x");
});`
  • What are you using to serve the files? The problem is in the server configuration that "says" that the style file is CSS and not HTML.

  • So, I made a fast server with Node: var http = require('http'). createServer(server); var Fs = require('Fs'); Function server(req, Response){ Response.writeHead(200); Response.end(Fs.readFileSync('view/index.html'); }; http.Listen(4000, Function(){ console.log("x------------------x"); console.log("x Online Server x"); console.log("x------------------x"); });` ...

  • @Rafaelacordeiro, it is not the best option to make a server "fast" with the module http. You can choose to express or http-server. That being said, follow a "small" code to resolve your problem. https://stackoverflow.com/a/29046869/9101590

  • You can post your css file please if it’s not too big.

  • The problem then is clearly on the server you created. Use a development-ready server - or post your server code so it can be fixed: that’s where your specific problem is. (you can use the python language installed on your system to serve static files directly: python3 -m http.server 4000 )

3 answers

6

Your MIME type is wrong, is not type="text/html" is type="text/css"

So he doesn’t carry:

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

The right thing to do:

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

A tip is to use the DevTools of Chrome for example to check if the file .CSS correctly loaded. No Chrome open your document, press F12, goes in the Aba Network, make the CSS and after a F5

inserir a descrição da imagem aqui

  • So, I already did this. There I explained: "put: <link rel="stylesheet" type="text/css" href="main.css"> and kept on giving the same message. I switched to <link rel="stylesheet" type="text/html" href="bootstrap.css"> and the message stopped working on the console, but my styles don’t work.

  • I started a Node server and thought this was giving some conflict, I took the files (index and css’s) copied to an empty folder, opened in the browser and it worked. Is there something wrong with the server? var http = require('http').createServer(server);&#xA;var fs = require('fs');&#xA;&#xA;function server(req, response){&#xA; response.writeHead(200);&#xA; response.end(fs.readFileSync('view/index.html'));&#xA;};&#xA;&#xA;http.listen(4000, function(){&#xA; console.log("x------------------x");&#xA; console.log("x Servidor On-line x");&#xA; console.log("x------------------x");&#xA;});

  • Maybe your server doesn’t recognize this MIME type... Have you checked if CSS is appearing in the Network tab? From Node I don’t understand anything...

  • Really, it was careless, was without type,already added and is appearing on the network correctly, but does not work the style anyway.

  • I saw that this problem has a lot of tendency to be server related problem. I found a similar case on ONLY, take a look...

  • @Andréfilipe cool the reference seems that there are even some tricks to try to solve using / in the path, or #. But then I’ll leave it to the author of the question to analyze... What’s interesting is that the other answer you give here is basically a plagiarism, the guy translated the whole text in hard face and didn’t even quote the source...

  • 1

    @hugocsl, was the first thing I noticed when looking at the answers from here. Unfortunately there are still people who want to take some advantage, but do not know that citing the source is as useful as expanding knowledge.

  • @Andréfilipe and to make matters worse I gave the tip to the guy, he saw, commented that it was lack of attention on his part and yet did not put the source.... You’ll understand... :/

  • 1

    In addition to alerting you advised on the fact. I believe he is already aware of it. I hope very much that you are layperson!

Show 4 more comments

0

Opa so you can load a css file from a directory on Node.js you first need to determine the static folder where these files are, what you can do is the following:

  • Create a folder at the root of the project called public
  • Inside this public folder you place the folder with your css files

Dai in your app.js will put the following command if you are using express

app.use('/public', express.static('public'));

And inside your index html file instead of staying:

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

Will stay:

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

I hope it helps

  • And I solved the case, but I didn’t use express, I used the angular http-server, and it worked. I really needed to add a server. I’m still learning Angular, so I’m in search of seeing the best way yet. Thank you :)

-1

Let’s start with the explanation:

Browsers make HTTP requests for servers. The server then makes an HTTP response.

Both requests and responses consist of multiple headers and a body (sometimes optional) with some content.

If there is a body, then one of the headings is the Content-Type what describes the body (is it an HTML document? An image? The content of a form submission? Etc.).

When you request your stylesheet, your server is telling the browser that it is an HTML document (Content-Type: text/html) instead of a style sheet (Content-Type: text/css).

Change of Content-Type: text/html:

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

for:

<link rel="stylesheet" type="text/css" href="bootstrap.css">
  • So, I already did that. There I explained: "put: <link rel="stylesheet" type="text/css" href="main.css"> and kept on giving the same message. I switched to <link rel="stylesheet" type="text/html" href="bootstrap.css"> and the message stopped working on the console, but my styles don’t work.

  • I started a Node server and thought this was giving some conflict, I took the files (index and css’s) copied to an empty folder, opened in the browser and it worked. Is there something wrong with the server? var http = require('http').createServer(server);&#xA;var fs = require('fs');&#xA;&#xA;function server(req, response){&#xA; response.writeHead(200);&#xA; response.end(fs.readFileSync('view/index.html'));&#xA;};&#xA;&#xA;http.listen(4000, function(){&#xA; console.log("x------------------x");&#xA; console.log("x Servidor On-line x");&#xA; console.log("x------------------x");&#xA;});

  • 1

    Friend when you copy the answer from Stackoverflow in English is interesting you at least quote the source. Getting an answer from there is no problem, what you might consider to be ethical or not is quoting the source from where you got the answer! It’s not a personal attack ok, it’s just a beauty tip. [s]

  • Beauty! It was the lack of attention. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.