CSS Fundamentals That Actually Matter
Forget the fancy frameworks for a minute. These are the CSS concepts you need to genuinely understand before reaching for any tool.
CSS Fundamentals That Actually Matter
I've seen developers jump straight into Tailwind or styled-components without understanding what's happening underneath. It works, until it doesn't. Then they're stuck staring at a layout that refuses to behave, with no idea why.
This post covers the CSS concepts I wish someone had drilled into me earlier. Not everything, just the stuff that actually trips people up in real projects.
The Box Model Isn't Optional
Every element on the page is a box. You've heard this before. But do you actually think in boxes when you're debugging layout issues?
/* The default box model is unintuitive */
.card {
width: 300px;
padding: 20px;
border: 1px solid #ccc;
}
/* Actual rendered width: 300 + 20*2 + 1*2 = 342px. Surprised? */
This catches everyone at some point. The fix is simple, and you should set it globally on every project:
*,
*::before,
*::after {
box-sizing: border-box;
}
With border-box, padding and border are included inside the declared width. A 300px element stays 300px. Period.
Specificity: Why Your Styles Get Ignored
CSS specificity compares selector weights after other cascade priorities have been resolved. It helps to separate those two ideas before debugging which declaration wins.
Within the same origin, importance, and cascade layer, selector specificity compares these columns from left to right:
- Type selectors -
div,p,span→ (0, 0, 1) - Class selectors -
.card,.active→ (0, 1, 0) - ID selectors -
#header→ (1, 0, 0)
Inline styles have priority over normal author stylesheet declarations. !important changes a declaration's importance; it is not another specificity column. Important user styles and active transitions can take priority over important author styles, and important declarations reverse the normal layer order. The cascade specification describes that ordering.
/* Specificity: (0, 1, 0) */
.nav-link {
color: blue;
}
/* Specificity: (0, 2, 0) - wins because two classes > one class */
.nav .nav-link {
color: red;
}
/* Specificity: (1, 0, 0) - wins when the same element matches
and the earlier cascade priorities are equal */
#main-nav {
color: green;
}
The practical rule: keep specificity low and consistent. The moment you start using IDs for styling or sprinkling !important around, you're building a house of cards. Classes are the sweet spot, specific enough to target what you need, flat enough to override when you need to.
The Cascade: Source Order Matters
When competing declarations have equal origin, importance, layer priority, specificity, and scoping proximity, the one that comes later wins. Source order is the final tie-breaker, not a shortcut around the earlier comparisons.
.button {
background: blue;
}
/* This wins - same specificity, but declared later */
.button {
background: green;
}
This is why CSS import order can matter. A reset loaded later can override component styles when the other cascade comparisons tie. A less specific reset selector does not beat a more specific component selector just because its file loads afterward.
Display, Position, and the Flow of Things
The display property controls how an element participates in layout. This is where most layout confusion starts.
Block vs Inline:
block- takes full width, stacks vertically (div,p,h1)inline- only takes as much width as needed, sits in a line (span,a,strong)inline-block- inline flow but respects width/height (useful for buttons, badges)
Position:
.tooltip {
position: absolute;
top: 100%;
left: 0;
}
absolute uses its containing block. Often that comes from the nearest ancestor whose position is not static, but properties such as transform can establish one too. If no ancestor establishes it, the initial containing block is used; in continuous media its dimensions come from the viewport. See the positioning specification.
For a tooltip that should be anchored to its wrapper, a common solution is:
.tooltip-wrapper {
position: relative; /* Creates the positioning context */
}
I can't count how many times I've debugged a tooltip or dropdown that appeared in the wrong place, only to find a missing position: relative on the parent.
Margin Collapse - The Weird One
Adjoining vertical margins of normal-flow block boxes can collapse into one margin. With two positive margins, the larger one wins:
.heading {
margin-bottom: 30px;
}
.paragraph {
margin-top: 20px;
}
/* Gap between them: 30px, NOT 50px */
Negative margins change the calculation: a positive 30px margin and a negative -20px margin collapse to 10px. If all the collapsing margins are negative, the most negative value wins. The margin-collapsing rules cover these cases.
Flex and grid item margins do not collapse. For a parent and child, padding or a border can separate the relevant margins and prevent their collapse. Adding padding or borders to adjacent siblings does not, by itself, stop their outside margins collapsing together.
Flexbox: Learn This, Use This
Flexbox handles one-dimensional layouts. It's the tool you'll reach for most often.
.navbar {
display: flex;
justify-content: space-between; /* Horizontal distribution */
align-items: center; /* Vertical centering */
gap: 1rem; /* Spacing between children */
}
The mental model: the parent (display: flex) controls how children are laid out. Children can adjust their own behavior with flex-grow, flex-shrink, and flex-basis, but I start with the parent properties when debugging distribution and alignment.
A few combos I use constantly:
/* Center anything */
.center {
display: flex;
justify-content: center;
align-items: center;
}
/* Push last item to the right */
.header {
display: flex;
align-items: center;
gap: 1rem;
}
.header .logout {
margin-left: auto;
}
/* Equal-width columns */
.grid-row {
display: flex;
}
.grid-row > * {
flex: 1;
}
CSS Grid: When Flexbox Isn't Enough
Grid handles two-dimensional layouts - rows AND columns simultaneously.
.dashboard {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-rows: 60px 1fr;
min-height: 100vh;
}
.sidebar {
grid-row: 1 / -1; /* Span all rows */
}
My rule of thumb: if you're only controlling items in one direction (a row of buttons, a stack of cards), use flexbox. If you need to control both rows and columns at once (a page layout, a dashboard grid, an image gallery), use grid.
They also compose well together. Grid for the page skeleton, Flexbox for component internals.
Responsive Design: Mobile-First
Start with the smallest screen, then layer on complexity:
.card-grid {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
}
@media (min-width: 640px) {
.card-grid {
grid-template-columns: repeat(2, 1fr);
}
}
@media (min-width: 1024px) {
.card-grid {
grid-template-columns: repeat(3, 1fr);
}
}
Mobile-first isn't just a convention. It produces cleaner CSS. Your base styles are simple, and breakpoints only add rules. Going desktop-first means your base is complex and breakpoints undo things, which gets messy quickly.
Units: When to Use What
rem- for font sizes, spacing, anything that should scale with the root font sizeem- for component-relative sizing (padding relative to its own font size)px- for borders, shadows, tiny details that shouldn't scale%- for fluid widths relative to the parentvh/vw- for viewport-relative sizing (use sparingly,dvhis more reliable on mobile)
I usually start with rem for scalable text and spacing, then use fixed or relative units where the layout calls for them.
Wrapping Up
None of this is glamorous. You won't see "I understand the box model" on anyone's LinkedIn. But these fundamentals are what separate someone who writes CSS from someone who actually controls their layouts. Every framework and utility library is built on top of these concepts. If you understand them, picking up any CSS tool becomes straightforward.