← All Lessons Solution Walkthrough →

CiTi · Computer Coding · Year Two · Unit 1

CSS Radios: Custom Form Controls

A lesson within “Building Data-Driven Applications”

Program Computer Coding: Year 2
Unit 1: Building Data-Driven Applications
Duration Two class periods (~55 min each)
Grouping Individual build, pair compare
Deliverable A styled radio-button form page

Essential Question

From the Year 2 curriculum map
What tools can I use to streamline the process?

Every data-driven application starts with a form: it is where a user’s choice becomes data. Today’s tool is CSS itself. Browsers draw form controls their own way, but a handful of advanced selectors let a developer design data-entry forms that match any site, with no JavaScript and no extra plugins.

Curriculum Map Alignment

Verbatim from Yr 2 curriculum

Concept / Knowledge / Content: Mapping Tables

  • Create forms that will display table components with a database authoring tool
  • Edit created forms and specify appropriate field labels for data entry

Precision Web Development 2 Assessment

  • Standard 1 Fundamentals of HTML5
    • Obj 1 Use advanced HTML5 elements to create a website
      • Item 2 Code HTML forms on websites
  • Standard 2 Cascading Style Sheets
    • Obj 1 Understand the use of various CSS selectors
      • Item 5 Code pseudo element selectors, such as ::after with clear:both, or p::first-line with a color
    • Obj 2 Format page layout with advanced CSS
    • Obj 4 Code animation and graphics with advanced CSS

Skills / Tasks

Demonstrate proficiency with:

Managing Data-Driven Application Abilities Backend Administration Design & Development
Standards touched: Essential Standard ITPD01.07.02: Perform testing and validation (students test their form against a compare checklist, by mouse and by keyboard).

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:

#5 Work through ambiguity
#7 Treat a mistake as data, not failure

NYS Portrait of a Graduate

Critical Thinker Creative Innovator Academically Prepared

Guaranteed & Viable Curriculum Framework

3 Dimensions for this lesson

Integrated Academic Knowledge

Geometry and measurement: centering a 15px dot inside a 25px ring ((25 − 15) ÷ 2 = 5px), and adding padding, height, and margin to keep every row the same height. Logical ordering: a rule that can only reach elements that come after a given element, and a scoring system (specificity) that decides which of two matching rules wins.

Employability Skills

Reverse-engineering a finished product from a short technical brief, the way developers rebuild a client’s mockup. Testing beyond the happy path (keyboard as well as mouse) and comparing one’s own work against a reference without simply copying it.

Technical Skills

Hiding a native radio button while keeping it usable, drawing a replacement control with an empty element and ::before, styling checked and hover states with :checked, :hover, the adjacent sibling +, general sibling ~, descendant, and attribute selectors, positioning with relative/absolute and z-index, and reading selector specificity.

Learning Intention

“Unwrapped” from the standard

Students will understand how advanced CSS selectors let a hidden form control drive the look of other elements: and will build a fully custom radio-button form that still works by mouse and keyboard, then explain why each selector and positioning rule is needed.

Success Criteria

  • I can explain why the real radio is hidden with opacity: 0 instead of display: none, and how its label still picks it.
  • I can draw a custom circle and inner dot using an empty div, border-radius, and ::before with content: ''.
  • I can style the checked state using :checked + label and :checked ~ .check, and explain why the circle needs ~ but the label can use +.
  • I can place each circle inside its own row with position: relative and absolute, and use z-index so clicks on the circle still work.
  • I can use specificity to explain why a checked option stays green when it is hovered.

Authenticity Continuum

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

Students rebuild a finished design from a one-paragraph brief and a list of required techniques, the same way a developer rebuilds a designer’s mockup. Push further: use the technique on the data-entry form of the student’s own Unit 1 project (for example, a status or category choice in the Student Project System or Material Inventory System) styled to that project’s look, moving toward “Realistic Industry Task.”

Materials & Prep

  • The finished example running on the projector (or the demo video), so students can click and hover it
  • The brief and required-techniques list (The Challenge, below), posted or projected
  • Sublime Text, CyberDuck, Chrome with DevTools
  • Solution Walkthrough (y2-u1-l1-sol): a step-by-step build with a live preview after every step, a selector cheat sheet, a Fix It list, a self-check quiz, and a Compare Your Work checklist. Release it on Day 2, after students have made their own attempt.
  • Teacher prep: decide whether students type the HTML themselves from the structure description or start from the provided markup. The CSS is the learning target either way.

The Challenge

What students are given

The brief

This is an example of what you can do with pure CSS to style radio buttons or checkboxes. You will need to use a variety of CSS techniques: ::before, input[type=????], the descendant selector (space), the child selector (>), the adjacent sibling selector (+), the general sibling selector (~), position, and z-index.

The HTML structure

One form, one heading, one list. Every option is an <li> holding exactly three things, in this order:

<li> <input type="radio" id="Pizza" name="fave" value="pizza"> <!-- 1st: the real radio (hidden) --> <label for="Pizza">Pizza</label> <!-- 2nd: the click target --> <div class="check"></div> <!-- 3rd: the circle CSS draws --> </li>
  • Same name on every radio makes them one group, so only one can be picked, and the form sends fave=pizza.
  • for matches id exactly, capital letters included.
  • The input comes first because sibling selectors only look forward. This order is the whole trick.

