Cascading Stylesheets (CSS) is what allows you to tell the browser how you’d like your content to be displayed. CSS can be used to refer to the actual code, or the file containing the code. These files will have the file extension of .css.
As one of the key web technologies, CSS has a dedicated group that works on developing CSS standards within the W3C. They are known as the CSS working group. New CSS is actively being developed through an open process between the group members, browser vendors and the public. Discussions take place on GitHub, where anyone can browse through all the different topics and even chime in, if they are interested.
CSS alone does not do anything. It requires HTML to exist because CSS describes how HTML elements are presented on the page. Without any elements, there is nothing to describe. There are 3 ways apply CSS to your web page:
- External stylesheets
- The
<style>tag - Inline styles
External stylesheets
The most common and recommended approach is via external stylesheets. This means all your CSS will be written in a separate file, then linked to your .html file so the browser knows the styles apply to that specific page.
To do that, you can make use of the <link> tag, also known as the External Resource Link element. As its name suggests, this tag allows authors to link other resources in separate files, like stylesheets, custom favicons or scripts, to their document. This tag should be used in the <head> element.
The <link> element is a void element, which means it does not have a closing tag. It also has many attributes, but we will focus on the two attributes required for linking an external stylesheet to your document: href and rel.
<link rel="stylesheet" href="css/styles.css" />The href attribute is also used in the <a> tag and is short for hypertext reference, which defines the target URL where the resource file can be found. The URL can be a relative URL or an absolute URL. A relative URL tells the browser to locate the file based on the location of the document. For example, in a folder structure like below:
website/ ├── css/ └── styles.css ├── images/ ├── fonts/ └── index.htmlThe styles.css file is in a folder called css. A URL starting without a backslash tells the browser to start looking from the same level as index.html for the folder, then within the folder, find the styles.css file.
An absolute URL will provide the exact location of file starting from the root folder. If you are including stylesheets from an external library that is hosted on another server, for example, you will usually be using an absolute URL, like so:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/charts.css/dist/charts.min.css"/>The <style> tag
The <style> tag should also be used in the <head> element. What’s special about the <style> tag is that you are allowed to write CSS within the opening and closing tags, and this CSS will be recognised by the browser as such.
<style> body { background-color: midnightblue; }
h1 { color: gold; }</style>In general, it is still advisable to put your CSS in an external file and link to it with the <link> tag as described in the above section.
Inline CSS
Another way to style the elements in your page is via inline CSS. This method involves writing the CSS as values of the style attribute in the element’s tag.
<h1 style="color: navy">Essential CSS (and HTML)</h1>It is advisable to keep CSS in a separate file, or even within the <style> tag because inline CSS increases the specificity of the CSS declaration, making it very hard to override. In fact, the only way to override inline CSS is with !important but if you get to specificity levels that high, the maintainability of your code is probably going downhill real quick.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson