Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Essential CSS
Lesson Plan
  1. Introduction
    1. Welcome to Essential CSS
    2. Getting started with the course
    3. What are file formats?
    4. Web standards
    5. What is HTML?
    6. What is CSS?
    7. Devtools, your indispensable best friend
  2. CSS Key Concepts
    1. Some background about CSS
    2. CSS Syntax and Terminology
    3. Selectors
    4. Cascade and Specificity
    5. Origin and Importance
    6. Inheritance
    7. How browsers handle CSS
  3. Basic Mental Model
    1. Everything on the web is a box
    2. The box model
    3. How big is your box?
    4. The relationship between px and em
    5. The `box-sizing` property
    6. The `display` property
    7. Formatting contexts
    8. Block boxes
    9. Inline boxes
    10. What is inline-block?
  4. Flexbox and Grid
    1. The parent-child relationship
    2. Sizing in Flexbox
    3. Flex directions
    4. Basic grid concepts
    5. Setting up a grid
    6. Placing grid items
    7. Box alignment
  5. Building web layouts
    1. Arranging your boxes on the web
    2. Positioning schemes
    3. Floating boxes
  6. Typography
    1. Making your text look good
    2. What is a font?
    3. Typesetting with CSS

What is CSS?

3m:19s

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:

  1. External stylesheets
  2. The <style> tag
  3. 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.html

The 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.