CSS - Style Sheet (Script|File) - Stylesheet


A stylesheet is a list of rules that specify the presentation of a source document.

The stylesheet is a combination of :

The stylesheet rules are parsed during the loading of the page and transformed in the CSS Object model that is made available via the stylesheets property of the document object (There is actually more than one but conceptually, this is the process)



Style sheets may have three different origins:

The priority of these sources is governed by this mechanisms:



A valid style sheet must be written according to the grammar

Content type

CSS style sheets that exist in separate files are sent over the Internet as a sequence of bytes accompanied by encoding information. The structure of the transmission, termed a message entity, is defined by RFC 2045 and RFC 2616.

A message entity with a content type of “text/css” represents an independent CSS document. The “text/css” content type has been registered by RFC 2318.

Declaration / Loading

HTML - The link element (inter-document relationships)

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

Style (Inline)

Inline with the style node

@import url("css/style.css");
@import url("https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/themes/prism.min.css");

More see CSS - Style declaration (Inline - (External|Internal) Style sheet)

loading programmatically with Javascript

We are creating link element programatically.

const head = document.querySelector('head');
const baseCdn = "https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/";
const stylesheets = [
	["themes/prism-tomorrow.min.css", "sha512-vswe+cgvic/XBoF1OcM/TeJ2FW0OofqAVdCZiEYkd6dwGXthvkSFWOoGGJgS2CW70VK5dQM5Oh+7ne47s74VTg=="],
stylesheets.forEach(stylesheet => {
		let link = document.createElement('link');

How to add a stylesheet with rules created programmatically

If you want to add dynamically new rules via javascript, just create dynamically a style element.


  • The goal is to add dynamically the following rule:
p.steelblue { color: steelblue; }
  • The javascript that adds the below css rule dynamically
let style = document.createElement("style");
style.innerText = `p.steelblue { color: steelblue; }`;
var sheet = style.sheet;
console.log("Is sheet a CSSStylesheet ? "+(sheet instanceof CSSStyleSheet));
  • The HTML that should be styled
<p class="steelblue">A steelblue paragraph</p>
  • Output:

Powered by ComboStrap