← All Lessons Student Handout →

CiTi · Computer Coding · Year One · Unit 3

Dropdown Navigation Menus on MySite

A lesson within “My First Website & Server-Side Management”

Program Computer Coding: Year 1
Unit 3: My First Website & Server-Side Management
Duration One class period (~55 min)
Grouping Individual, with a pair “Break It” activity

Essential Question

From the Year 1 curriculum map
How do I manage large CSS projects? Can CSS be dynamic?

Curriculum Map Alignment

Verbatim from Yr 1 curriculum

Concept / Knowledge / Content: Use of CSS

  • Explain “document flow” and its implications on web design
  • Use element, ID, class, pseudo-class, and descendant selectors
  • Explain how inheritance and specificity affect CSS rule conflicts
  • Apply basic CSS properties: background, border, clear, color, float, font, height, line-height, list-style, margin, overflow, padding, position, text-align, text-indent, width, z-index

Web Tools

  • Identify technologies to enhance user experience
  • Use templates and snippets to demonstrate efficient and consistent web site development

Skills / Tasks

Demonstrates understanding in:

Advanced CSS Techniques CSS Inheritance Compatibility Issues Mastering CSS Inspection Tools Incorporating Design Elements
Standards touched: CSS Fundamentals (selectors, document flow, specificity) · Site Planning (navigation structure for a multi-page site)
  • Standard 2 Cascading Style Sheets
    • Obj 1 Understand the use of various CSS selectors
    • Obj 2 Format page layout with advanced CSS
      • Item 3 Code navigation bars
      • Item 5 Use float to position elements
    • Obj 3 Build responsive websites

Program Connections

Skills & Portrait alignment

Program Skills: What I’m Building Toward

Hands-On:

#2 Style with CSS
#7 Validate and debug my own work

Professional:

#7 Treat a mistake as data, not failure
#9 Document as you go

NYS Portrait of a Graduate

Academically Prepared Critical Thinker Creative Innovator

Guaranteed & Viable Curriculum Framework

3 Dimensions for this lesson

Integrated Academic Knowledge

Cause and effect in a rule-based system: predicting how one CSS change (float, overflow, position, rule order) ripples through the layout, then testing the prediction.

Employability Skills

Adapting a borrowed code snippet responsibly: understanding it before shipping it, testing on more than one device, and documenting known limitations.

Technical Skills

Building a CSS hover dropdown inside a navigation bar, moving snippet CSS into an external stylesheet, using browser DevTools device mode to test mobile, and adding a small-screen fallback with a media query.

Learning Intention

“Unwrapped” from the standard

Students will understand how a CSS-only dropdown shows and hides content using pseudo-classes, floats, and positioning: and will add a working dropdown to MySite, explain why it breaks when certain rules change, and identify why hover menus are unreliable on touchscreens.

Success Criteria

  • I can name the three layers of the dropdown HTML (wrapper, button, content) and what each one does.
  • I can explain how display:none and :hover work together to show the menu.
  • I can explain why overflow:hidden is on the navbar and what happens without it.
  • I can add a dropdown to every page of MySite using my external stylesheet.
  • I can describe at least two reasons a hover dropdown fails on a phone and add a small-screen fallback.

Authenticity Continuum

Where this lesson sits today
Instruction & Skill Practice
Teacher-Designed Simulation
Realistic Industry Task
Authentic Audience / Client
Product, Service, Impact, Solution

Students adapt a published snippet into their own project, which is exactly how working developers use references like W3Schools. Push further: have a classmate test the dropdown on their own phone and report what went wrong, moving toward “Realistic Industry Task” (QA testing on real devices).

Materials & Prep

  • Base code: W3Schools “Dropdown Menu in a Navigation Bar” open on the projector
  • Students’ MySite project (from the My First Website lesson) with a working top navigation bar and css/style.css
  • Sublime Text, CyberDuck, and Chrome (right-click → Inspect → mobile device icon)
  • Student Handout (y1-u3-l3-so)
  • Optional: one or two phones or tablets for real touch testing
  • Teacher prep: decide whether students may use a menu grouping like “About” (about + author pages) or must create new sub-pages

