1. What Is CSS?
CSS (Cascading Style Sheets) is a declarative stylesheet language used to describe the visual presentation, layout, and styling of documents written in HTML.
HTML = Structure (The skeleton, headings, paragraphs, and buttons)
CSS = Presentation (Colors, typography, grid alignment, spacing, and animations)
Browser = Render Engine (Merges HTML DOM + CSSOM into rendered pixels on the screen)
2. How the Browser Renders CSS (DOM + CSSOM)
DOM + CSSOM
Browser parses HTML into the DOM Tree and CSS stylesheets into the CSSOM Tree.
Render Tree
Combines visible DOM nodes with their computed CSS styles (omitting display: none nodes).
Layout & Paint
Calculates exact coordinates and geometry (Layout) and draws colors, shadows, and text onto the screen (Paint).
3. Adding CSS: Inline, Internal & External
1. External Stylesheet (Recommended)
Cached by browser, separation of concerns, reusable across all pages.
2. Internal <style>
body { margin: 0; }
</style>
Good for single-page standalone demos or critical above-the-fold CSS.
3. Inline style="" (Avoid)
High specificity overrides, zero caching, hard to maintain.
4. CSS Syntax & Anatomy
.card {
background-color: #1e1b4b; /* Declaration */
color: #ffffff;
padding: 24px;
border-radius: 12px;
}
5. Master Guide to CSS Selectors
| Selector Type | Syntax Example | What It Targets |
|---|---|---|
| Element Selector | p { ... } | All <p> elements in the document |
| Class Selector | .btn-primary { ... } | Elements with class="btn-primary" (reusable) |
| ID Selector | #main-nav { ... } | Unique element with id="main-nav" (high specificity) |
| Descendant Combinator | article p { ... } | Any <p> anywhere inside an <article> |
| Direct Child Combinator | ul > li { ... } | Only <li> that are immediate direct children of <ul> |
| Grouping Selector | h1, h2, h3 { ... } | Applies shared rules to multiple selectors |
6. Understanding the Cascade & Specificity Math
When multiple rules target the same element, the browser resolves conflicts using 3 criteria:
- Importance & Origin: User agent styles ➔ Author styles ➔
!importantrules. - Specificity Scoring:
(Inline, ID, Class/Attribute/Pseudo-class, Element). - Source Order: If specificity is equal, the rule written lowest down in the stylesheet wins.
Inline Styles
style="color: red;"
ID Selectors
#navbar
Classes & Pseudo
.btn:hover
Elements
p, h1, div
7. Colors & Background Gradients
color: #4f46e5; /* HEX */
color: rgb(79, 70, 229); /* RGB */
color: hsl(243, 75%, 59%); /* HSL (Hue, Saturation, Lightness) */
color: rgba(79, 70, 229, 0.8); /* With 80% Alpha Transparency */
/* Gradient backgrounds */
background: linear-gradient(135deg, #6366f1 0%, #a855f7 100%);
8. CSS Units: Absolute vs Relative
| Unit | Type | Behavior & Best Use Case |
|---|---|---|
px | Absolute | Fixed pixels. Best for borders (1px solid #ccc) and fine shadows. |
rem | Relative to Root | Scales with <html> font size (1rem = 16px). Gold standard for font-size and spacing. |
em | Relative to Parent | Scales with immediate parent element font size. Best for buttons scaling with icons. |
% | Relative to Container | Percentage of parent container width/height. Best for responsive column widths. |
vw / vh | Viewport Percentage | 100vw = 100% of viewport width; 100vh = 100% of viewport height. |
9. Typography: Crafting Readable Text
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
font-size: 1rem; /* 16px default */
font-weight: 400; /* Regular */
line-height: 1.6; /* 1.5 to 1.7 is ideal for readability */
letter-spacing: -0.01em;
color: #334155;
}
10. The CSS Box Model & box-sizing
Every single HTML element is rendered as a rectangular box composed of 4 concentric layers:
- Content: The raw text, image, or child elements.
- Padding: Transparent inner clearance surrounding the content.
- Border: Solid, dashed, or stylized perimeter enclosing the padding.
- Margin: Transparent outer space separating the element from neighbors.
box-sizing: border-box globally:*, *::before, *::after { box-sizing: border-box; }11. Width, Height & Responsive Constraints
width: 100%; /* Expand to fill parent on mobile */
max-width: 1200px; /* Never exceed 1200px on ultra-wide desktop monitors */
margin: 0 auto; /* Center horizontally */
min-height: 100vh; /* Fill full screen height */
}
12. Borders, Rounded Corners & Box Shadows
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 16px; /* Smooth rounded corners */
/* box-shadow: X-offset Y-offset Blur Spread Color */
box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.3), 0 8px 10px -6px rgba(0, 0, 0, 0.2);
}
13. The Display Property
block
Starts on a new line, takes 100% full container width (div, p, h1).
inline
Flows within text, ignores width and height (span, a, strong).
inline-block
Flows horizontally on the same line while respecting custom width, height & padding.
none
Removes the element completely from document layout flow.
14. CSS Positioning Strategies
| Position Value | Document Flow | Offsets (top/left/right/bottom) Reference |
|---|---|---|
static (Default) | Normal Flow | Offsets are ignored. |
relative | Normal Flow | Offset relative to its own default static position without affecting neighbors. |
absolute | Removed from Flow | Positioned relative to its closest positioned ancestor (position: relative). |
fixed | Removed from Flow | Anchored directly to the viewport window (e.g. sticky navbar, floating back-to-top button). |
sticky | Hybrid Flow | Acts as relative until scroll position reaches an offset (e.g. top: 0), then sticks. |
15. Handling Overflow
overflow: hidden
Clips any text or images that spill outside the container box.
overflow: auto
Automatically adds scrollbars only when content exceeds the box dimensions.
16. Interactive UI Elements: Buttons & Links
display: inline-flex;
align-items: center;
gap: 8px;
background: #6366f1;
color: #ffffff;
padding: 10px 20px;
border-radius: 10px;
font-weight: 700;
border: none;
cursor: pointer;
transition: all 0.2s ease;
}
.btn-primary:hover {
background: #4f46e5;
transform: translateY(-2px);
}
17. Pseudo-Classes (:hover) & Pseudo-Elements (::after)
Pseudo-Classes (State)
:hover— Pointer hovers over element:focus-visible— Keyboard focus ring:active— Moment mouse is pressed down:nth-child(2n)— Alternating table rows
Pseudo-Elements (Virtual DOM)
::before— Inserts decorative content before::after— Inserts decorative content after::placeholder— Styles input placeholder text- Must include
content: "";
18. Responsive Design & Media Queries
.grid-layout {
display: grid;
grid-template-columns: 1fr;
gap: 16px;
}
/* 2. Tablet Enhancement (>= 768px: 2 Columns) */
@media (min-width: 768px) {
.grid-layout {
grid-template-columns: repeat(2, 1fr);
}
}
/* 3. Desktop Enhancement (>= 1024px: 3 Columns) */
@media (min-width: 1024px) {
.grid-layout {
grid-template-columns: repeat(3, 1fr);
}
}
19. CSS Custom Properties (Variables)
--primary: #6366f1;
--bg-surface: #ffffff;
--text-main: #0f172a;
--radius-md: 12px;
}
/* Dark mode toggle with zero duplicated styles! */
[data-theme="dark"] {
--bg-surface: #090714;
--text-main: #f8fafc;
}
.card {
background: var(--bg-surface);
color: var(--text-main);
border-radius: var(--radius-md);
}
20. Organizing CSS & BEM Methodology
The BEM (Block, Element, Modifier) naming pattern prevents CSS naming collisions:
.card { ... }
/* Element (Inside Block, double underscore) */
.card__title { ... }
.card__button { ... }
/* Modifier (Variation, double hyphen) */
.card--featured { ... }
.card__button--disabled { ... }
21. Common CSS Beginner Gotchas & Mistakes
1. Hardcoded Fixed Widths
Using width: 500px causes horizontal scroll on 375px mobile screens. Use max-width: 500px; width: 100%;.
2. Specificity Wars (!important)
Overusing !important breaks the cascade and makes code unmaintainable. Use semantic class names instead.
3. Margin Collapse
Vertical margins of adjacent block elements collapse into a single margin equal to the largest value.
22. A Practical Example: Complete Styled Card
background: #110e1c;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 16px;
padding: 24px;
display: flex;
flex-direction: column;
gap: 12px;
transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.feature-card:hover {
transform: translateY(-4px);
box-shadow: 0 12px 24px rgba(99, 102, 241, 0.25);
}
23. Professional CSS Best Practices
- Always set
box-sizing: border-boxin a universal reset. - Use
remfor font-size and spacing to respect browser accessibility preferences. - Adopt a Mobile-First design pattern with
min-widthmedia queries. - Use CSS Custom Properties (variables) for design tokens (colors, radii, spacing).
- Never use
outline: nonewithout a custom high-contrast:focus-visiblering.
24. What You Should Learn Next
Now that you understand the core mechanics of CSS syntax, cascade, and the Box Model, you are ready for Phase 3 layout systems:
- Flexbox (1D Layouts): Aligning navbars, centering elements, and fluid rows.
- CSS Grid (2D Layouts): Building full-page grids, dashboard cards, and photo galleries.
- Transitions & Animations: Smooth interactive micro-animations and keyframes.