CSS Fundamentals

January 18, 2022

Last updated: October 19, 2023

2,266 words

Post contents

This course will guide you through the fundamentals of CSS with a few of my own personal recommendations for further learning.

Keep in mind that some concepts or principles laid out here are broad and may not be accurate or reliable for every situation.

💡 This tutorial assumes basic knowledge of HTML and CSS

This tutorial will cover the following items in order:


Box Model

Four boxes, each containing one-another. The boxes, from largest to smallest are: "Margin", "Border", "Padding", and "Content"

The box model is a representation of a documents element through a set of boxes with the following properties:

  • Margin - Wraps any border, padding, and content as white space.
  • Border - Wraps any padding and content.
  • Padding - Wraps any contents as white space.
  • Content - Contains text, imagery, videos, etc.

Every element on the browser has a box model. You can inspect them using browser developer tools. Understanding the box model layers will help you identify element boundaries.

📚 Learn More About Developer Tools


HTML Defaults

Nearly every HTML element has some default browser styles. These styles are called HTML defaults. These defaults may change depending on the browsers rendering engine.

🤓 Not every browser supports every CSS property! For up-to-date browser support I suggest checking out Can I Use?

Every HTML element has a place and a purpose. Some HTML elements are strictly used for grouping content and are generally referred to as containers, while other HTML elements are used for text, images and more.

Here are some examples of HTML container elements:

<aside>	<!-- Represents anything indirectly related to the documents content --></aside><body>	<!-- There can only be one body. It contains all the documents content --></body><div>	<!-- A pure container as it does not inherently represent anything --></div><footer>	<!-- Footers represent the last child of a given container --></footer><header>	<!-- Headers represent the first child of a given container --></header><main>	<!-- Contains the primary content inside a body container --></main><nav>	<!-- Contains links for navigating to current or related documents --></nav><section>	<!-- Contains a stand-alone piece of content --></section><ul>	<!-- Represents a list of items --></ul>

Containing elements are very useful for styling as they allow developers to target groups of content easily through class, ID, and type selectors.

âš¡ Live Code Example: HTML Defaults

It is important to know about HTML defaults and so you can work with them and not against them when styling a document.

📚 Learn More About HTML


Flexbox Layout

The CSS property display: flex is also known as flexbox. Flexbox is used for creating one-dimensional layouts on a column (up and down) or row (left and right) direction.

Flex-Direction
flex-direction: column;
A column of three items stacked on one-another
flex-direction: row;
A row of three items stacked next to one-another

Adding display: flex to a container will cause any immediate descendants to become flex items. Using a few additional CSS properties we can align, justify, and space these same flex items inside the container.

Placement Methods

These placement methods are used to distribute both flex and gridbox items:

🤓 These are some godly CSS properties that everyone should know about

Align-Items
align-items: center;
Three items in a container vertically aligned to the middle of the container
align-items: flex-end;
Three items in a container vertically aligned to the bottom of the container
align-items: flex-start;
Three items in a container vertically aligned to the top of the container

🤓 Keep everything inline with align-items


Justify-Content
justify-content: center;
Three items in a container horizontally aligned to the center of the container
justify-content: flex-end;
Three items in a container horizontally aligned to the end of the container
justify-content: flex-start;
Three items in a container horizontally aligned to the start of the container
justify-content: space-around;
Three items in a container with one item in the center and the other two on the left and right with spacing equal the left and right
justify-content: space-between;
Three items in a container with one item in the center and the other two on the furthest left and right
justify-content: space-evenly;
Three items in a container with equal spacing on all sides

🤓 Space your content out with justify-content

Here is a list of CSS properties used to control flexbox properties:

âš¡ Live Code Example: Flexbox Layout


Gridbox Layout

The CSS property display: grid is commonly referred to as gridbox. Unlike flexbox, it is capable of creating two-dimensional layouts using intersecting columns and rows.

Grid-Template-Areas & Grid-Template-Columns
grid-template-areas:  "a a"  "b c";grid-template-columns: 1fr 1fr;
A grid with an item on the top spanning two columns and two items on the bottom
Grid-Template-Rows
grid-template-rows: 1fr 2fr 1fr;
A grid with a single column and three items; the item in the middle is double the height as the other two items above and below it

Adding display: grid to a container will cause any immediate descendants to become grid items. Similar to flexbox, we can use placement methods to help align, justify, and space grid items inside the container.


Place-Items
place-items: center center;
A grid of items centered horizontally and vertically
place-items: end end;
A grid of items aligned to the bottom left corner
place-items: start start;
A grid of items aligned to the top right corner

🤓 Place-items is super effective if using gridbox

Here is a list of CSS properties used to control gridbox properties:

âš¡ Live Code Example: Gridbox Layout


Positioning

The CSS property position determines an elements flow inside a document.

The CSS properties top, bottom, left, right are used on positioned elements to control an offset while z-index controls the elements order (bringing it to the front or back).

A series of boxes absolutely positioned in one-another with the dimensions of "left", "right", "top", and "bottom" precalculated
.root {	position: relative;	width: 768px;    height: 272px;}.container {	position: absolute;	left: 224px;    top: 100px;    width: 320px;    height: 145px;    z-index: 90;}.item {	position: absolute;    bottom: 50px;    right: 90px;	width: 213px;	height: 65px;    z-index: 100;}

