๐Ÿ“š Sanjay's Study Notes Home Roadmap HTML CSS JavaScript TypeScript Node SQL & Prisma React Tailwind Auth Apps Git & Deploy

02 ยท CSS Basics

CSS = Cascading Style Sheets. HTML says what something is; CSS says how it looks โ€” colors, spacing, borders, layout.

Why "Cascading" Style Sheets? Cascade = like water flowing down a series of steps. Many style rules can land on the same element, and they flow down and combine โ€” when two rules disagree, the more specific (or later) one wins, like water settling at the lowest step. That controlled "flowing down and layering" of rules is the cascade. A style sheet is just a sheet (file) of style rules โ€” the word comes from print design, where a "style sheet" listed how a document should look.

1. Where CSS lives

CSS goes in its own file (e.g. style.css). Each HTML page opts in with one line inside <head>:

<link rel="stylesheet" href="style.css">
Why a separate file? One stylesheet, many pages. Change a color once and every linked page updates. That's why your <h1> color appeared on both index and members pages at once.

2. A rule = who + what

h1 { color: darkslateblue; } selector (WHO) property value
Selector picks the target; each property: value; line changes one thing.
Punctuation matters โ€” CSS fails silently Colon between property and value, semicolon after every line, curly braces around the block. Miss a semicolon and the rules after it quietly stop working โ€” no error message, things just don't apply. Also: a misspelled property (like margin-bottum) is silently ignored.

3. Kinds of selectors

SelectorTargetsExample
h1Every tag of that typeall headings
.low-stockAnything with class="low-stock" (note the dot)just the rows you tagged
th, tdBoth, at once (comma = "and")header + data cells
nav aAn <a> inside a <nav> (space = "descendant")only navbar links
Gotcha you hit You colored links white with a { color: white; } โ€” which hits every link on the site, a trap waiting to make some future link invisible. The fix was the descendant selector nav a { color: white; } so only navbar links change. Prefer the narrowest selector that does the job.

4. The box model

Every element is a box with four layers. This is the single most important idea in CSS layout.

margin โ€” space OUTSIDE (pushes others away) border padding โ€” space INSIDE content
content โ†’ padding (inside the border) โ†’ border โ†’ margin (outside, pushes neighbors).
PropertyWhereEffect
paddingInside the borderBreathing room around content (your table cells)
borderThe wall1px solid black = thickness, style, color
marginOutside the borderPushes other elements away
Why "margin" and "padding"? Both come from the physical world of paper. A margin is the blank border around a printed page โ€” the space outside the text, keeping pages apart. Padding is stuffing placed inside something (like a padded envelope) โ€” cushioning between the content and its edge. So: margin = outside space, padding = inside cushion. The everyday meanings tell you exactly which is which.

5. Block vs inline

Boxes come in two default behaviors โ€” this explains a lot of "why won't it move?" moments.

BLOCK <h1> โ€” full width, starts a new line <p> โ€” stacks below, full width INLINE <a> <label> <input> flow side by side, like words in a sentence โ†’
Block = own line, full width. Inline = flows along the line.

Switch between them with display: block; or display: inline;. That's how you turned form labels into stacked rows (label { display: block; }) and retired the <br> hack.

Gotcha you hit โ€” and figured out margin-bottom on an inline element does nothing. Inline boxes have no vertical box to push from โ€” top/bottom margin is ignored (left/right still work). Fixes: put the margin on a block element instead, or use display: inline-block; (flows inline, but gains a real box that respects vertical margin).

6. Flexbox โ€” layout made sane

Set display: flex; on a parent and its direct children line up in a row. It's a conversation: the switch goes on the parent, the children fall into line.

nav {
  display: flex;
  gap: 20px;
  background-color: darkslateblue;
  padding: 12px;
}
display: flex โ†’ children in a row โ† gap: 20px between them justify-content: space-between justify-content: center
The container controls direction, spacing (gap), and distribution (justify-content).
Property (on the parent)Does
display: flexTurns on flex; children become a row
gapEven space between children
justify-contentSpread along the row: center, space-betweenโ€ฆ
align-itemsLine up on the cross axis: center is the workhorse
Why flexbox exists Before ~2015, laying out boxes meant abusing float (made for wrapping text around images), inline-block with margin hacks, or even <table> tags for layout. All fragile. Flexbox was the first tool built for arranging boxes โ€” centering and even spacing became one line each. (Its 2D sibling, CSS Grid, comes later.)
Remove dead code When a rule does nothing (like an empty leftover a { }), delete it and refresh. If nothing changes, it was dead weight. Messy CSS is where bugs hide.

โ† Prev: HTML Basics  ยท  Back to contents