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.
/* Part B */ comment in the CSS panel.
What you'll learn:
Open the CodePen starter template. You are not writing any HTML in this tutorial. Your CodePen starter already contains the full page structure and all the text. Part A is a guided tour so you understand what you'll be styling in Parts BβF. Spend a few minutes hereβit makes everything after it easier.
Open the HTML panel in your CodePen and find each of these:
<header> β the page title and description. You can leave this alone for now.<main class="container"> β the box that wraps the sidebar and the content. This is the Flexbox parent you'll style in Part B.<nav class="sidebar"> β five links that are already pointing at #section-1 through #section-5.<div class="content"> β the box that holds the five <section> blocks.<section> elements, each with a unique id (id="section-1" β¦ id="section-5"), an <h2>, paragraphs, and an empty <div class="deco" id="deco-1"> you'll fill in Part E.<footer> placed outside (after) the <main> element..container, .sidebar, .content, .deco, #section-1β¦#section-5, and #deco-1β¦#deco-5. If you rename a class or id, the hints stop working and your layout breaks. You only ever add to this codeβyou never rename it.
<main>? The <main> element represents the dominant content of the pageβhere, the two-column sidebar + content layout. The footer is supplementary information, so it sits outside that layout. Keeping it outside <main> also means it won't accidentally become a flex item when you style .container in Part B.
* { box-sizing } rule and the body rule)βleave that alone. Below it you'll see comment markers: /* Part B */, /* Part C */, and so on. Write each step's CSS under its matching comment. The CSS panel is the only thing you edit in Parts BβF.
<main class="container"> that wraps the sidebar and content<section> elements there are, and what their ids are<div class="deco"> β what is its id?<footer> is inside or outside <main>/* Part B */ comment where your first code goesWhat 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.
You want the sidebar to stay a fixed 300px and never get squished, and the content to take all the leftover space. That's just three lines of Flexbox:
.sidebar {
width: 300px;
flex-shrink: 0;
}
.content {
flex: 1;
}
In plain English:
width: 300px; β the sidebar is 300px wide (a property you already know).flex-shrink: 0; β never let it get squished below 300px.flex: 1; on the content β take whatever space is left over.That's the whole layout. Add a little styling to the sidebar so you can see itβreveal the hints if you want specifics.
flex: 1 really meansYou don't need this to finish the projectβyour layout already works. Expand it only if you're curious why those lines do what they do.
In your own words, what does flex: 1 do? And why does the sidebar need flex-shrink: 0?
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 positioningYour 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?
Your starter already wires the navigation for youβyou don't build it. In the HTML panel, find one matching pair:
.sidebar with an href like href="#section-1"<section> with the matching 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. This is exactly why Part A warned you not to rename the section idsβrenaming them would break this navigation.
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.