Reading the Header
Now let's analyze a simpler component - the Header. This will help you understand basic component anatomy without the complexity of a full page.
What You'll Learn
- Basic structure of a React component
- How components import and use other components
- Component composition (components within components)
- How JSX creates visual hierarchy
- Semantic HTML5 elements
Open the File
In VS Code, open:
components/layout/Header.tsx
Keep it side-by-side with this guide!
The Complete File
Here's the entire Header component (only 29 lines!):
import Link from "next/link";
import Navigation from "./Navigation";
export default function Header() {
return (
<header className="bg-white shadow-lg border-b border-gray-100 sticky top-0 z-50 backdrop-blur-sm bg-white/95">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div className="flex justify-between items-center h-20">
<div className="flex items-center">
<Link href="/" className="group flex items-center space-x-3">
<div className="w-10 h-10 bg-gradient-to-br from-blue-600 to-purple-600 rounded-xl flex items-center justify-center shadow-lg group-hover:shadow-xl transition-all duration-200">
<span className="text-white font-bold text-lg">K</span>
</div>
<div className="flex flex-col">
<span className="text-xl font-bold text-gray-900 group-hover:text-blue-600 transition-colors duration-200">
K12worX Jamboree
</span>
<span className="text-xs font-medium text-yellow-600 -mt-1">
Launching Soon
</span>
</div>
</Link>
</div>
<Navigation />
</div>
</div>
</header>
);
}
Much simpler than the homepage! Let's break it down.
Part 1: Imports (Lines 1-2)
import Link from "next/link";
import Navigation from "./Navigation";
Line-by-Line Analysis
Line 1: import Link from "next/link"
- External library import (from Next.js)
Linkcomponent for navigation- No
@/because it's fromnode_modules
Line 2: import Navigation from "./Navigation"
- Local file import (our own component)
./means "current directory"- So this imports
components/layout/Navigation.tsx - No file extension needed (TypeScript figures it out)
Key learning: ./ imports from same folder, @/ imports from project root
Part 2: Function Declaration (Line 4)
export default function Header() {
Breaking It Down
export default:
- Makes this component available to other files
- Other files can import it:
import Header from '@/components/layout/Header'
function Header():
- Standard JavaScript function
- Name matches filename (Header.tsx → Header function)
- No parameters (this component doesn't receive props)
- Convention: Component names use PascalCase
No props needed because the Header looks the same on every page!
Part 3: Return Statement (Line 5)
return (
<header className="bg-white shadow-lg border-b border-gray-100 sticky top-0 z-50 backdrop-blur-sm bg-white/95">
The <header> Element
Semantic HTML:
<header>is HTML5 semantic element- Tells browsers and screen readers "this is the site header"
- Better for SEO and accessibility than generic
<div>
Sticky Header Classes
sticky top-0 z-50:
sticky: Header stays visible when scrollingtop-0: Sticks to top of viewportz-50: High z-index (appears above other content)
Visual styling:
bg-white: White backgroundshadow-lg: Large drop shadowborder-b border-gray-100: Bottom border
Advanced effect:
backdrop-blur-sm: Blur effect behind headerbg-white/95: White background at 95% opacity- Creates modern "frosted glass" effect when scrolling
Try this: Scroll the website and watch the header stay at the top!
Part 4: Container and Flex Layout (Lines 7-8)
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div className="flex justify-between items-center h-20">
Container Div (Line 7)
Purpose: Center and constrain content width
max-w-7xl: Maximum width (prevents it being too wide)mx-auto: Centers the contentpx-4 sm:px-6 lg:px-8: Responsive horizontal padding
This is the standard container pattern you'll see everywhere!
Flex Layout (Line 8)
Purpose: Position logo on left, navigation on right
flex: Enable flexbox layoutjustify-between: Push children to edges (space between)items-center: Vertically center childrenh-20: Fixed height of 5rem
Visual result: Logo left, navigation right, both vertically centered
Part 5: Logo Section (Lines 9-22)
<div className="flex items-center">
<Link href="/" className="group flex items-center space-x-3">
Outer Container (Line 9)
flex items-center: Logo elements horizontally aligned
Link Wrapper (Line 10)
Link href="/":
- Clicking anywhere on logo goes home
href="/"is the homepage route
className="group ...":
group: Special Tailwind class- Enables hover effects on child elements
- When you hover this link, children can react
Example: Logo can change when you hover the parent
Logo Icon (Lines 11-13)
<div className="w-10 h-10 bg-gradient-to-br from-blue-600 to-purple-600 rounded-xl flex items-center justify-center shadow-lg group-hover:shadow-xl transition-all duration-200">
<span className="text-white font-bold text-lg">K</span>
</div>
Box dimensions:
w-10 h-10: 40px × 40px square
Styling:
bg-gradient-to-br: Gradient bottom-right directionfrom-blue-600 to-purple-600: Blue to purple gradientrounded-xl: Rounded cornersshadow-lg: Drop shadow
Centering the letter:
flex items-center justify-center: Centers child (the "K")
Hover effect:
group-hover:shadow-xl: Larger shadow on parent hovertransition-all duration-200: Smooth animation (200ms)
The "K" letter:
<span>with white, bold text- Centered inside the square
Pattern: Using Tailwind gradients for colorful branded elements
Logo Text (Lines 14-21)
<div className="flex flex-col">
<span className="text-xl font-bold text-gray-900 group-hover:text-blue-600 transition-colors duration-200">
K12worX Jamboree
</span>
<span className="text-xs font-medium text-yellow-600 -mt-1">
Launching Soon
</span>
</div>
Container:
flex flex-col: Stack text vertically (column)
Site name (lines 15-17):
- Large, bold text
group-hover:text-blue-600: Changes color when hovering linktransition-colors duration-200: Smooth color change
Status badge (lines 18-20):
- Small text (
text-xs) - Yellow color to stand out
-mt-1: Negative margin (pulls it up closer to site name)
Pattern: Negative margins fine-tune spacing between elements
Part 6: Navigation Component (Line 23)
<Navigation />
Simple Component Usage
That's it! Just one line to include the entire navigation menu.
What this shows:
- Navigation is defined in another file
- We imported it at the top
- Now we use it like an HTML tag
- All the navigation logic is in
Navigation.tsx
This is component composition: Build complex UIs from simple pieces!
Component Composition Diagram
<Header> ← This file
<header>
<div> (container)
<div> (flex layout)
<div> (logo section)
<Link>
<div> (icon)
<div> (text)
<Navigation /> ← Separate component
Key insight: Header uses Navigation, but doesn't need to know how Navigation works!
Visual Structure
Here's what the code creates:
┌──────────────────────────────────────────────────────┐
│ [K] K12worX Jamboree [Nav] [Nav] [Nav] │
│ Launching Soon │
└──────────────────────────────────────────────────────┘
- Left side: Logo with icon and text
- Right side: Navigation menu
- Full width, centered content
- Sticky to top when scrolling
Key Patterns to Remember
Pattern 1: Semantic HTML
<header> // Not <div>, use semantic tags
<nav> // For navigation
<main> // For main content
<footer> // For footer
Why: Better for accessibility, SEO, and code clarity
Pattern 2: Container Pattern
<div className="max-w-7xl mx-auto px-4">
{/* Content */}
</div>
Every major section should use this to center content
Pattern 3: Flex for Horizontal Layout
<div className="flex justify-between items-center">
<div>Left content</div>
<div>Right content</div>
</div>
Common pattern for headers, navigation bars, and horizontal layouts
Pattern 4: Group Hover Effects
<div className="group">
<div className="group-hover:shadow-xl">
{/* Changes when parent is hovered */}
</div>
</div>
Interactive components use this for sophisticated hover effects
Pattern 5: Component as Single Tag
<Navigation /> // Self-closing if no children
<Header></Header> // Or with opening/closing tags
Both valid, but self-closing is more concise for components without children
Hands-On Exercise
Exercise 1: Change the Logo Letter
Change the "K" to your first initial.
Steps:
- Find the
<span>with "K" (line 12) - Change it to your initial
- Save and check the browser
You should see: Your letter in the gradient box!
Exercise 2: Change Logo Colors
Change the gradient from blue/purple to your favorite colors.
Find:
from-blue-600 to-purple-600
Change to (example):
from-green-600 to-teal-600
Try these combinations:
- Red to orange:
from-red-600 to-orange-600 - Pink to purple:
from-pink-600 to-purple-600 - Indigo to blue:
from-indigo-600 to-blue-600
Exercise 3: Update the Status Badge
Change "Launching Soon" to "Now Live" and make it green.
Find:
<span className="text-xs font-medium text-yellow-600 -mt-1">
Launching Soon
</span>
Change to:
<span className="text-xs font-medium text-green-600 -mt-1">
Now Live
</span>
Exercise 4: Make the Header Non-Sticky
Remove the sticky behavior so header scrolls away.
Find:
className="... sticky top-0 z-50 ..."
Remove: sticky top-0 z-50
Save and scroll: Header should scroll away now!
Put it back when done to restore sticky behavior.
Exercise 5: Add an Emoji to the Site Name
Find:
K12worX Jamboree
Change to:
🎓 K12worX Jamboree
Or try: 📚, 🚀, ✨, 💡, or any emoji you like!
Common Questions
Q: Why is the component file called Header.tsx not Header.jsx?
A: .tsx indicates TypeScript + JSX. .jsx is JavaScript + JSX. We use TypeScript for better type safety and code quality.
Q: What's the difference between <header> and <Header>?
A:
<header>(lowercase) = HTML element<Header>(PascalCase) = React component
Q: Can I remove the group class?
A: Yes, but you'll lose hover effects. Any group-hover:* classes on children will stop working.
Q: Why import from ./Navigation not ./Navigation.tsx?
A: TypeScript automatically resolves .tsx and .ts files. You can include the extension, but it's conventional to omit it.
Q: What if Navigation component breaks?
A: Header would show an error. This is why it's important to test after changes. Error messages will tell you what's wrong.
Troubleshooting
Problem: Logo text doesn't change color on hover
Check:
- Is
groupclass on the<Link>parent? - Is
group-hover:text-blue-600on the text element? - Are you hovering over the link (not just near it)?
Problem: Header isn't sticky anymore
Solution:
- Make sure you have
sticky top-0 z-50in the className - Check that you didn't accidentally delete it
Problem: Gradient doesn't show
Check:
- Make sure you have
bg-gradient-to-br(or other direction) - Make sure you have both
from-COLORandto-COLOR - Colors must be valid Tailwind colors
Problem: Navigation disappeared
Solution:
- Make sure
<Navigation />line is still there - Check that import at top is correct
- Look for errors in terminal
Quick Reference
Sticky header:
<header className="sticky top-0 z-50">
Container pattern:
<div className="max-w-7xl mx-auto px-4">
Flex horizontal layout:
<div className="flex justify-between items-center">
Group hover effects:
<div className="group">
<div className="group-hover:shadow-xl">
Gradient background:
bg-gradient-to-br from-blue-600 to-purple-600
Using a component:
import Navigation from './Navigation'
<Navigation />
What's Next
Now let's examine the Navigation component to see how data-driven components work: