Description and Basic Syntax

CSS (Cascading Style Sheets) is a stylesheet language that controls the presentation layer of web documents. It transforms raw HTML structure into visually appealing, responsive, and interactive user interfaces.

Core Principles:

  • Separation of Concerns: Structure (HTML) vs. Presentation (CSS)

  • Cascade: Multiple style rules can apply to the same element

  • Inheritance: Child elements inherit certain properties from parents

  • Specificity: Determines which styles win when conflicts occur

CSS terminology

Anatomy of a CSS rule

/* Selector  {  Property: Value;  } */
   h1        {  color:    blue;   }

/* Complete rule with multiple declarations */
.card {
  background-color: white;  /* Declaration 1 */
  border-radius: 8px;       /* Declaration 2 */
  box-shadow: 0 2px 4px rgba(0,0,0,0.1); /* Declaration 3 */
}
  • Selectors: Patterns that target HTML elements

  • Properties: Aspects of elements you want to style (color, size, position)

  • Values: Settings applied to properties

  • Declaration: A property-value pair

  • Rule/Ruleset: Selector(s) + declaration block

How CSS is applied

Three ways to add CSS

The Cascade and Inheritance

CSS applies styles based on three factors:

  1. Source Order: Later rules override earlier ones

  2. Specificity: More specific selectors win

  3. Importance: !important overrides everything (use sparingly)

CSS Selectors

Basic Selectors

Pseudo-Classes and Pseudo-Elements

Advanced Selectors

Specificity calculation

Specificity determines which styles apply when multiple rules target the same element.

Specificity Hierarchy (0-0-0-0)

CSS Specificity

The Box Model

Every element is a rectangular box with content, padding, border, and margin:

Box Model

Box Model properties

Understanding box-sizing

Display property

Controls how elements behave in the document flow:

Modern layout systems

Flexbox (one-dimensional layout)

CSS Grid (two-dimensional layout)

Positioning

CSS custom properties (variables)

Responsive Design

Media Queries

Responsive Units

Transforms and Transitions

Animations

Modern CSS features

CSS Logical Properties

Aspect ratio

CSS functions

CSS Architecture Patterns

BEM (Block Element Modifier)

Utility-First (similar to Tailwind)

Examples of CSS patterns for modern web apps

Loading Spinner

This creates a spinning circle using a border trick where only the top is colored, combined with a continuous rotation animation. It's a simple, pure CSS solution for loading states that works everywhere without images or external dependencies.

Card Component

This demonstrates modern card styling with rounded corners, subtle shadows, and smooth hover effects that lift the card and intensify the shadow. This pattern creates depth and interactive feedback that makes interfaces feel polished and responsive.

This pattern creates centered modal dialogs: a full-screen semi-transparent backdrop using inset: 0 with flexbox to center the actual content, plus high z-index to appear above everything else. The max-height with overflow-y ensures modals work even if content is too tall for the screen.

Last updated

Was this helpful?