css-fundamentals
1/8/2022 | 6/14/2022

CSS Fundamentals

A beginners course for CSS box model, HTML defaults, flexbox layout, gridbox layout, responsive design, selectors, units, and variables.

Introduction

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

box-model.svg

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;

flex-column.svg

flex-direction: row;

flex-row.svg

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;

align-center.svg

align-items: flex-end;

align-end.svg

align-items: flex-start;

align-start.svg

🤓 Keep everything inline with align-items

Justify-Content

justify-content: center;

justify-center.svg

justify-content: flex-end;

justify-end.svg

justify-content: flex-start;

justify-start.svg

justify-content: space-around;

justify-around.svg

justify-content: space-between;

justify-between.svg

justify-content: space-evenly;

justify-evenly.svg

🤓 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;

grid-columns.svg

Grid-Template-Rows

grid-template-rows: 1fr 2fr 1fr;

grid-rows.svg

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;

center-center.svg

place-items: end end;

place-items-end-end.svg

place-items: start start;

start-start.svg

🤓 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).

positioning.svg

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

youtube.png

🤓 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:

spotify.png

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:

stackoverflow.png


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.

responsive-design.png

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