Two-Column Flexbox with Sticky Sidebar + Anchor Nav
Goal: Create a two-column page with Flexbox: a 300px sticky sidebar on the left and a scrollable content column on the right. Nav links jump to section IDs.
What you'll learn:
Create your HTML with a semantic skeleton. Do not change class names; add your content where noted.
Your HTML should include:
<header> with an <h1> for your page title and a <p> for a description<main class="container"> element<nav class="sidebar"> with 5 anchor links. Use <a href="#"></a> as placeholders for now—we'll connect them to sections next.<div class="content"> with 3-5 sections. Give each section an id attribute labeled id="section-1", id="section-2", etc.<div class="deco" id="deco-1"> for decoration. Give each deco div a unique id: id="deco-1", id="deco-2", etc.<footer> with basic info (note: place this outside the <main> element)<footer> should be placed outside (after) the <main> element. The <main> element represents the dominant content of your page—in this case, the two-column layout with sidebar and content. The footer contains supplementary information and isn't part of that flex layout.
Now that you have your structure, you need content to fill it! We've provided sample flexbox-themed content for you to use.
What you'll get:
id="section-1", id="section-2", etc.?href="#section-1")?<div class="deco"> with a unique id inside each section (e.g., id="deco-1")?<footer> placed outside the <main> element?What advantages do you see in giving each section a unique id? Where could id vs. class trip you up?
Question: What element do you think the flexbox parent is? Why?
Once you've identified the flexbox parent, add CSS to make it a flex container. This sets up your two-column layout.
Give the sidebar a fixed 300px width that won't grow or shrink. Let the content area flex to fill the remaining space.
For the sidebar (.sidebar):
width: 300px;flex-grow: 0; (don't grow)flex-shrink: 0; (don't shrink)flex-basis: 300px; (start at 300px)padding, background, and border-radiusFor the content (.content):
flex-grow: 1; (grow to fill space)flex-shrink: 1; (can shrink if needed)flex-basis: auto; (start with content size)Let's break down what you accomplished with those flex properties:
flex-grow: 0; — The sidebar won't expand when there's extra spaceflex-shrink: 0; — The sidebar won't shrink when space gets tightflex-basis: 300px; — The sidebar starts at 300px and stays thereflex-grow: 1; — The content expands to fill all available spaceflex-shrink: 1; — The content can shrink if the viewport gets smallerflex-basis: auto; — The content starts with its natural size, then adjusts0, 0), it claims its 300px and refuses to budge. The content area, with grow: 1, says "I'll take whatever's left!" This is how you create fixed-width sidebars with flexible content areas—a common pattern in web design.
What's the difference between width and flex-basis in your mental model? When might you prefer one over the other?
Now let's make the sidebar stay pinned while you scroll the page. This is pure CSS—no JavaScript needed!
📚 First, do some research:
position: stickyposition propertytop property does with sticky positioningmax-height and overflowYour task: Add CSS to .sidebar to make it stick to the top of the viewport when scrolling.
overflow: hidden on parent elements). Keep .container simple!
top?In your words, why is position: sticky different from position: fixed here?
Verify your HTML setup:
href pointing to a section id (e.g., href="#section-1")id attribute (e.g., id="section-1")href="#section-1", the browser looks for an element with id="section-1" and scrolls to it. The # symbol tells the browser to look for an id on the current page rather than navigating to a new page.
Right now, clicking anchor links causes an instant jump. Let's add smooth scrolling and prevent headings from being cut off at the top.
📚 Research these CSS properties:
scroll-behavior on MDN
html or your sections?)scroll-margin-top on MDN
Your task: Add CSS for smooth scrolling and proper spacing when jumping to sections. Also, add some visual styling to your sections (padding, background, borders, etc.).
What problems might appear if you later add a fixed header? How would scroll-margin-top help?
Let's add visual interest to each section with thematic icons related to flexbox and web design.
📥 Download icons:
icon-flexbox.svg, icon-container.svg, etc.Now let's add the icons to each section's deco div.
For CodePen users (using Snipboard):
<img> tag:
<div class="deco" id="deco-1">
<img src="SNIPBOARD_URL_HERE" alt="">
</div>
images folder in your project directoryimages/icon-name.svgFor local files (working outside CodePen):
images folder in your project directory<div class="deco" id="deco-1">
<img src="images/icon-flexbox.svg" alt="">
</div>
alt="" (empty alt text) because these icons are purely decorative and don't convey essential information. Screen readers will skip them.
Style the icons so they add visual interest without interfering with content.
📚 Research CSS positioning:
position: absolute and how it removes elements from normal document flowpointer-events: none to prevent icons from blocking clicksYour task: Style the .deco class and the img inside it:
opacitypointer-events: noneStyle each decoration differently using their unique ids. Place icons in different positions to create visual variety.
Your task: Use id selectors to position each icon differently:
#deco-1 — Try top: -10%; left: -5%;#deco-2 — Try top: 50%; right: -5%;#deco-3 — Try bottom: -10%; left: 50%;<img> tags inside each .deco divHow did you ensure decoration didn't harm readability or usability? What would you change for dark vs. light themes?
Polish your project with these accessibility and usability improvements. Note: While optional, completing Part F demonstrates attention to accessibility and can earn you extra consideration in grading.
:focus-visible)What one improvement most increases usability? Why?
Save your reflections directly in your CodePen HTML file so they're preserved with your work.
📋 Steps:
</footer> tag)Link to this tutorial from your workspace homepage (index.html):
<a href="YOUR_CODEPEN_LINK">Flexbox Tutorial</a>
This way, your instructor can easily find all your work from your main workspace page.
Code along as you follow the tutorial →