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

<!-- 1. Inline CSS (highest specificity, avoid when possible) -->
<p style="color: red; font-size: 16px;">Inline styled text</p>

<!-- 2. Internal CSS (in <head>) -->
<style>
  p { color: blue; }
</style>

<!-- 3. External CSS (recommended) -->
<link rel="stylesheet" href="styles.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)

/* Cascade example - last rule wins */
p { color: blue; }
p { color: red; }    /* This wins */

/* Inheritance example */
body {
  font-family: Arial, sans-serif;  /* Inherited by all text */
  color: #333;                      /* Inherited */
  border: 1px solid black;          /* NOT inherited */
}

/* Force inheritance */
.child {
  color: inherit;  /* Explicitly inherit parent's color */
}

CSS Selectors

Basic Selectors

/* Universal selector */
* {
  margin: 0;
  padding: 0;
}

/* Type/Element selector */
p { line-height: 1.6; }

/* Class selector */
.highlight { background-color: yellow; }

/* ID selector */
#header { position: fixed; }

/* Attribute selectors */
input[type="text"] { border: 1px solid #ccc; }
a[href^="https"] { color: green; }  /* Starts with */
a[href$=".pdf"] { color: red; }     /* Ends with */
a[href*="google"] { color: blue; }  /* Contains */

Pseudo-Classes and Pseudo-Elements

/* Pseudo-classes (state) */
a:hover { color: red; }
a:active { color: darkred; }
a:visited { color: purple; }
input:focus { outline: 2px solid blue; }
li:first-child { font-weight: bold; }
li:last-child { margin-bottom: 0; }
li:nth-child(2n) { background: #f0f0f0; }  /* Even items */
li:nth-child(odd) { background: white; }    /* Odd items */
input:disabled { opacity: 0.5; }
input:checked + label { color: green; }
div:empty { display: none; }

/* Pseudo-elements (create elements) */
p::before { content: "→ "; }
p::after { content: " ←"; }
p::first-line { font-weight: bold; }
p::first-letter { font-size: 2em; }
input::placeholder { color: #999; }
::selection { background: yellow; }

Advanced Selectors

/* Descendant combinator (space) */
article p { margin-bottom: 1em; }

/* Child combinator (>) */
ul > li { list-style: none; }

/* Adjacent sibling (+) */
h1 + p { font-size: 1.2em; }  /* First p after h1 */

/* General sibling (~) */
h1 ~ p { color: gray; }  /* All p siblings after h1 */

/* Multiple selectors (,) */
h1, h2, h3 { font-family: 'Georgia', serif; }

/* Combining selectors */
div.container > ul.menu li:hover a {
  color: blue;
}

/* :not() pseudo-class */
input:not([type="submit"]) {
  width: 100%;
}

/* :has() - Parent selector (modern browsers) */
div:has(> img) {
  padding: 20px;  /* Divs containing images */
}

Specificity calculation

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

Specificity Hierarchy (0-0-0-0)

/* Inline styles:    1-0-0-0 (1000 points) */
/* IDs:              0-1-0-0 (100 points)  */
/* Classes/pseudo:   0-0-1-0 (10 points)   */
/* Elements:         0-0-0-1 (1 point)     */

/* Examples with calculated specificity: */
p                    /* 0-0-0-1 = 1 */
.class               /* 0-0-1-0 = 10 */
#id                  /* 0-1-0-0 = 100 */
p.class              /* 0-0-1-1 = 11 */
#id .class p         /* 0-1-1-1 = 111 */
#id #anotherId       /* 0-2-0-0 = 200 */
style="..."          /* 1-0-0-0 = 1000 */

/* !important overrides everything (avoid!) */
p { color: red !important; }
CSS Specificity

The Box Model

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

Box Model

Box Model properties

.box {
  /* Content dimensions */
  width: 300px;
  height: 200px;
  
  /* Padding (inside border) */
  padding: 20px;                    /* All sides */
  padding: 10px 20px;               /* Vertical | Horizontal */
  padding: 10px 20px 30px;          /* Top | Horizontal | Bottom */
  padding: 10px 20px 30px 40px;    /* Top | Right | Bottom | Left */
  
  /* Border */
  border: 2px solid #333;
  border-radius: 8px;
  
  /* Margin (outside border) */
  margin: 20px auto;  /* Center horizontally */
  
  /* Box sizing */
  box-sizing: border-box;  /* Include padding/border in width */
}

/* Global box-sizing reset (recommended) */
*, *::before, *::after {
  box-sizing: border-box;
}

Understanding box-sizing

/* Default (content-box) */
.content-box {
  box-sizing: content-box;
  width: 300px;
  padding: 20px;
  border: 10px solid;
  /* Total width = 300 + (20*2) + (10*2) = 360px */
}

/* Border-box (recommended) */
.border-box {
  box-sizing: border-box;
  width: 300px;
  padding: 20px;
  border: 10px solid;
  /* Total width = 300px (padding/border included) */
}

Display property

Controls how elements behave in the document flow:

/* Common display values */
.element {
  display: block;        /* Full width, new line */
  display: inline;       /* Inline with text */
  display: inline-block; /* Inline but respects width/height */
  display: none;         /* Hidden, removed from flow */
  display: flex;         /* Flexbox container */
  display: grid;         /* Grid container */
  display: inline-flex;  /* Inline flexbox */
  display: inline-grid;  /* Inline grid */
}

Modern layout systems

Flexbox (one-dimensional layout)

/* Flex container */
.flex-container {
  display: flex;
  flex-direction: row;        /* row | column | row-reverse | column-reverse */
  justify-content: center;    /* Main axis: flex-start | center | space-between | space-around | space-evenly */
  align-items: center;        /* Cross axis: stretch | center | flex-start | flex-end | baseline */
  flex-wrap: wrap;           /* nowrap | wrap | wrap-reverse */
  gap: 20px;                 /* Space between items */
}

/* Flex items */
.flex-item {
  flex: 1;                   /* Grow to fill space */
  flex-basis: 200px;         /* Initial size */
  flex-grow: 1;              /* Growth factor */
  flex-shrink: 0;            /* Shrink factor */
  align-self: flex-start;    /* Override align-items */
  order: 2;                  /* Display order */
}

/* Common flexbox patterns */
/* Center everything */
.center {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Navbar */
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* Card layout */
.card-container {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

CSS Grid (two-dimensional layout)

/* Grid container */
.grid-container {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;      /* Three columns */
  grid-template-columns: repeat(3, 1fr);   /* Three equal columns */
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); /* Responsive */
  grid-template-rows: 100px auto 50px;     /* Three rows */
  gap: 20px;                                /* Row and column gap */
  grid-template-areas:                     /* Named areas */
    "header header header"
    "sidebar main main"
    "footer footer footer";
}

/* Grid items */
.grid-item {
  grid-column: 1 / 3;        /* Span columns 1-2 */
  grid-row: 2 / 4;           /* Span rows 2-3 */
  grid-area: header;         /* Use named area */
  place-self: center;        /* Center in cell */
}

/* Responsive grid example */
.responsive-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
}

Positioning

/* Position values */
.element {
  position: static;     /* Default - normal flow */
  position: relative;   /* Relative to normal position */
  position: absolute;   /* Relative to positioned parent */
  position: fixed;      /* Relative to viewport */
  position: sticky;     /* Hybrid relative/fixed */
}

/* Positioning properties */
.positioned {
  position: absolute;
  top: 20px;
  right: 20px;
  bottom: auto;
  left: auto;
  z-index: 10;  /* Stacking order */
}

/* Common patterns */
/* Center absolutely positioned element */
.center-absolute {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

/* Sticky header */
.sticky-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: white;
}

/* Overlay */
.overlay {
  position: fixed;
  inset: 0;  /* top: 0; right: 0; bottom: 0; left: 0; */
  background: rgba(0, 0, 0, 0.5);
}

CSS custom properties (variables)

/* Define variables */
:root {
  /* Colors */
  --primary-color: #007bff;
  --secondary-color: #6c757d;
  --text-color: #333;
  --bg-color: #f8f9fa;
  
  /* Spacing */
  --spacing-unit: 8px;
  --spacing-small: calc(var(--spacing-unit) * 2);
  --spacing-medium: calc(var(--spacing-unit) * 3);
  --spacing-large: calc(var(--spacing-unit) * 4);
  
  /* Typography */
  --font-family: 'Inter', -apple-system, sans-serif;
  --font-size-base: 16px;
  --line-height: 1.5;
}

/* Use variables */
.button {
  background: var(--primary-color);
  padding: var(--spacing-small) var(--spacing-medium);
  font-family: var(--font-family);
  color: var(--text-color, black);  /* Fallback value */
}

/* Change variables dynamically */
.dark-theme {
  --text-color: #f0f0f0;
  --bg-color: #1a1a1a;
}

/* Scoped variables */
.card {
  --card-padding: 20px;
  padding: var(--card-padding);
}

Responsive Design

Media Queries

/* Mobile-first approach (recommended) */
/* Base styles for mobile */
.container {
  width: 100%;
  padding: 15px;
}

/* Tablet and up */
@media (min-width: 768px) {
  .container {
    max-width: 750px;
    margin: 0 auto;
  }
}

/* Desktop and up */
@media (min-width: 1024px) {
  .container {
    max-width: 960px;
  }
}

/* Large desktop */
@media (min-width: 1200px) {
  .container {
    max-width: 1140px;
  }
}

/* Other media query features */
@media (orientation: landscape) { }
@media (prefers-color-scheme: dark) { }
@media (prefers-reduced-motion: reduce) { }
@media print { }

/* Container queries (modern browsers) */
.card-container {
  container-type: inline-size;
}

@container (min-width: 400px) {
  .card {
    display: flex;
  }
}

Responsive Units

/* Relative units */
.text {
  font-size: 1rem;      /* Relative to root font-size */
  padding: 1em;         /* Relative to current font-size */
  width: 50%;           /* Percentage of parent */
  height: 100vh;        /* Viewport height */
  max-width: 90vw;      /* Viewport width */
  margin: 5vmin;        /* Smaller of vw or vh */
  font-size: clamp(1rem, 2.5vw, 2rem);  /* Responsive clamping */
}

/* Modern responsive typography */
:root {
  font-size: clamp(14px, 2vw, 18px);
}

h1 {
  font-size: clamp(1.5rem, 4vw, 3rem);
}

Transforms and Transitions

/* Transforms */
.transform {
  transform: translate(50px, 100px);
  transform: translateX(-50%);
  transform: rotate(45deg);
  transform: scale(1.5);
  transform: skew(10deg, 20deg);
  transform: rotate(45deg) scale(1.2) translateX(100px);  /* Multiple */
  transform-origin: center;  /* Transform anchor point */
}

/* Transitions */
.transition {
  transition: all 0.3s ease;
  transition: opacity 0.3s, transform 0.3s;
  transition-timing-function: ease | linear | ease-in | ease-out | ease-in-out | cubic-bezier();
}

/* Common transition pattern */
.button {
  background: blue;
  transform: translateY(0);
  transition: all 0.3s ease;
}

.button:hover {
  background: darkblue;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}

Animations

/* Define keyframes */
@keyframes slideIn {
  from {
    transform: translateX(-100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

/* Apply animation */
.animated {
  animation: slideIn 0.5s ease forwards;
  animation-name: slideIn;
  animation-duration: 0.5s;
  animation-timing-function: ease;
  animation-delay: 0.2s;
  animation-iteration-count: 1;  /* or infinite */
  animation-direction: normal;   /* or reverse, alternate */
  animation-fill-mode: forwards; /* or backwards, both */
}

/* Multiple animations */
.multi-animated {
  animation: 
    slideIn 0.5s ease,
    fadeIn 0.3s ease 0.2s;
}

Modern CSS features

CSS Logical Properties

/* Physical properties (old way) */
.old {
  margin-left: 20px;
  padding-right: 10px;
}

/* Logical properties (internationalization-friendly) */
.modern {
  margin-inline-start: 20px;  /* left in LTR, right in RTL */
  padding-inline-end: 10px;   /* right in LTR, left in RTL */
  border-block-start: 1px solid;  /* top */
  border-block-end: 1px solid;    /* bottom */
  inline-size: 100%;  /* width */
  block-size: 50px;   /* height */
}

Aspect ratio

/* Maintain aspect ratio */
.video-container {
  aspect-ratio: 16 / 9;
  width: 100%;
  background: black;
}

.square {
  aspect-ratio: 1;  /* 1:1 ratio */
}

CSS functions

/* Calculations */
.calculated {
  width: calc(100% - 40px);
  height: calc(100vh - var(--header-height));
  padding: calc(var(--spacing) * 2);
}

/* Min, Max, Clamp */
.responsive {
  width: min(90%, 1200px);
  height: max(300px, 50vh);
  font-size: clamp(14px, 2vw, 20px);
}

/* CSS Filters */
.filtered {
  filter: blur(5px);
  filter: brightness(1.2);
  filter: contrast(1.5);
  filter: grayscale(100%);
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.2));
}

CSS Architecture Patterns

BEM (Block Element Modifier)

/* Block */
.card { }

/* Element */
.card__header { }
.card__body { }
.card__footer { }

/* Modifier */
.card--featured { }
.card__header--large { }

Utility-First (similar to Tailwind)

/* Utility classes */
.flex { display: flex; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.p-4 { padding: 1rem; }
.mt-2 { margin-top: 0.5rem; }
.text-center { text-align: center; }
.bg-primary { background: var(--primary-color); }

Examples of CSS patterns for modern web apps

Loading Spinner

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(0,0,0,0.1);
  border-top-color: var(--primary-color);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

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

.card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  padding: 1.5rem;
  transition: transform 0.2s, box-shadow 0.2s;
}

.card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

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.

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  padding: 2rem;
  border-radius: 8px;
  max-width: 500px;
  width: 90%;
  max-height: 90vh;
  overflow-y: auto;
}

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?