39

I've got a repository on github with a .css file in it. Is there any way to have github serve this file in a way that I can consume it in a web page?

In other words, I'd like to be able to reference this source file at github directly, from an HTML file on my local computer or a live domain. Something like:

<link rel="stylesheet"
      type="text/css"
      href="http://github.com/foouser/barproject/master/xenu-is-my-lover.css"
/>

I've tried including a<link> to the "raw" source file (http://raw.github.com...), but github serves its Content-Type as text/plain, and consequently, Chrome and FF are not adding its content as CSS styles to the page—the file's data is being discarded and a warning is shown in the debugger consoles of the browsers.

2
  • 2
    possible duplicate of Link and execute external JavaScript file hosted on GitHub Commented Oct 24, 2014 at 20:43
  • (try this) You can create a folder/file.css and serve it from GitHub pages. Just follow the correct path from the URL and get the file when referencing it from your local html file. Commented Nov 27, 2021 at 18:00

5 Answers 5

66

Important: rawgit.com is shutting down. Read more about other alternatives here - https://rawgit.com/


Found something really cool. You get the raw link as: http://raw.github.com/...

Simply fetch the files from rawgit.com (or cdn.rawgit.com) instead of raw.github.com and DONE!


UPDATE:

You can also use raw.githack.com if you want to serves raw files directly from Bitbucket or GitLab

Sign up to request clarification or add additional context in comments.

4 Comments

Just what I needed to serve the proper MIME type!
Keep in mind that you should not use rawgithub for production code.
Mind Blown. Private gists and repo's don't work. just as an FYI
JsDelivr does this apparently! Example: cdn.jsdelivr.net/gh/StylishThemes/Syntax-Themes/pygments/… from this GitHub file. This service seems to have been around for a long time, and widely used.
16

GitHub repos aren't web hosting, you should push that stuff up to a service specifically designed to serve files, like pages.github.com.

1 Comment

I don't think this is true. You can host static files on github, granted web hosting is better but it can work on github pages.
9

Check out https://gitcdn.link/ . Seems to work well.

Rawgit.com has shut down.

2 Comments

Does not like this works either; there is a front page, but About etc do not resolve ("Cannot GET /about"), and when you try to paste a link, you get nothing in the text field?
This seems to be abandoned too unfortunately. Might be due to Heroku removing its free tier and removing inactive accounts.
2
  • First Visit RawGit as suggested earlier
  • Next copy your file path from github into the RawGit box
  • RawGit will automatically produce two refrences to your web page
  • The Development and Production refrence
  • refrence the development link in your webpage if you are still developing
  • save/upload then reload your webpage
  • if there was no change it means your browser has saved your former refrence
  • clear your browser data then reload

Comments

0

https://raw.githack.com/ works perfectly well, for free, and now supports GitHub (with Gist), Bitbucket, GitLab, sourcehut. It also provides you two URLs, one for dev and one for prod (with caching, Cloudflare & stuff)

Explanations on why it doesn't work from Github out of the box and what does GitHack (from their FAQ)

When you request a file from source control hosting services, they are usually served (in the case of JavaScript, HTML, CSS, and some other file types) with a Content-Type of text/plain. As a result, most modern browsers won't actually interpret it as JavaScript, HTML, or CSS.

They do this because serving raw files from a git repo is relatively inefficient, so they want to discourage people from using their repos for static file hosting.

raw.githack.com acts as a caching proxy, forwarding requests to the corresponding service, caching the responses either for a short time (in the case of development URLs) or permanently (in the case of CDN URLs), and relaying them to your browser with the correct Content-Type headers.

The caching layer ensures that minimal load is placed on each service, and you get quick and easy static file hosting right from a repo. Everyone's happy!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.