CiTi · Computer Coding · Year One · Unit 3
A lesson within “My First Website & Server-Side Management”
How do I manage large CSS projects? Can CSS be dynamic?
Demonstrates understanding in:
Advanced CSS Techniques CSS Inheritance Compatibility Issues Mastering CSS Inspection Tools Incorporating Design ElementsHands-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
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.
Adapting a borrowed code snippet responsibly: understanding it before shipping it, testing on more than one device, and documenting known limitations.
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.
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.
display:none and :hover work together to show the menu.overflow:hidden is on the navbar and what happens without it.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).
css/style.cssThe 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.
.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.
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.
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.
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.
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.
overflow:hidden is on the navbarWhen 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.
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.
.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.
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.
This snippet was designed for a mouse. Test it in DevTools device mode, and on a real phone if possible.
| Limitation | What 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. |
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.
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.
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.mysite/index.html may break from mysite/about/index.html. Test every link from every page on the live server.<head> of every page, or the caret disappears..navbar or nav a rule, it will fight the snippet. Use DevTools → Elements → Styles to see which rule is crossed out.href="#" left behind; every dropdown link goes to a real page.| Time | Phase | What 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. |
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).