Advanced CSS & Responsive Design Cheatsheet
Quick reference for all responsive design concepts covered in Lecture 5.
CSS Custom Properties (Variables)
Define once in :root, use everywhere with var().
1
2:root {
3 --color-primary: #C9A84C;
4 --space-md: 1.5rem;
5 --text-xl: clamp(1.5rem, 4vw, 2rem);
6}
7
8
9h1 {
10 color: var(--color-primary);
11 margin-bottom: var(--space-md);
12 font-size: var(--text-xl);
13}
14
15
16p {
17 color: var(--color-primary, #FFD700);
18}
Pro Tip: Name variables by purpose, not value: --color-primary not --gold-color. If you rebrand, the name still makes sense.
Responsive Units
rem Quick Reference
The clamp() Function
Fluid sizing: clamp(minimum, preferred, maximum)
1
2h1 {
3 font-size: clamp(2rem, 6vw, 3.5rem);
4}
5
6
7section {
8 padding: clamp(1rem, 4vw, 3rem);
9}
How it works:
- Minimum: Never go smaller than this (e.g.,
2rem) - Preferred: Use this value (scales with viewport, e.g.,
6vw) - Maximum: Never go bigger than this (e.g.,
3.5rem)
Media Queries (Mobile-First)
Base CSS targets mobile. Use min-width to enhance for larger screens.
1
2.grid {
3 grid-template-columns: 1fr;
4}
5
6
7@media (min-width: 768px) {
8 .grid {
9 grid-template-columns: repeat(2, 1fr);
10 }
11}
12
13
14@media (min-width: 1024px) {
15 .grid {
16 grid-template-columns: repeat(3, 1fr);
17 }
18}
19
20
21@media (min-width: 1280px) {
22 .grid {
23 grid-template-columns: repeat(4, 1fr);
24 }
25}
Syntax Breakdown
1@media (min-width: 768px) { ... }
2 ^ ^ ^
3 | | |
4 rule condition breakpoint value
Standard Breakpoints
Key Rule: Mobile-first = min-width. Desktop-first = max-width. Pick min-width.
CSS Transitions
Smooth animation between property states.
1
2.card {
3 transition: transform 0.3s ease, box-shadow 0.3s ease;
4}
5
6
7.card:hover {
8 transform: translateY(-8px);
9 box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
10}
Syntax
1transition: property duration timing-function delay;
2transition: transform 0.3s ease 0s;
Performance Rule: Only animate transform and opacity. These are GPU-accelerated. Avoid animating width, height, margin, padding.
CSS Transform Functions
1
2transform: translateY(-8px);
3transform: translateX(10px);
4
5
6transform: scale(1.05);
7transform: scale(0.95);
8
9
10transform: rotate(45deg);
11
12
13transform: translateY(-10px) scale(1.02);
Responsive Images
1
2img {
3 max-width: 100%;
4 height: auto;
5}
6
7
8.card-image img {
9 width: 100%;
10 height: 100%;
11 object-fit: cover;
12}
13
14
15.image-container {
16 overflow: hidden;
17}
18
19.image-container img {
20 transition: transform 0.3s ease;
21}
22
23.image-container:hover img {
24 transform: scale(1.05);
25}
position: sticky
Sticks an element when it reaches a scroll threshold.
1.nav {
2 position: sticky;
3 top: 0;
4 z-index: 100;
5}
Requirements:
- Must set
top, bottom, left, or right - Parent must NOT have
overflow: hidden - Works within the parent's scroll context
The ::before Pseudo-Element (Overlays)
1.hero {
2 position: relative;
3 background: url('image.jpg') center / cover;
4}
5
6.hero::before {
7 content: '';
8 position: absolute;
9 top: 0;
10 left: 0;
11 right: 0;
12 bottom: 0;
13 background: linear-gradient(to bottom, rgba(0,0,0,0.7), rgba(0,0,0,0.4));
14}
15
16.hero-content {
17 position: relative;
18 z-index: 1;
19}
Common Mistakes to Avoid
1. Misspelled Variable Name
1
2color: var(--color-primry);
3
4
5color: var(--color-primary);
6
7
8color: var(--color-primary, #C9A84C);
2. max-width in Mobile-First Code
1
2@media (max-width: 768px) { ... }
3
4
5@media (min-width: 768px) { ... }
3. Missing Viewport Meta Tag
1
2<meta name="viewport" content="width=device-width, initial-scale=1.0">
4. Transition on Hover Only
1
2.card:hover {
3 transition: transform 0.3s ease;
4 transform: translateY(-8px);
5}
6
7
8.card {
9 transition: transform 0.3s ease;
10}
11.card:hover {
12 transform: translateY(-8px);
13}
5. Using transition: all
1
2.card { transition: all 0.3s ease; }
3
4
5.card { transition: transform 0.3s ease, box-shadow 0.3s ease; }
VS Code Shortcuts
Chrome DevTools Responsive Mode
Opening Responsive Mode
- Open DevTools (
F12 or Ctrl + Shift + I) - Click the Device Toolbar icon (phone/tablet icon) or press
Ctrl + Shift + M - Select device presets (iPhone SE, iPad, etc.) or drag the viewport handles
Testing Workflow
- Start at 320px width (smallest phones)
- Slowly drag wider — watch for layout breaks
- Check at 375px (iPhone), 768px (iPad), 1024px (laptop), 1440px (desktop)
- Verify nothing is cut off, overlapping, or unreadable at each size
Debugging Media Queries
- Add
background: red !important; inside a media query to see exactly when it activates - Check the Computed tab to see which styles are currently applied
- Look for the media query badges in the Styles panel
Quick Reference Table
What's Next?
In Lecture 6: Landing Page Components, you'll learn:
- Tailwind CSS — utility-first CSS framework
- Building components with just class names
- No more writing custom CSS rules
- Rapid UI development workflow
> "You wrote a LOT of CSS today. What if there's a framework that does all this with just class names? Meet Tailwind CSS."
Keep this cheatsheet handy while working on your assignment!