Code Anatomy

How the pieces work

The HTML: three layers

<div class="navbar"> <a href="#home">Home</a> <a href="#news">News</a> <div class="dropdown"> <!-- 1. WRAPPER: the hover target --> <button class="dropbtn">Dropdown <!-- 2. BUTTON: what you see in the bar --> <i class="fa fa-caret-down"></i> </button> <div class="dropdown-content"> <!-- 3. CONTENT: hidden until hover --> <a href="#">Link 1</a> <a href="#">Link 2</a> </div> </div> </div>

The key idea: the button and the menu are siblings inside one wrapper. Hovering the wrapper covers both, so the menu stays open while the mouse travels down into it.

The CSS, piece by piece

Show / hide

.dropdown-content starts at display:none. The rule .dropdown:hover .dropdown-content flips it to display:block. Read it right to left: “the content inside a dropdown that is being hovered.” This is the pseudo-class + descendant selector from the curriculum map.

Horizontal bar

float:left on the links and on .dropdown lines them up in a row. Floats leave the normal document flow, which causes Issue 1 below.

Floating over the page

position:absolute takes the menu out of the flow so it covers the content below instead of pushing it down. z-index:1 keeps it on top. min-width:160px and box-shadow are just looks.

The button reset

Buttons do not inherit fonts or colors like other elements. border:none, margin:0, font-family:inherit, and background-color:inherit make the button look like the other links. The caret icon needs the Font Awesome <link> in the head, or it disappears.

The Tricky CSS

Break it on purpose

These are the four things that make this snippet confusing. Each one has a one-line experiment students run during the “Break It” phase. Predict first, then test.

1Why overflow:hidden is on the navbar

When every child of .navbar is floated, the navbar has nothing left in its flow and collapses to 0px tall. The dark background vanishes. overflow:hidden forces the navbar to wrap around its floated children. It has nothing to do with hiding anything: it is a float-containing trick.

Try it: delete overflow:hidden from .navbar. The text stays, the gray bar disappears.

2Why the menu isn’t clipped (and how it suddenly gets clipped)

overflow:hidden is on .navbar and .dropdown, so why can the menu hang below them? Because neither one has a position set. An absolute element is positioned (and clipped) relative to its nearest positioned ancestor. With none, it escapes to the page. With no top or left set, it simply appears where it would have been: right under the button.

The moment a student adds position:relative to .dropdown (common when trying to line the menu up) or makes the navbar position:sticky (common when trying to keep the nav on screen), the menu is trapped inside a 47px-tall box and becomes invisible.

Try it: add position:relative; to .dropdown. Hover: nothing appears.

The fix, if they need sticky/relative: replace floats with flexbox so no clipping trick is needed: .navbar{display:flex;} and remove overflow:hidden from both .navbar and .dropdown.

3Two rules, one link: specificity and source order

.navbar a matches every link inside the navbar at any depth, including the ones inside the dropdown. That rule says float left, white text, centered. .dropdown-content a overrides it with float:none, black text, left aligned, display:block. Both selectors have the same specificity (one class + one element), so the one written later wins.

Try it: cut the .navbar a rule and paste it at the bottom of the stylesheet. The dropdown links turn white-on-white and line up sideways.

4Hover the wrapper, not the button

The show rule and the red highlight both use .dropdown:hover. If the hover were on .dropbtn instead, the menu would close the instant the mouse left the button to reach a link. The grouped selector .navbar a:hover, .dropdown:hover .dropbtn keeps the button red while you are down in the menu.

Try it: change .dropdown:hover .dropbtn to .dropbtn:hover. Move into the menu and watch the highlight drop.

Mobile Limitations

Why hover menus struggle

This snippet was designed for a mouse. Test it in DevTools device mode, and on a real phone if possible.