The form’s action is [thisurl], a WebDNA tag that submits the form back to the same page, so students can press Save and see what the form sends in the address bar.

Key Concepts

How the pieces work

The big idea: you can’t style a radio, so fake one

CSS can barely change a native radio button. So the solution hides it and draws a new circle with a plain <div>. The hidden radio still does all the real work: it gets clicked through its label, remembers the choice, and sends the value. Its :checked state is what restyles the fake circle and the label.

Read the selectors right to left

SelectorIn plain English
input[type=radio]Attribute selector: only radios, not the submit button (also an <input>)
ul liDescendant (space): any li inside a ul
input[type=radio]:checked + labelAdjacent sibling: the label immediately after a checked radio
input[type=radio]:checked ~ .checkGeneral sibling: the circle anywhere after a checked radio in the same li (it skips over the label)
.check::beforePseudo-element: the inner dot, created with no extra HTML
li:hover labelThe label inside the row under the mouse; hovering the whole row reacts, circle included
ul > liChild: only lis directly inside the ul. In the brief, but not needed here, since there are no nested lists and ul li does the same job.

Positioning

Each li gets position: relative, so each .check with position: absolute; top: 30px; left: 20px is placed inside its own row. Without it, all four circles are placed against the page and pile up in one spot.

The z-index fix

The circle overlaps the label’s left padding. With no z-index, the element later in the HTML (the .check div) is drawn on top and swallows clicks. z-index: 1 on the label lifts it above; it has no background, so the circle still shows through.

Hiding the real radio

opacity: 0 hides it from the eyes only: Tab and arrow keys still work and the value still submits. display: none would remove it for keyboard users.

Hover vs. checked: specificity

input[type=radio]:checked + label scores (0, 2, 2) and beats li:hover label at (0, 1, 2), so a checked option stays green when hovered. The checked rule sets no padding, so the hover padding still slides it right.

Teacher Watch List

Symptom → cause
What students seeUsual cause (walkthrough step)
The font is plain, not Lato@import is not the first line of the stylesheet; the browser silently skips it (step 1)
All circles piled in one spotli is missing position: relative (step 2)
Default browser radios still showingSelector typo (no spaces inside input[type=radio]) or missing opacity: 0 (step 4)
Ring turns green but no center dot.check::before is missing content: ''; (step 6)
Circle and label don’t both light upWrong order inside the li: must be input, label, then .check (steps 0 and 7)
Clicking right on a circle does nothingLabel is missing z-index: 1 (step 8)
Clicking a word doesn’t pick itfor and id don’t match exactly, often a capital letter (step 0)
More than one option can be pickedRadios have different name values (step 0)

The walkthrough’s Fix It section lists these same symptoms for students. Point them there before answering.

Lesson Flow

Two periods, ~55 min each

Day 1: Reverse-engineer and build

TimePhaseWhat happens
5 min Warm-Up / Hook Show a plain browser radio list next to the finished example. Students click and hover the example. Ask: “Can CSS restyle that little gray circle? Make a guess how this was built.”
10 min Direct Instruction Hand out the brief. Walk the HTML structure and why the input comes first. Introduce + vs. ~, attribute selectors, and ::before with one-line demos in DevTools. Do not show the full solution.
10 min Guided Practice Together: page setup, list cleanup, and turning labels into full-row click targets (walkthrough steps 1 through 3). Stress position: relative on the li and say it will matter later.
25 min Independent Practice Students hide the radio, draw the circle and dot, and attempt the checked state on their own, using the brief’s technique list. Circulate with the Teacher Watch List.
5 min Check-In Quick poll: who has a circle, who has a dot, who has a working checked state. Students note their biggest blocker for tomorrow.

Day 2: Finish, compare, explain

TimePhaseWhat happens
5 min Warm-Up Project a broken version where clicking the circle does nothing. Pairs guess why (the z-index problem, step 8).
15 min Independent Practice Finish the checked state, hover effects, and submit button. Press Save and read fave=... in the address bar.
15 min Compare & Fix (pairs) Release the Solution Walkthrough. Pairs run the Compare Your Work checklist on each other’s forms, including the keyboard test (Tab and arrow keys). Use Fix It to repair differences in their own code rather than pasting the full solution.
10 min Direct Instruction: Specificity Hover the checked option: why does it stay green? Score li:hover label (0, 1, 2) against :checked + label (0, 2, 2) as a class. Note where a transition lives decides whether it animates both ways.
10 min Closure & Exit Ticket Students answer the walkthrough’s Check Yourself questions in their heads, then complete the exit ticket.

Assessment

Formative: Pair check against the walkthrough’s Compare Your Work list: Lato font loaded, no default radios showing, clicking a word or a circle picks one option only, checked option shows a thick green ring with a green dot and big green text, hover slides and recolors rows, hovering the checked row keeps it green, the Save button fades on hover, and Tab plus arrow keys still change the choice.

Exit ticket (completion-graded): “Explain why the circle uses ~ but the label can use +. Then explain what would break if the input came after the .check div.”

Extension: (1) Build a checkbox version: input[type=checkbox], unique name values, and a rounded-square .check. (2) Add a visible keyboard focus style with input[type=radio]:focus-visible ~ .check, so keyboard users can see which option they are on. (3) Restyle the form to match the student’s own Unit 1 project.