Stylesheet not loaded because of MIME-type
HtmlCssGulpMime TypesBrowser SyncHtml Problem Overview
I'm working on a website that uses gulp
to compile and browser sync to keep the browser synchronised with my changes.
The gulp task compiles everything properly, but on the website, I'm unable to see any style, and the console shows this error message:
> Refused to apply style from > 'http://localhost:3000/assets/styles/custom-style.css'; because its > MIME type ('text/html') is not a supported stylesheet MIME type, and > strict MIME checking is enabled.
Now, I don't really understand why this happens.
The HTML includes the file like this (which I am pretty sure is correct):
<link rel="stylesheet" type="text/css" href="assets/styles/custom-style.css"/>
And the stylesheet is a merge between Bootstrap & font-awesome styles for now (nothing custom yet).
The path is correct as well, as this is the folder structure:
index.html
assets
|-styles
|-custom-style.css
But I keep getting the error.
What could it be? Is this something (maybe a setting?) for gulp/browsersync maybe?
Html Solutions
Solution 1 - Html
For Node.js applications, check your configuration:
app.use(express.static(__dirname + '/public'));
Notice that /public
does not have a forward slash at the end, so you will need to include it in your href option of your HTML:
href="/css/style.css">
If you did include a forward slash (/public/
) then you can just do href="css/style.css"
.
Solution 2 - Html
The issue, I think, was with a CSS library starting with comments.
While in development, I do not minify files and I don't remove comments. This meant that the stylesheet started with some comments, causing it to be seen as something different from CSS.
Removing the library and putting it into a vendor file (which is ALWAYS minified without comments) solved the issue.
Again, I'm not 100% sure this is a fix, but it's still a win for me as it works as expected now.
Solution 3 - Html
In most cases, this could be simply the CSS file path is wrong. So the web server returns status: 404
with some Not Found
content payload of html
type.
The browser follows this (wrong) path from <link rel="stylesheet" ...>
tag with the intention of applying CSS styles. But the returned content type contradicts so that it logs an error.
Solution 4 - Html
This error can also come up when you're not referring to your CSS file properly.
For example, if your link tag is
<link rel="stylesheet" href="styles.css">
but your CSS file is named style.css
(without the second s) then there is a good chance that you will see this error.
Solution 5 - Html
I had this error for a Bootstrap template.
<link href="starter-template.css" rel="stylesheet">
Then I removed the rel="stylesheet"
from the link, i.e.:
<link href="starter-template.css">
And everything works fine. Try this if you are using Bootstrap templates.
Solution 6 - Html
I have changed my href
to src
. So from this:
<link rel="stylesheet" href="dist/photoswipe.css">
to this:
<link rel="stylesheet" src="dist/photoswipe.css">
It worked. I don't know why, but it did the job.
Solution 7 - Html
Make a folder just below/above the style.css file as per the Angular structure and provide a link like <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
.
Solution 8 - Html
Comments in your file will trip this. Some minifiers will not remove comments.
ALSO
If you use Node.js and set your static files using express
such as:
app.use(express.static(__dirname + '/public'));
You need to properly address the files.
In my case both were the issue, so I prefixed my CSS links with "/css/styles.css".
Example:
<link type="text/css" rel="stylesheet" href='/css/styles.css">
This solution is perfect as the path is the main issue for CSS not getting rendering
Solution 9 - Html
In addition to using:
<base href="/">
Remove the rel="stylesheet"
part from your css links:
<link type="text/css" href="assets/styles/custom-style.css"/>
Solution 10 - Html
I simply referenced the CSS file (an Angular theme in my case) in the styles section of my Angular 6 build configuration in angular.json:
This does not answer the question, but it might be a suitable workaround, as it was for me.
Solution 11 - Html
I know it might be out of context but linking a non existed file might cause this issue as it happened to me before.
<!-- bootstrap grid -->
<link rel="stylesheet" href="./css/bootstrap-grid.css" />
If this file does not exist you will face that issue.
Solution 12 - Html
As mentioned solutions in this post, some of the solutions worked for me, but CSS does not apply on the page.
Simply, I just moved the "css" directory into the "Assest/" directory and everything works fine.
<link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="assets/css/site.css" >
Solution 13 - Html
The problem is that if you have a relative path, and you navigate to a nested page, that would resolve to the wrong path:
<link rel="stylesheet" href='./index.css'>
so the simple solution was to remove the .
since mine is a single-page application.
Like this:
<link rel="stylesheet" href='/index.css'>
so it always resolves to /index.css
There are a lot of answers to this question but none of them seem to really work. If you remove rel="stylesheet"
it will stop the errors but won't apply the stylesheets.
The real solution:
Just remove the .
it works then
Solution 14 - Html
Also for others using Angular-CLI and publishing to a sub-folder on the webserver, check this answer:
When you're deploying to a non-root path within a domain, you'll need to manually update the <base href="/">
tag in your dist/index.html.
In this case, you will need to update to <base href="/sub-folder/">
Solution 15 - Html
I got the same issue and then I checked that I wrote:
<base href="./">
in index.html
Then I changed to
<base href="/">
And then it worked fine.
Solution 16 - Html
I had this problem with a site I knew worked online when I moved it to localhost and PhpStorm.
This worked fine online:
<link rel="stylesheet" href="/css/additional.css">
But for localhost I needed to get rid of the slash:
<link rel="stylesheet" href="css/additional.css">
So I am reinforcing a few answers provided here already - it is likely to be a path or spelling mistake rather than any complicated server setup problem. The error in the console is a red herring; the network tab needs to be checked for the 404 first.
Among the answers provided here are a few solutions that are not correct. The addition of type="text/html"
or changing href
to src
is not the answer.
If you want to have all of the attributes so it validates on the pickiest of validators and your IDE then the media value should be provided and the rel
should be stylesheet
, e.g.:
<link rel="stylesheet" href="css/additional.css" type="text/css" media="all">
Solution 17 - Html
I have had the same problem.
If your project's structure is like the following tree:
index.html
assets
|-styles
|-custom-style.css
server
|- server.js
I recommend to add the following piece of code in server.js
:
var path = require('path')
var express = require('express')
var app = express()
app.use('/assets', express.static(path.join(__dirname, "../assets")));
Note: Path is a built-in Node.js module, so it doesn't need to install this package via npm.
Solution 18 - Html
You can open the Google Chrome tools, select the network tab, reload your page and find the file request of the CSS and look for what it have inside the file.
Maybe you did something wrong when you merged the two libraries in your file, including some characters or headers not properly for CSS?
Solution 19 - Html
Adding to a long list of answers, this issue also happened to me because I did not realize the path was wrong from a browser-sync point of view.
Given this simple folder structure:
package.json
app
|-index.html
|-styles
|-style.css
the href
attribute inside <link>
in index.html
has to be app/styles/style.css
and not styles/style.css
Solution 20 - Html
In case you using Express with no JS try with:
app.use(express.static('public'));
As an example, my CSS file is at public/stylesheets/app.css
Solution 21 - Html
How I solved this.
For Node.js applications, you need to set your **public**
folder configuration.
// Express js
app.use(express.static(__dirname + '/public'));
Otherwise, you need to do like href="public/css/style.css".
<link href="public/assets/css/custom.css">
<script src="public/assets/js/scripts.js"></script>
> Note: It will work for http://localhost:3000/public/assets/css/custom.css
. But couldn't work after build. You need to set app.use(express.static(__dirname + '/public'));
for Express
Solution 22 - Html
For a Node.js application, just use this after importing all the required modules in your server file:
app.use(express.static("."));
- express.static built-in middleware function in Express and this in your .html file: <
link rel="stylesheet" href="style.css">
Solution 23 - Html
by going into my browsers console > network > style.css ...clicked on it and it showed "cannot get /path/to/my/CSS", this told me my link was wrong. i changed that to the path of my CSS file.
Original path before change was localhost:3000/Example/public/style.css changing it to localhost:3000/style.css solved it.
if you are serving the file from app.use(express.static(path.join(__dirname, "public"))); or app.use(express.static("public")); your server would pass "that folder" to the browser so adding a "/yourCssName.css" link in your browser solves it
By adding other routes in your browser CSS link, you'd be telling the browser to search for the css in route specified.
in summary... check where your browser CSS link points to.
Solution 24 - Html
This is specific to Typescript+Express
I ctrl+f'd "Typescript" and ".ts" and found nothing in these answers, so I'll add my solution here, since it was caused by (my inexperience with) typescript, and the solutions I've read don't explicit solve this particular issue.
The problem was that Typescript was compiling my app.ts
file into a javascript file in my project's dist
directory, dist/app.js
Here's my directory structure, see if you can spot the problem:
├── app.ts
├── dist
│ ├── app.js
│ ├── app.js.map
│ └── js
│ ├── dbclient.js
│ ├── dbclient.js.map
│ ├── mutators.js
│ └── mutators.js.map
├── public
│ ├── css
│ │ └── styles.css
├── tsconfig.json
├── tslint.json
└── views
├── index.hbs
└── results.hbs
My problem is that in app.ts
, I was telling express to set my public directory as /public
, which would be a valid path if Node actually were running Typescript. But Node is running the compiled javascript, app.js
, which is in the dist
directory.
So having app.ts
pretend it's dist/app.js
solved my problem. Thus, I fixed the problem in app.ts
by changing
app.use(e.static(path.join(__dirname, "/public")));
to
app.use(e.static(path.join(__dirname, "../public")));
Solution 25 - Html
I was working with the React.js
app and also had this error which led me here. This is what helped me. Instead of adding <link>
to the index.html
I added an import
to the component where I need to use this stylesheet:
import 'path/to/stylesheet.css';
Solution 26 - Html
In my case, when I was deploying the package live, I had it out of the public HTML folder. It was for a reason.
But apparently a strict MIME type check has been activated, and I am not too sure if it's on my side or by the company I am hosting with.
But as soon as I moved the styling folder in the same directory as the index.php file I stopped getting the error, and styling was activated perfectly.
Solution 27 - Html
If you are setting Styles in JavaScript as:
var cssLink = document.createElement("link");
cssLink.href = "./content.component.scss";
cssLink.rel = "stylesheet";
--> cssLink.type = "html/css";
(iframe as HTMLIFrameElement).contentDocument.head.appendChild(cssLink);
Then just change cssLint.type (denoted by arrow in above description) to "MIME":
cssLink.type = "MIME";
It will help you to get rid of the error.
Solution 28 - Html
Remove rel="stylesheet" and add type="text/html". So it will look like this -
<link href="styles.css" type="text/html" />
Solution 29 - Html
Bootstrap styles not loading #3411
https://github.com/angular/angular-cli/issues/3411
-
I installed Bootstrap v. 3.3.7
npm install bootstrap --save
-
Then I added the needed script files to
apps[0].scripts
in the angular-cli.json file:"scripts": [ "../node_modules/bootstrap/dist/js/bootstrap.js" ], // And the Bootstrap CSS to the apps[0].styles array "styles": [ "styles.css", "../node_modules/bootstrap/dist/css/bootstrap.css" ],
-
I restarted ng serve
It worked for me.
Solution 30 - Html
if browser can not find related css file, it could give this error.
If you use Angular application you do not have to put css file path on index.html
<link href="xxx.css" rel="stylesheet"> -->
You could put related css file path on styles.css file.
@import "../node_modules/material-design-icons-iconfont/dist/material-design-icons.css";
Solution 31 - Html
https://github.com/froala/angular-froala/issues/170#issuecomment-386117678 Found the above solution of adding
href="/">
Just before the style tag in index.html
Solution 32 - Html
At times, this happens when the css file is not found. It's worth checking your base url / path to the file
Solution 33 - Html
One of the main reasons for the issue is the CSS file which is trying to load isn't a valid CSS file.
Causes:
- Invalid MIME type
- Having JavaScript code inside style sheet - (may occur due to incorrect Webpack bundler configuration)
Check the file which you're trying to load is a valid CSS style sheet (get the server URL of the file from the network tab and hit in a new tab and verify).
Useful info for consideration when using <link> inside the body tag.
Though having a link
tag inside the body is not the standard way to use the tag. But we can use it for page optimization (more information: https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery) / if the business use case demands (when you serve the body of the content and server configured to have to render the HTML page with content provided).
While keeping inside the body tag we have to add the attribute itemProperty
in the link
tag like
<body>
<!-- … -->
<link itemprop="url" href="http://en.wikipedia.org/wiki/The_Catcher_in_the_Rye" />
<!-- … -->
</body>`
For more information on itemProperty
have a look in https://webmasters.stackexchange.com/questions/55130/can-i-use-link-tags-in-the-body-of-an-html-document.
Solution 34 - Html
This issue happens when you're using cli tool for either reactjs or angular, so the key is to copy the entire final build from those tools since they initialize they're own lite servers which confuses your URLs with back end server you've created... take that whole build folder and dump it on asset folder of your back end server project and ref them from your back end server and not the server which ships with Angular or Reactjs Otherwise you're using it as front end from a certain API server
Solution 35 - Html
In my case I had to both make sure that the link was relative and the rel property was after the href property:
<link href="/assets/styles/iframe.css" rel="stylesheet">
Solution 36 - Html
i also had the same problem : here is the solution, don't write public in path of css link in your .html file. although your .css file present in the public folder.
example:- js file
app.use(express.static("public"));
html file
use this
<link href="styles.css" rel="stylesheet">
instead of
<link href="index/styles.css" rel="stylesheet">
Solution 37 - Html
The solution from this thread solved my problem:
> Not sure if this will help anyone, but if you are using angular-cli, I fixed this by removing the CSS reference from my index.html and adding it to the angular-cli.json file under the "style" portion. After restarting my webserver I no longer had that issue.
Solution 38 - Html
I came across this issue having the same problem adding a custom look and feel to an Azure B2C user flow. What I found was that the root that the html page referred to was ../oauth/v2 (i.e. the oauth server path) rather than the path to my storage bob.
Putting in the full url of the pages fixed the problem for me.
Solution 39 - Html
Check if you have a compression enabled or disabled. If you use it or someone enabled it then app.use(express.static(xxx))
won't help. Make sure your server allows for compression.
I started to see the similar error when I added Brotli and Compression Plugins to my Webpack. Then your server needs to support this type of content compression too.
If you are using Express then the following should help:
app.use(url, expressStaticGzip(dir, gzipOptions)
Module is called: express-static-gzip
My settings are:
const gzipOptions = {
enableBrotli: true,
customCompressions: [{
encodingName: 'deflate',
fileExtension: 'zz'
}],
orderPreference: ['br']
}
Solution 40 - Html
Nothing that everyone comments has helped me, the solution has been to fix the routes since from the main page I found the file but when I navigated it disappeared, the correct solution would say to have these things well placed
Solution 41 - Html
Triple check the name and path of the file. In my case I had something like this content in the target folder:
lib
foobar.bundle.js
foobr.css
And this link:
<link rel="stylesheet" href="lib/foobar.css">
I guess that the browser was trying to load the JavaScript file and complaining about its MIME type instead of giving me a file not found error.
Solution 42 - Html
I had this error, in Angular. The way I solved it was to put an ngIf on my link element so it didn't appear in the DOM until my dynamic URL was populated.
It may be unrelated to the OP a little bit, but I ended up here looking for an answer.
<link *ngIf="cssUrl" rel="stylesheet" type="text/css" [href]="sanitizer.bypassSecurityTrustResourceUrl(cssUrl)">
Solution 43 - Html
I met this issue.
I refused to apply the style from 'http://m.b2b-v2-pre1.jcloudec.com/mobile-dynamic-load-component-view/resource/js/resource/js/need/layer.css?2.0'; because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
Changing the path could solve this issue.
Solution 44 - Html
I had the same problem, and in my case it was due to the fact that I had manually tried to import (s)CSS files in the HTML file (like we always do with static websites), when in fact the files had to be imported in the entry point JavaScript file used by Webpack.
When the stylesheets were imported into the main JavaScript file, rather than manually written in the HTML, it all worked as expected.
Solution 45 - Html
May be You have authorization issue Buddy :
Try these steps :
- Go to ISS -> find your project listed -> Click on it -> Click on Edit permissions in right pane under actions
- you will see your project properties wizard . Click on Securities
- Under groups or user names -> If you see 'Authenticated users' , then you are authorized if not then you have to it .
- Once you add it ( your self or with the help of your administrator if you work in a company :)) , the website will start loading resources. You may need to restart your project under ISS.
Thanks
Solution 46 - Html
Yet another reason for this error may be that the CSS file permissions are incorrect. In my case the file was inaccessible because ownership had been changed to the root user-- which happened due to pushing Git files as the root user.
Solution 47 - Html
In my case it was the execution order of tasks ran by Grunt
.
It was executing the task connect
that sets up the local server and automatically opens the app in a new tab before executing less
and postcss
that transpile styles.
Basically, I changed this:
grunt.registerTask('default', 'Start server. Process styles.', ['connect', 'less', 'postcss']);
To this:
grunt.registerTask('default', 'Process styles. Start server.', ['less', 'postcss', 'connect']);
And it worked! Hope it helps other people too.
Solution 48 - Html
I faced similar error and found that the error was adding '/' at the end of style.css link href.
Replacing <link rel="stylesheet" href="style.css/">
to <link rel="stylesheet" href="style.css">
fixed the issue.
Solution 49 - Html
Incase, you're working with a Node application, make sure that the \public
folder is immediately below the root folder, and not within the views folder. This can become troublesome. Move the \public
immediately below the root and then restart the server and witness the changes.
Solution 50 - Html
I almost tried all given solutions, the problem for me was I had no MIME types option in IIS, that is I was missing this Windows feature.
The solution for me was:
> "And if you're on a non-server OS like Windows 8 or 10, do a search on the start page for "Turn Windows features on or off" and enable: Internet Information Services -> World Wide Web Services -> Common HTTP Features -> Static Content"
Enable IIS Static Content
Solution 51 - Html
I was facing the same problem.
I change my directories' permission to "755" now all the files are loading.
You can also try my answer. I hope this will work for you. If the answer works for you, don't hesitate to vote for the answer. Best of luck.
Solution 52 - Html
I faced this challenge with select2. It go resolved after I downloaded the latest version of the library and replaced the one (the css and js files) in my project.
Solution 53 - Html
In my case the problem was solved just after changing the random value in the .scss file. After reloading the problem disappeared and the styles began to load well. Simple, but works :P
Solution 54 - Html
I have used virtual domain in my XAMPP and got this issue. So in httpd-vshosts.conf file when I checked, I had explicitly pointed to index.php file and this had caused the issue.
So I changed this:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/cv/index.php"
ServerName cv.dv
</VirtualHost>
to this:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/cv/"
ServerName cv.dv
</VirtualHost>
and then the files were loaded without issues.
Solution 55 - Html
In my angular-ionic project I've a css entry like this for a component which would only load when I request.
.searchBar {
// --placeholder-color: white;
// --color: white;
// --icon-color: white;
// --border-radius: 20px;
// --background: color:rgba(73,76,67,1.0);
// --placeholder-opacity: 100%;
// background-color: red;
}
as soon as I commented out all the values inside the css class entry it started working again.
I think this was happening due to having these properties background-color
with --background
together.
Solution 56 - Html
I would like this share some thoughts on this topic, when I place app.use(expres.static('../frontEnd/public')) it don't work, but when I use app.use(express.static('/frontEnd/public')) it works fine, I hope this would help someone
Solution 57 - Html
I had the same problem and after hours of debugging I found out that it is related to temporary files not able to be written.
Go to Admin → Config → File system. Set the correct temporary directory. Make sure your server has permission to write to that directory.
Solution 58 - Html
Resaving a .ts file (forcing Ionic to rebuild) solved it for me.
It doesn't really make sense... but as long as it works, who am I to judge?
Here I have seen this workaround: https://stackoverflow.com/questions/48977389/ionic-angular2-refused-to-apply-style-from-http-localhost8100-build-main-c
Solution 59 - Html
I tried to restart my Windows machine and reinstalled the "npm i".
It worked for me.
Solution 60 - Html
Please also check for typos in your filename. Happened to me:
style.css.css
Solution 61 - Html
add express default static middleware as:
Import express const express = require("express")
initialize express const app = express()
app.use(express.static(__dirname));
then you need to use complete path of public directory
for example:
your file structure inside public directory
public > css > style.css
your path will be
<link rel="stylesheet" type="text/css" href="./public/css/style.css"/>
NOTE1:
you can use any name instead of public
i.e assets
myassets
any other
NOTE2:
YOUR SERVER SHOULD RUINING IN MAIN DIRECTORY
IF YOUR SERVER RUINING IN SUB DIRECTORY
THEN YOU CAN COME OUT BY USING ../
Solution 62 - Html
Faced the similar issue and solved it using this simple fix. If your project is React based then instead of importing your "styles.css" in "index.html", import it in "index.js" which generally resides inside "src" folder of your project. This will make sure that all your routes inside your React Application has access to the styles file.
Solution 63 - Html
I was facing same issue in ejs
& nodejs
I have set up my view engine public folder like this
app.use('/public', express.static(process.cwd() + '/public'));
app.set('view engine', 'ejs');
and my directory structure was like this
public
| -assets
| --CSS
|---style.css
I was trying to link my Style.css
in ejs
head like this
<link href="/assets/css/style.css" rel="stylesheet" type="text/css">
By Adding the public
solved the problem (Make Sure TO Check Network Tab)
<link href="/public/assets/css/style.css" rel="stylesheet" type="text/css">
Solution 64 - Html
You must have imported multiple stylesheets. try to remove one and try again