LimitationWhat happens
There is no hover on a touchscreen Phones fake a hover on the first tap. Behavior varies by browser: some open on tap, some open and follow the link at once, and the menu often won’t close by tapping the button again. It “sticks” open until you tap somewhere else.
Keyboard users are locked out Tabbing to the button focuses it but does not hover it, so the menu never opens. Not a phone problem, but the same root cause: hover-only design.
Floats wrap on narrow screens With five or six nav items, the bar wraps onto a second line in an uneven, ragged layout.
The menu can run off screen An absolute menu with min-width:160px on the right edge of a 375px screen pokes past the edge and causes sideways scrolling.
Hidden links are hard to find On a phone there is no mouse to “discover” the menu. Users may never know the links exist.

Year 1 fix: CSS-only fallback

Two small additions to the bottom of style.css. The first opens the menu on keyboard focus (and helps on many touch browsers). The second turns the whole bar into a stacked list on small screens, with the dropdown links always visible and indented.

/* open on keyboard focus, too */ .dropdown:focus-within .dropdown-content { display: block; } /* small screens: stack everything, show sub-links */ @media (max-width: 600px) { .navbar a, .dropdown, .dropdown .dropbtn { float: none; display: block; width: 100%; text-align: left; } .dropdown-content { display: block; position: static; box-shadow: none; } .dropdown-content a { padding-left: 32px; } }

position:static puts the menu back into the normal flow so it pushes content down instead of floating over it. The real-world answer is a tap-to-open “hamburger” menu built with JavaScript; name it, and tell students it is coming later.

Teacher Watch List

MySite integration
  • CSS goes in css/style.css, not a <style> block. The snippet ships as embedded CSS; students copy it as-is and then only the homepage has a dropdown.
  • The nav HTML must be pasted into every page. Change one page, change them all. (Reusable includes come in a later lesson.)
  • Link paths differ by page. A link that works from mysite/index.html may break from mysite/about/index.html. Test every link from every page on the live server.
  • Font Awesome link must go in the <head> of every page, or the caret disappears.
  • Leftover class names. If MySite already has a .navbar or nav a rule, it will fight the snippet. Use DevTools → Elements → Styles to see which rule is crossed out.
  • Placeholder links. No href="#" left behind; every dropdown link goes to a real page.

Lesson Flow

~55 minutes
TimePhaseWhat happens
5 min Warm-Up / Hook Show a large site (a college or retail site) with dropdown menus on the projector, then the same site in DevTools phone mode. Ask: “On a laptop you hover. What do you do on a phone, where there is no mouse?” Quick pair talk.
10 min Direct Instruction: Anatomy Open the W3Schools base code. Walk the three HTML layers, then the show/hide rule. Use DevTools to force :hover on .dropdown (Elements → :hov) so the menu stays open while you inspect it.
15 min Guided Practice: Add to MySite Students pick one nav item to turn into a dropdown of 2–3 related pages. Move the snippet CSS into css/style.css, add the Font Awesome link, replace # links with real MySite paths. Paste the nav into every page.
10 min Break It On Purpose In pairs, run the four “Try it” experiments from The Tricky CSS. For each: write a prediction on the handout, test it, record what happened, then undo. Debrief the position:relative clipping and source-order results as a class.
10 min Mobile Test & Fallback Students switch to device mode (iPhone size), record what goes wrong on the handout, then add the :focus-within rule and media query. Re-test at phone width and with the Tab key.
5 min Publish & Exit Ticket Upload updated pages and style.css via CyberDuck; test the live URL. Exit ticket on handout.

Assessment

Formative: Live MySite check: dropdown works on every page, all dropdown links go to real pages, menu stacks correctly at phone width. Handout “Break It” predictions and results reviewed for reasoning, not correctness of the prediction.

Exit ticket (completion-graded): “Give one reason a hover dropdown doesn’t work well on a phone, and explain what your media query does about it.”

Extension: Rebuild the navbar with display:flex instead of floats and make it position:sticky; top:0; without losing the dropdown (Issue 2 fix).