There are five types of element positions:

  • Absolute - The element is removed from document flow and positioned relative to the nearest position: relative parent
    • Can be offset relative to the parent container and ordered
  • Fixed - The element is removed from document flow and positioned relative to the initial container
    • Can be offset relative to the initial container and ordered
  • Relative - The element flows normally and provides relative positioning for children elements
    • Can be offset relative to itself and ordered
  • Static - The default position
    • Unaffected by offset and order
  • Sticky - The element flows normally and "sticks" to the nearest container
    • A mixture between relative and fixed positions depending on the scroll mechanism
    • Can be offset relative to the parent container and ordered

âš¡ Live Code Example: Positioning


Where do I use Flexbox, Gridbox or Positioning?

Flexbox:
  • Used in headers, lists, tags, or any other block or inline content with the correct flex-direction
  • Primary method to align and justify content in small components
  • Easy to use

For example, YouTube uses a flexbox to space out their headers children elements:

A heading on the YouTube search bar with spacing for each item

🤓 Mastering the flexbox will take you very far in CSS as it is used everywhere

Gridbox:
  • Used in creating complex layouts that require both columns and rows
  • Provides the easiest and shortest way to center elements
  • Verbose and powerful

For example, Spotify uses a gridbox to achieve their playlist player layout:

A player view of Spotify with a left-hand sidebar and a list of items on the right
Positioning:
  • Used in lightboxes, mobile menus, modal windows, and similar overlaying elements
  • Primarily used to remove elements from document flow

For example, the cookies modal on stackoverflow uses a fixed position to stay on your screen while hovering above other document elements:

An "accept all cookies" banner on StackOverflow absolutely positioned to the bottom of the page

Responsive Design

Responsive Design is an approach to web design where the goal is to create a layout that will render beautifully on any device or screen size.

Two folding phones; The Samsung Galaxy Flip and Galaxy Fold

To achieve this, designers can use media queries (AKA breakpoints) to add, override, and unset styles on any screen size.

The code snippet below shows how breakpoints can be used inside a .css file to override an existing CSS rule:

.foobar {  border: 1px solid red;  color: red;}@media (min-width: 768px) {  .foobar {    border: unset;    color: orange;    display: flex;	}}

Here is a list of popular screen size breakpoints for max- and min-width:

  • 320px - Mobile S
  • 375px - Mobile M
  • 425px - Mobile L
  • 768px - Tablet
  • 1024px - Laptop
  • 1440px - Laptop L
Mobile First Design

One great method for responsive designing is called mobile first design. To use the mobile first method, simply use the min- prefix on your rules when applying breakpoints. This min- prefix will limit your breakpoints to a minimum screen size. This allows for smaller screens to be styled first, and exceptions made for larger devices.

âš¡ Live Code Example: Responsive Design


CSS Selectors

CSS selectors are used inside .css files in order to target HTML elements and allows for CSS rules to be applied.

There are five basic CSS selectors:

  • Universal ( * ) - Targets all elements
  • Class (.class) - Targets all with the given class
  • ID (#id) -Targets all with the given ID
  • Type (h1) - Targets all with the given type
  • Attribute ([type="submit"]) Targets all with the given attribute

🤓 I recommend using the .class selector over the #id selector as ID attributes are unique

You can group selectors under one CSS rule using commas to share properties among multiple selectors:

.foo {  color: red;}#bar {	color: blue;}.foo, #bar {	padding: 1rem;}

You can also combine selectors using a variety of syntax to target anything from descendants to siblings:

section h1 {	color: red;}section > h2 {  color: orange;}section + h3 {	color: yellow;}section ~ h4 {	color: green;}

Selectors can also be used to target browser pseudo-elements:

input::placeholder {  color: #dddddd;}

Using this variety of combinators and selectors you can easily style any part of a web document.

âš¡ Live Code Example: Selectors

📚 Learn More About Selectors


Units & Value Types

In CSS there are seven absolute and eight relative length unit types. Here are the popular ones:

  • px - Pixels, absolute length unit
  • em - Relative to the parent size
  • rem - Relative to the root element size
  • vw - View-width, relative to the current device
  • vh - View-height, relative to the current device

These CSS units are used to determine the size of a property value.

🤓 I recommend using the units px and rem units

CSS property values will only accept certain syntax and types. Let's use color for example:

.foobar__keyword {  color: red; /* Color will accept certain keywords */	}.foobar__hex {  color: #ff0000; /* It will also take hexadecimal values */}.foobar__rgb {  color: rgb(255, 0, 0); /* As well as functional notations */}

📚 Learn More About CSS Types

📚 Learn More About Units & Values


CSS Variables

CSS variables allow us to define arbitrary values for reuse across a stylesheet. For example:

:root {  --red: #ff0000;}.foo {	background-color: var(--red);}.bar {	color: var(--red);}

It is common to use CSS variables for repeated values such as colors, font-size, padding, etc.

âš¡ Live Code Example: CSS Variables

Subscribe to our newsletter!

Subscribe to our newsletter to get updates on new content we create, events we have coming up, and more! We'll make sure not to spam you and provide good insights to the content we have.