The Mental Model
Everything is a Box inside a Box.
A website isn't a canvas—it's a stack of Russian Nesting Dolls. We call this the DOM Tree (Document Object Model).
Parents control the layout. They decide how children are arranged.
Children fill the space. They inherit rules from parents.
Siblings share the same parent. They compete for space.
The Anatomy
Every element on the web has layers. Beginners mess up layouts because they confuse Padding (Internal) with Margin (External).
Think of it like a picture frame:
- MarginThe wall space around the frame. Pushes other frames away.
- BorderThe frame itself. Visible edge with color and thickness.
- PaddingThe matting inside the frame. Space between frame and photo.
- ContentThe actual photo. Your text, images, or child elements.
Pro tip: Use box-sizing: border-box to include padding and border in width calculations.
The Box Model
DevTools shows these exact colors when you inspect elements
Flexbox
display: flex
One-dimensional layout. Items flow in a single direction—row or column. Perfect for navbars, button groups, and centering.
Grid
display: grid
Two-dimensional layout. Rows AND columns simultaneously. Like a spreadsheet. This bento grid you're looking at? That's CSS Grid.
Block vs. Inline vs. Inline-Block
Block Elements <div>, <p>, <h1>
Greedy. Takes full width. Forces a new line before and after.
Inline Elements <span>, <a>, <strong>
Humble. Only takes needed space. Flows with text. Can't set width/height.
Inline-Block display: inline-block
Best of both. Flows inline but accepts width/height. Great for buttons.
Block elements stack vertically:
Inline elements flow with text:
Inline-block for buttons:
The Position Property
By default, elements follow the document flow—stacking top to bottom. The position property lets elements break free.
Static
Normal document flow. Can't use top/left/right/bottom.
Relative
Offset from its normal position. Space is preserved.
Absolute
Removed from flow. Positioned relative to nearest positioned ancestor.
Fixed
Positioned relative to viewport. Stays put when scrolling.
⚠️ Key Rule: An absolute element looks for the nearest ancestor with position: relative. If none exists, it uses the viewport. Always set position: relative on the parent container!
Z-Index & Stacking
The Z-Axis
X is horizontal. Y is vertical. Z is depth—what's on top of what. Higher z-index = closer to user.
Common Z-Index Scale
Use a consistent scale to avoid z-index wars. Here's a practical system:
Responsive Thinking
Breakpoints
Design for mobile first, then add styles for larger screens. Tailwind's breakpoints make this intuitive.
Mobile-First Example
💡 Remember: No prefix = mobile. Prefixes (sm:, md:) = that size and up.
The Exercises
Train your eyes to see the matrix.
X-Ray Vision
Browser DevTools
- →Right-click any website → Inspect
- →Hover elements in the HTML tree
- →See the colored box model overlay
Napkin Sketch
Before You Code
- →Draw a big rectangle (viewport)
- →Slice into header, main, footer
- →Label: "Flex" or "Grid" on each
Clone a Card
Hands-On Practice
- →Pick any card component online
- →Recreate with HTML + Tailwind
- →Focus on spacing, not perfection
📋 Quick Reference Cheatsheet
Centering
flex items-center justify-centerSpacing
p-4 m-4 gap-4 space-y-4Positioning
relative absolute top-0 right-0