Module 02 CSS ● Beginner → Pro ~ 50 min read

CSS3: From Box Model to Professional Architecture

CSS is not just about making things look pretty. At the professional level, CSS is a system you design — with structure, scalability, and maintainability built in from the start.

1

The Fundamentals — Zero Phase

Before you build layouts, you must understand how CSS attaches to HTML and how every element on a page is sized.

Selectors

Selectors are how CSS finds elements to style. There are three core types you must memorize:

css
/* 1. Tag Selector — targets ALL elements of that type */
p { color: #555; }

/* 2. Class Selector — targets elements with class="card" */
.card { border-radius: 8px; }

/* 3. ID Selector — targets ONE unique element with id="hero" */
#hero { background: #0d1117; }
💡 Best Practice
Prefer classes over IDs for styling. IDs have very high specificity and can cause frustrating bugs when you try to override them later. Reserve IDs for JavaScript targeting.

Combinator Selectors

Beyond basic selectors, combinators let you target elements based on their relationship to other elements:

css — Combinators
/* Descendant: any .title inside .card (any depth) */
.card .title { color: white; }

/* Child: only DIRECT .title children of .card */
.card > .title { font-size: 1.2rem; }

/* Adjacent sibling: the first p right after an h2 */
h2 + p { margin-top: 0; }

/* General sibling: ALL p elements after h2 */
h2 ~ p { color: #8b949e; }

/* Attribute selector: target inputs by type */
input[type="email"] { border-color: #3b82f6; }

/* Attribute contains: any href with "google" */
a[href*="google"] { color: #34d399; }
ℹ️ Interview Question
Q: What's the difference between .card .title and .card > .title?
A: The space (descendant) selects ANY .title nested at any depth. The > (child combinator) only selects direct children — not grandchildren or deeper.

The Box Model — The Most Important Concept in CSS

Every single element on a page — a paragraph, a button, an image — is a rectangular box. The Box Model defines the layers of that box:

Layer 1 (innermost)
Content
The actual text or image. Sized with width and height.
Layer 2
Padding
Space inside the border, between the content and the edge.
Layer 3
Border
The visible line around the padding. Can be solid, dashed, or dotted.
Layer 4 (outermost)
Margin
Space outside the border, pushing away neighboring elements.
css
.card {
    /* Content size */
    width: 300px;

    /* Padding: space inside */
    padding: 20px;

    /* Border: visible edge */
    border: 1px solid #30363d;

    /* Margin: space outside */
    margin: 16px;
}

/* CRITICAL: Always add this to your CSS reset.
   It makes width include padding and border — 
   avoiding confusing layout math. */
*, *::before, *::after {
    box-sizing: border-box;
}

Colors & Typography

css
body {
    font-family: 'Inter', sans-serif;
    font-size: 16px;
    line-height: 1.7;
    color: #e6edf3;
    background-color: #0d1117;
}

h1 {
    font-size: 2.5rem;
    font-weight: 700;
    letter-spacing: -0.02em;
}
2

Intermediate Layouts — Building Phase

Once you can style a single element, you need to arrange multiple elements into layouts. Modern CSS gives you two powerful tools.

Flexbox — 1D Layouts (Rows or Columns)

Flexbox is the modern standard for arranging items in a single direction. Perfect for navigation bars, card rows, and centering content.

css
.navbar {
    display: flex;
    justify-content: space-between; /* Space between logo and links */
    align-items: center;            /* Vertically center everything */
    gap: 1.5rem;                    /* Space between flex children */
}

/* Center anything perfectly */
.hero {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}
justify-content
Horizontal Alignment
flex-start, center, flex-end, space-between
align-items
Vertical Alignment
flex-start, center, flex-end, stretch

CSS Grid — 2D Layouts (Rows AND Columns)

Grid is the powerhouse for building entire page structures. When your layout has both rows and columns, reach for Grid.

css
/* 3-column responsive card grid */
.card-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr); /* 3 equal columns */
    gap: 1.5rem;
}

/* Named grid areas for complex page layouts */
.page-layout {
    display: grid;
    grid-template-areas:
        "header header"
        "sidebar main"
        "footer footer";
    grid-template-columns: 280px 1fr;
}

.site-header { grid-area: header; }
.sidebar     { grid-area: sidebar; }
.main        { grid-area: main; }
.site-footer { grid-area: footer; }

Positioning

The position property lets you break elements out of the normal document flow.

ValueBehaviorUse Case
staticDefault. Follows normal flow.Everything — unless you need to change it.
relativeStays in flow, but can be nudged.Creating a positioning context for children.
absoluteRemoved from flow, positioned to nearest relative parent.Tooltips, badges, dropdown menus.
fixedPinned to the browser viewport.Sticky navigation bars, cookie banners.
stickyRelative until it hits a threshold, then fixed.Table headers, side navigation.
3

Responsive Design — Modern Phase

A professional ensures their site looks great on everything from a 4K monitor to an iPhone SE.

Media Queries

css — Mobile-First Strategy
/* Mobile styles first (default) */
.card-grid {
    display: grid;
    grid-template-columns: 1fr; /* 1 column on mobile */
}

/* Then scale UP for larger screens */
@media (min-width: 600px) {
    .card-grid {
        grid-template-columns: 1fr 1fr; /* 2 columns on tablet */
    }
}

@media (min-width: 1024px) {
    .card-grid {
        grid-template-columns: repeat(3, 1fr); /* 3 on desktop */
    }
}

Relative Units

UnitRelative ToBest For
remRoot font-size (usually 16px)Font sizes, spacing — consistent scale
emParent element's font-sizeComponent-level spacing
%Parent element's sizeFluid widths (width: 100%)
vw / vhViewport width / heightFull-screen sections, hero banners
clamp()min, ideal, maxFluid typography that scales automatically
css — Fluid Typography
/* clamp(minimum, preferred, maximum) */
/* This heading scales fluidly — no media query needed! */
h1 {
    font-size: clamp(1.75rem, 5vw + 1rem, 3.5rem);
}
4

Advanced Polish — Expert Phase

This is where you make the site feel alive and premium.

Transitions & Animations

css
/* Smooth hover transition */
.btn {
    background: #3b82f6;
    transition: background 0.2s ease, transform 0.2s ease;
}
.btn:hover {
    background: #2563eb;
    transform: translateY(-2px);
}

/* Keyframe animation */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.hero-text {
    animation: fadeInUp 0.6s ease forwards;
}

CSS Variables — Keep Your Code DRY

css
:root {
    --primary:    #3b82f6;
    --bg:         #0d1117;
    --text:       #e6edf3;
    --radius:     8px;
    --spacing-md: 1.5rem;
}

/* Now use them everywhere */
.card {
    background: var(--bg);
    border-radius: var(--radius);
    padding: var(--spacing-md);
}

/* Change the whole theme by updating ONE value */
[data-theme="light"] {
    --bg:   #ffffff;
    --text: #1a1a1a;
}

Pseudo-classes & Pseudo-elements

css
/* Pseudo-classes — element state */
a:hover   { color: #60a5fa; }
a:visited { color: #a78bfa; }
li:nth-child(2) { font-weight: bold; }
input:focus { outline: 2px solid #3b82f6; }

/* Pseudo-elements — inject content via CSS */
.card::before {
    content: '';
    display: block;
    height: 4px;
    background: linear-gradient(90deg, #3b82f6, #8b5cf6);
    border-radius: 4px 4px 0 0;
}
5

Professional Workflow — Pro Phase

BEM Naming Convention

BEM (Block–Element–Modifier) is a naming system that prevents "CSS Hell" as projects scale.

css — BEM Pattern
/* Block: standalone component */
.card { }

/* Element: part of the block */
.card__title { font-size: 1.25rem; }
.card__image { width: 100%; }
.card__body  { padding: 1rem; }

/* Modifier: a variation of the block */
.card--featured { border: 2px solid #3b82f6; }
.card--compact  { padding: 0.5rem; }

Sass/SCSS

Sass adds features like nesting, variables, and mixins to CSS. It compiles down to regular CSS.

scss
// Variables
$primary: #3b82f6;
$radius:  8px;

// Nesting (no need to repeat parent selector)
.card {
    border-radius: $radius;

    &__title {
        font-size: 1.25rem;
        color: $primary;
    }

    &:hover {
        box-shadow: 0 8px 24px rgba(0,0,0,0.2);
    }
}

// Reusable mixin
@mixin flex-center {
    display: flex;
    justify-content: center;
    align-items: center;
}

.hero { @include flex-center; }
6

Specificity & The Cascade

When multiple CSS rules target the same element, the browser uses specificity to decide which one wins. Understanding this prevents the #1 frustration in CSS.

The Specificity Hierarchy (Low → High)

SelectorSpecificity ScoreExample
Element/Tag0-0-1p { }
Class / Attribute / Pseudo-class0-1-0.card { }, :hover
ID1-0-0#hero { }
Inline style1-0-0-0style="color:red"
!importantOverrides everythingcolor: red !important;
css — Specificity Battle
/* Specificity: 0-0-1 */
p { color: grey; }

/* Specificity: 0-1-0 — WINS over the tag selector */
.intro { color: blue; }

/* Specificity: 1-0-0 — WINS over the class */
#hero-text { color: white; }

/* !important overrides EVERYTHING (avoid this!) */
p { color: red !important; }
⚠️ Avoid !important
Using !important is a sign of poorly organized CSS. Once you start using it, you need !important to override the first !important, creating an endless cycle. Fix specificity issues by restructuring your selectors instead.

The Cascade Order

When specificity is equal, CSS applies rules in this order:

  1. Origin — Browser defaults → User styles → Author styles
  2. Specificity — Higher specificity wins
  3. Source Order — The LAST rule in the CSS file wins (when specificity is tied)
7

The Display Property & Overflow

The Display Property

Every element has a default display value that determines how it flows in the page:

ValueBehaviorDefault For
blockTakes full width, stacks vertically<div>, <p>, <h1><h6>
inlineFlows with text, no width/height<span>, <a>, <strong>
inline-blockInline flow BUT respects width/height<img>, <button>
flexEnables Flexbox on childrenSet manually
gridEnables Grid on childrenSet manually
noneRemoves element from the page entirelySet manually (hide elements)

Overflow — Handling Content That Doesn't Fit

css
/* Content overflows the box: */
.card {
    overflow: visible; /* Default — content spills out */
    overflow: hidden;  /* Cuts off extra content */
    overflow: scroll;  /* Always shows scrollbars */
    overflow: auto;    /* Shows scrollbars only when needed ✅ */
}

/* Horizontal scrolling container (image carousel) */
.carousel {
    display: flex;
    overflow-x: auto;
    gap: 1rem;
    scroll-snap-type: x mandatory;
}

.carousel img {
    scroll-snap-align: start;
    flex-shrink: 0;
    width: 300px;
}

Z-Index & Stacking Context

z-index controls which elements appear on top when they overlap. It only works on positioned elements (relative, absolute, fixed, sticky).

css
/* Dropdown menu above other content */
.dropdown {
    position: absolute;
    z-index: 10;
}

/* Modal overlay above everything */
.modal-backdrop {
    position: fixed;
    z-index: 100;
}

/* Toast notification — highest layer */
.toast {
    position: fixed;
    z-index: 1000;
}
💡 Z-Index Scale
Use a consistent z-index scale across your project: 1 (default), 10 (dropdowns), 100 (modals), 1000 (toasts). Don't use random numbers like 9999.
8

Shadows, Gradients & Filters

Box Shadows

css
/* Subtle shadow */
.card {
    box-shadow: 0 1px 3px rgba(0,0,0,0.12);
}

/* Elevated shadow (hover state) */
.card:hover {
    box-shadow: 0 12px 40px rgba(0,0,0,0.25);
}

/* Inset shadow (pressed button effect) */
.btn:active {
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.3);
}

/* Multiple shadows for depth */
.floating-card {
    box-shadow:
        0 1px 2px rgba(0,0,0,0.07),
        0 4px 8px rgba(0,0,0,0.07),
        0 16px 32px rgba(0,0,0,0.07);
}

Gradients

css
/* Linear gradient (top to bottom by default) */
.hero {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

/* Radial gradient (circular) */
.spotlight {
    background: radial-gradient(circle at center, #3b82f6, transparent 70%);
}

/* Gradient text effect */
.gradient-title {
    background: linear-gradient(90deg, #3b82f6, #8b5cf6);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

Backdrop Blur & Filters (Glassmorphism)

css — Glassmorphism Card
.glass-card {
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 12px;
}

/* Image filters */
img.muted    { filter: grayscale(100%); }
img.muted:hover { filter: grayscale(0%); transition: filter 0.3s; }
9

CSS Reset & Best Practices

The Modern CSS Reset

Every project should start with a reset to eliminate browser inconsistencies:

css — Minimal Modern Reset
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    scroll-behavior: smooth;
}

body {
    min-height: 100vh;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

img, picture, video, canvas, svg {
    display: block;
    max-width: 100%;
}

input, button, textarea, select {
    font: inherit;
}

a {
    text-decoration: none;
    color: inherit;
}

Common CSS Mistakes to Avoid

❌ Mistake✅ Better Approach
Using px for font sizesUse rem for scalability
Fixed widths like width: 800pxUse max-width: 800px; width: 100%
Using !important to fix bugsFix your selector specificity instead
Margins on the body elementAlways reset body { margin: 0; }
No box-sizing: border-boxAdd it to your reset — always
Not using gap in Flex/GridUse gap instead of margin hacks
Hard-coding colors everywhereUse CSS Variables (--primary)

The CSS Pro Roadmap

Level Focus Key Concept to Master
BeginnerFundamentalsBox Model & Selectors
IntermediateAlignmentFlexbox & Grid
AdvancedAdaptabilityMedia Queries & rem/em
ProMaintainabilitySass, BEM & CSS Variables