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)

circle-exclamation

CSS Selectors

Basic Selectors

circle-exclamation

Pseudo-Classes and Pseudo-Elements

circle-exclamation

Advanced Selectors

circle-exclamation

Specificity calculation

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

Specificity Hierarchy (0-0-0-0)

circle-exclamation
CSS Specificity

The Box Model

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

Box Model

Box Model properties

circle-exclamation

Understanding box-sizing

circle-exclamation

Display property

Controls how elements behave in the document flow:

circle-exclamation

Modern layout systems

Flexbox (one-dimensional layout)

circle-exclamation

CSS Grid (two-dimensional layout)

circle-exclamation

Positioning

circle-exclamation

CSS custom properties (variables)

circle-exclamation

Responsive Design

Media Queries

circle-exclamation

Responsive Units

circle-exclamation

Transforms and Transitions

circle-exclamation

Animations

circle-exclamation

Modern CSS features

CSS Logical Properties

circle-exclamation

Aspect ratio

circle-exclamation

CSS functions

circle-exclamation

CSS Architecture Patterns

BEM (Block Element Modifier)

circle-exclamation

Utility-First (similar to Tailwind)

Examples of CSS patterns for modern web apps

Loading Spinner

circle-info

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

circle-info

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.

circle-info

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?