CSS Fundamentals
This reading refreshes the core CSS concepts you will rely on throughout the course. The box model, specificity, the cascade, resets, mobile first thinking, and media queries are not new topics, but understanding them precisely is essential before you move into advanced territory. Skim quickly where you feel confident and slow down where things feel fuzzy.
The Box Model
Every element on a web page generates a rectangular box. The box model describes how the browser calculates the size of that box from four layers: content, padding, border, and margin. Getting this right is the foundation of every layout you will build.
content-box vs. border-box
By default browsers use content-box sizing, which means the width and height properties apply only to the content area. Padding and border are added on top, so the rendered size is larger than the value you set.
/* content-box (default) */
.box {
width: 200px;
padding: 20px;
border: 5px solid black;
}
/* Rendered width: 200 + 40 padding + 10 border = 250px */
The border-box model includes padding and border inside the declared width and height. The element never exceeds the size you specify, which makes layout math far more predictable.
/* border-box */
.box {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid black;
}
/* Rendered width: 200px (padding and border included) */
The Global border-box Reset
Switching every element to border-box removes an entire category of sizing surprises. The standard reset targets all elements and their pseudo-elements so nothing slips through:
*,
*::before,
*::after {
box-sizing: border-box;
}
You will see this rule at the top of nearly every professional stylesheet. It is one of the few cases where the universal selector is the right tool for the job.
Margin Collapse
When two block-level elements sit next to each other vertically, their margins do not add together. Instead the browser collapses them into a single margin equal to the larger of the two values.
.element-one {
margin-bottom: 30px;
}
.element-two {
margin-top: 20px;
}
/* The gap between them is 30px, not 50px */
Margin collapse only affects vertical margins between block-level elements in normal flow. It does not happen with horizontal margins, with elements inside flex or grid containers, or when padding or a border separates the margins.
Specificity and the Cascade
When multiple CSS rules target the same element, the browser needs a way to decide which one wins. That decision process is called the cascade, and specificity is the primary tiebreaker within it.
How Specificity Is Calculated
Every selector carries a specificity weight based on the types of selectors it contains. Think of specificity as a three-part score where each part outranks all of the parts to its right:
- IDs carry the highest weight.
- Classes, attributes, and pseudo-classes carry medium weight.
- Elements and pseudo-elements carry the lowest weight.
Inline styles override all selector-based rules, and !important overrides everything including inline styles. Neither should be part of your regular workflow.
/* Specificity: 0-0-1 */
p {
color: blue;
}
/* Specificity: 0-1-0 */
.intro {
color: green;
}
/* Specificity: 1-0-0 */
#header {
color: red;
}
/* Specificity: 0-1-1 */
p.intro {
color: purple;
}
When two selectors have exactly the same specificity, the one that appears later in the stylesheet wins. This is the "order" part of the cascade, and it is why organizing your stylesheet from general to specific matters.
The Full Cascade Order
The browser resolves competing declarations in this order:
-
Origin and importance: Browser defaults lose to your styles, and
!importantdeclarations override normal ones. - Specificity: Higher specificity wins.
- Source order: If specificity is equal, the rule that appears later wins.
Understanding this order lets you structure your CSS so that rules layer naturally without fighting each other. When you find yourself reaching for !important, it is almost always a sign that your specificity structure needs rethinking.
Common Specificity Mistakes
Overusing IDs in selectors creates rules that are very difficult to override without resorting to more IDs or !important. Prefer classes for styling hooks and reserve IDs for JavaScript targets or anchor links. Another common trap is writing overly long selectors like div.wrapper section.content ul li a when a single class would do the same job with lower specificity and better readability.
Avoid !important in this course. It short-circuits the cascade and makes your stylesheets unpredictable. If a rule is not applying the way you expect, trace the specificity rather than forcing it.
Specificity Tools
When you need to compare complex selectors, these calculators can help:
Resets and Normalization
Browsers apply their own default styles to HTML elements. These defaults vary between browsers, which means the same HTML can look slightly different in Chrome, Firefox, and Safari. Reset and normalization stylesheets address this problem, but they take different approaches.
CSS Resets
A CSS reset strips all browser defaults down to zero. Margins, padding, font sizes, and list styles are all removed so you start from a completely blank slate. The most well-known example is Eric Meyer's Reset CSS. The advantage is total control; the disadvantage is that you must explicitly style every element because nothing has any default appearance.
/* Simplified reset example */
* {
margin: 0;
padding: 0;
}
ul, ol {
list-style: none;
}
img {
display: block;
max-width: 100%;
}
Normalization
Normalize.css takes the opposite philosophy. Instead of removing all defaults, it makes them consistent across browsers. Headings still look like headings, lists still have bullets, and form elements still have recognizable styling. The goal is a uniform starting point that preserves useful defaults rather than erasing them.
Which Approach to Use
Most modern projects use a hybrid approach: a light reset that handles the most common inconsistencies (like the border-box reset above) combined with a few targeted normalizations for elements like forms and images. You do not need to choose one camp exclusively. In this course you will build up your own baseline styles as you learn what each property does, which gives you more control and understanding than dropping in someone else's reset file.
Mobile First and Progressive Enhancement
Mobile first is a development strategy where you write your base styles for small screens and then add complexity for larger viewports using media queries. This is not just a technical preference; it reflects how the majority of people access the web today.
Why Mobile First Works
A small screen forces you to focus on content and hierarchy before decoration. Your base CSS handles typography, spacing, and a single column layout with no media queries at all. As the viewport grows, you add layout rules that take advantage of the extra space. This approach means that if a media query fails to load or a browser does not support it, the user still gets a fully functional experience.
/* Base styles: single column, no media query needed */
.container {
padding: 1rem;
}
.card {
margin-bottom: 1rem;
}
/* Larger screens: add a two-column grid */
@media (min-width: 768px) {
.container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
}
.card {
margin-bottom: 0;
}
}
Progressive Enhancement
Mobile first is one expression of a broader principle called progressive enhancement: start with a solid baseline that works everywhere, then layer on enhancements for more capable environments. This applies to CSS features as well. If a browser does not support a property, the layout should still function. You will learn how to use @supports queries for this in Unit 2.
Modern Media Query Range Syntax
Traditional media queries use min-width and max-width keywords, which work but can be confusing when you need to express a range. Modern CSS introduces a range syntax that reads more like a mathematical comparison.
Traditional Syntax
The traditional approach requires separate keywords for minimum and maximum values, and combining them into a range means writing both in the same query:
/* Traditional: 768px and wider */
@media (min-width: 768px) {
/* styles */
}
/* Traditional: between 768px and 1024px */
@media (min-width: 768px) and (max-width: 1024px) {
/* styles */
}
Range Syntax
The range syntax replaces the keyword approach with comparison operators. The same queries become shorter, clearer, and harder to get wrong:
/* Range: 768px and wider */
@media (width >= 768px) {
/* styles */
}
/* Range: between 768px and 1024px */
@media (768px <= width <= 1024px) {
/* styles */
}
The comparison operators available are >, >=, <, and <=. You can also use them with height and other numeric media features. This syntax is supported in all modern browsers and is the preferred approach going forward.
Practical Breakpoints
There is no universal set of correct breakpoints. The right values depend on your content and layout, not on specific device dimensions. That said, a common starting set for a mobile first approach looks like this:
/* Small tablets and large phones in landscape */
@media (width >= 640px) {
/* styles */
}
/* Tablets and small laptops */
@media (width >= 768px) {
/* styles */
}
/* Laptops and desktops */
@media (width >= 1024px) {
/* styles */
}
/* Large screens */
@media (width >= 1280px) {
/* styles */
}
Use these as a starting point and adjust them based on where your content actually needs to reflow. Adding breakpoints only when the layout starts to break keeps your CSS lean and intentional.
Resize your browser while developing and add a breakpoint when the layout stops looking good. This content-driven approach produces better results than targeting specific device widths.
Putting It All Together
Here is a short example that combines the concepts from this page: a global border-box reset, class-based selectors with manageable specificity, mobile first layout, and range media queries.
/* Global reset */
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
/* Base styles: mobile first, single column */
body {
font-family: system-ui, sans-serif;
line-height: 1.6;
padding: 1rem;
}
.card {
padding: 1.5rem;
border: 1px solid #ddd;
border-radius: 0.5rem;
margin-bottom: 1rem;
h2 {
margin-bottom: 0.5rem;
color: #333;
}
p {
color: #555;
}
}
/* Two columns on tablets */
@media (width >= 768px) {
body {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
}
.card {
margin-bottom: 0;
}
}
/* Three columns on desktops */
@media (width >= 1024px) {
body {
grid-template-columns: repeat(3, 1fr);
}
}
Notice that the base styles carry no media query at all. The layout starts as a single column with comfortable padding and only adds grid columns as the viewport grows. The specificity stays flat because everything uses classes, and the native nesting keeps the .card styles organized without inflating selector length.