CiTi · Computer Coding · Year Two · Unit 1

Solution Walkthrough: CSS Radios

Custom radio buttons with pure CSS, built one step at a time

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

Browsers draw radio buttons their own way, and CSS can barely change them. So this solution does not style the real radio button at all. It hides it, and draws a new circle with a plain <div>. Then it uses the hidden radio's :checked state to change how the fake circle and the label look.

  1. The real radio still does the work. It is invisible, but it still gets clicked (through its label), still remembers which one is picked, and still sends its value when the form is submitted.
  2. The label is the click target. A <label for="Pizza"> clicks the input with id="Pizza". The label is stretched to fill the whole row, so clicking anywhere on the row picks that option.
  3. Sibling selectors connect the pieces. :checked + label and :checked ~ .check mean “when this radio is checked, style the label and circle that come after it.” That only works because of the order of the HTML.

Each step below shows only the CSS that step adds, why it works, and a live preview of the form at that point. The previews are real pages: click, hover, and try them.

0The HTML: the order matters

Every option is one <li> with exactly three things inside, in this order:

1stThe real radio. It will be hidden.<input type="radio">
→
2ndThe label, right after it.<label for="...">
→
3rdThe fake circle we draw.<div class="check">
body of the page
Three things to notice
  • Same name on every radio (name="fave"). That makes them one group, so only one can be picked. When the form submits, it sends fave=pizza (or whichever was picked).
  • for matches id exactly. for="Pizza" points at id="Pizza". Capital letters count.
  • CSS sibling selectors only look forward. The input comes first so later CSS can say “the label after a checked input” and “the circle after a checked input.” If the input came last, there would be no way to reach the label and circle from it.
Live preview: HTML only, no CSS yetClick a word: the radio next to it gets picked

1Page setup

First the basics: load the Lato font, make the page dark, color the heading, and center a 500px column.

@import must be the very first line

An @import only works at the top of the stylesheet, before every other rule. If you put it lower down, the browser silently skips it. Your page will still work, but the font will be the plain default instead of Lato. This is the most common small difference between student versions and the solution.

  • margin: 40px auto centers .container. auto on the left and right splits the leftover space evenly. It only works because the box has a fixed width.
  • font-family: 'Lato', sans-serif on body is inherited by everything inside it. sans-serif is the backup if Lato doesn't load.
Live preview: after step 1

2Clean up the list

Remove the bullets and default spacing, then make every <li> a full-width row with a thin line under it. ul li is a descendant selector (a space): any li inside a ul.

The most important line here: position: relative

It does nothing you can see yet. But in step 5, the fake circle uses position: absolute. An absolute element is placed inside its nearest parent that has a position set. position: relative on the li makes each row that parent. Without it, all four circles would be placed against the whole page and pile up in one spot.

Live preview: after step 2

3Turn each label into a big click target

The label becomes a block that fills the row. The extra 80px of left padding leaves an empty space where the fake circle will go.

  • display: block makes the label stretch across the whole row. Now clicking anywhere in the row clicks the label, which picks the radio.
  • padding: 25px 25px 25px 80px means top, right, bottom, left. The big left value pushes the text over to make room for the circle.
  • height: 30px plus 50px of padding plus 20px of margin keeps every row the same height (100px), even when the text grows later.
  • transition: all 0.25s linear means that when any of these values change later (color, size, padding), they slide over a quarter second instead of jumping.
  • z-index: 1 doesn't do anything yet. You'll see why it matters in step 8.
Live preview: after step 3The default radios are still showing. Step 4 hides them.

4Hide the real radio button

input[type=radio] is an attribute selector. It matches only inputs whose type is radio. It won't touch the submit button, which is also an <input>.

  • opacity: 0 makes it invisible, but it is still there and still works. You can still Tab to it and use the arrow keys, and it still sends its value with the form.
  • position: absolute takes it out of the layout so it doesn't push anything around.
Why not display: none?

display: none removes the radio completely for the keyboard. Clicking the labels still works, but someone who uses Tab and the arrow keys could no longer pick an option. opacity: 0 hides it from your eyes only.

Live preview: after step 4No circles at all now. Clicking still picks an option; you just can't see it.

5Draw the fake circle

The empty <div class="check"> becomes a magenta ring, placed in the empty space on the left of each row.

  • border-radius: 50% turns the square box into a circle.
  • position: absolute with top: 30px; left: 20px puts it 30px down and 20px in from the corner of its li. That works because of position: relative on the li from step 2.
  • The ring is just a thick border around an empty box. In step 7, changing the border color and thickness is what makes it light up.
Live preview: after step 5Nothing changes when you pick an option yet. That comes in step 7.

6The inner dot with ::before

A radio needs a dot in the middle. Instead of adding more HTML, we make one with ::before. This creates a fake element inside .check that only exists in CSS.

  • content: '' is required. Without a content line, ::before is never drawn at all, even if every other line is perfect. The empty quotes mean “draw it, with no text in it.”
  • Centering math: the circle's inside is 25px. The dot is 15px. 25 − 15 = 10, split evenly = 5px on each side. So top: 5px; left: 5px.
  • The dot has no background yet, so it's invisible. Step 7 fills it in only when its radio is checked.
Why the dot stays centered when the border gets thicker

An absolute child measures top and left from the inside edge of its parent's border. When the border grows from 5px to 9px in step 7, the inside stays 25px, so the dot stays centered.

Live preview: after step 6Looks the same as step 5. The dot is there, but it has no color yet.

7The checked state: + and ~

This is the heart of the assignment. When a hidden radio is checked, these rules light up its circle, dot, and label.

Read the selectors right to left

SelectorIn plain English
input[type=radio]:checked + labelThe label that comes right after a checked radio. + is the adjacent sibling selector: same parent, the very next element.
input[type=radio]:checked ~ .checkAny .check that comes anywhere after a checked radio. ~ is the general sibling selector: same parent, later in the HTML.
input[type=radio]:checked ~ .check::beforeThe inner dot of that same circle.
Why ~ for the circle but + for the label?

The label sits directly after the input, so + reaches it. The circle has the label in between, so it isn't the next element and + would miss it. ~ skips over the label and finds it.

Each li is its own family. Siblings only means elements with the same parent. That's why checking Pizza lights up Pizza's circle and not Bacon's.

Live preview: after step 7Click a word. Now try clicking right on a circle. Nothing happens? Step 8 explains.

This preview turns off the z-index: 1 from step 3 so you can see the problem it solves.

8Why the label needs z-index: 1

No new CSS in this step. This explains the z-index: 1 line you already added to label in step 3.

The circle and the label overlap: the circle sits on top of the label's left padding. When two positioned elements overlap and neither has a z-index, the one later in the HTML is drawn on top. The .check div comes after the label, so it covers it. A click on the circle hits the div, which isn't connected to the radio, so nothing happens.

z-index: 1 lifts the label above the circle. The label has no background, so you can still see the circle through it, but now every click lands on the label.

Without z-indexClick a circle: nothing
With z-index: 1Click a circle: it works

9Hover effects

Hovering over a row makes it grow, turn magenta, and slide right, and the ring turns thick and white.

All three use the descendant selector: li:hover label means “the label inside a row the mouse is over.” Putting the hover on the li (not on the label) means the whole row reacts, including the circle.

Hover vs. checked: who wins?

Hover over the option you already picked. It stays green and big instead of turning magenta. Both rules match, so the browser uses specificity. Count each selector as (IDs, classes/attributes/pseudo-classes, elements):

RuleScoreResult
li:hover label0, 1, 2loses
input[type=radio]:checked + label0, 2, 2wins: label stays green
li:hover .check0, 2, 1loses
input[type=radio]:checked ~ .check0, 3, 1wins: ring stays green

The checked rule doesn't set padding, so nothing beats the hover padding. That's why a checked row still slides right when you hover it.

Where you put a transition matters

The label's transition is on the plain label rule, so it animates both ways: growing in and shrinking back. The row background's transition is only inside li:hover, so it fades in but snaps off when the mouse leaves. A transition on the normal (non-hover) rule works in both directions.

Live preview: after step 9Hover the rows, then hover the one you picked

10The submit button

  • input[type=submit] is the same attribute-selector idea as step 4, this time matching only the submit button.
  • float: right pushes it to the right edge. The rows are floated too, so it lines up below them.
  • border-radius: 40% uses a percentage, so the corners curve into a pill shape that changes with the button's size.
  • The 1-second color fade is only in the :hover rule. Like the row background in step 9, it fades to green slowly but snaps right back to magenta.
Live preview: finishedPick one and press Save to see what the form would send

The full solution

Here is the whole file in its final order. The rules are grouped a little differently than in the steps above, but the CSS is the same.

solution.html
<!DOCTYPE html> <html> <head> <title>CSS Radios</title> <style> /* Import Google Font (must be the first rule in the stylesheet or it is ignored) */ @import url('https://fonts.googleapis.com/css?family=Lato'); /* Full-page setup */ body, html { height: 100%; /* Full viewport height */ background: #222222; /* Dark background */ font-family: 'Lato', sans-serif; /* Apply imported font */ } /* Default h2 color */ h2 { color: #AAAAAA; /* Light gray */ } /* Main container */ .container { display: block; /* Default block layout */ margin: 40px auto; /* Center horizontally with 40px top/bottom spacing */ height: auto; /* Height adjusts to content */ width: 500px; /* Fixed width */ padding: 20px; /* Inner spacing */ } /* Remove default list styling */ ul { list-style: none; /* No bullets */ margin: 0; /* Remove default margin */ padding: 0; /* Remove default padding */ } /* List item styling */ ul li { color: #AAAAAA; /* Light gray text */ display: block; /* Block-level item */ position: relative; /* Allows absolutely positioned children */ float: left; /* Float layout */ width: 100%; /* Full container width */ border-bottom: 1px solid #333; /* Divider line */ } /* Label styling */ label { display: block; /* Full clickable area */ position: relative; /* Allows internal positioning */ font-size: 1.35em; /* Slightly larger text */ padding: 25px 25px 25px 80px; /* Extra left padding for custom radio circle */ margin: 10px auto; /* Vertical spacing */ height: 30px; /* Fixed height */ z-index: 1; /* Ensure clickable above elements */ cursor: pointer; /* Pointer cursor on hover */ transition: all 0.25s linear; /* Smooth animation */ -webkit-transition: all 0.25s linear; /* Safari compatibility */ } /* Hover effect on list item label */ li:hover label { color: #ee00ff; /* Neon magenta */ font-size: 2em; /* Dramatic enlargement */ padding: 25px 25px 25px 150px; /* Shift content right */ } /* Hover border change */ li:hover .check { border: 9px solid #FFFFFF; /* Thicker white border */ } /* Subtle hover background */ li:hover { background-color: rgb(100, 100, 100, .05); /* Light transparent overlay */ transition: background-color .15s linear; -webkit-transition: background-color .15s linear; } /* Hide actual radio input */ input[type=radio] { position: absolute; /* Remove from normal layout */ opacity: 0; /* Better than visibility:hidden for accessibility */ } /* Custom radio circle */ .check { position: absolute; /* Positioned inside li */ border: 5px solid #ee00ff; /* Outer circle border */ border-radius: 50%; /* Makes it circular */ height: 25px; /* Circle height */ width: 25px; /* Circle width */ top: 30px; /* Vertical placement */ left: 20px; /* Horizontal placement */ transition: border .25s linear; /* Smooth border animation */ -webkit-transition: border .25s linear; } /* Inner circle (radio dot) */ .check::before { display: block; /* Creates pseudo-element */ position: absolute; /* Positioned inside circle */ content: ''; /* Required for pseudo-element */ border-radius: 50%; /* Makes inner dot circular */ height: 15px; /* Inner dot height */ width: 15px; /* Inner dot width */ top: 5px; /* Center vertically */ left: 5px; /* Center horizontally */ } /* When radio is checked, fill inner dot */ input[type=radio]:checked ~ .check::before { background: #0DFF92; /* Bright green */ } /* When radio is checked, change border color */ input[type=radio]:checked ~ .check { border: 9px solid #0DFF92; } /* When radio is checked, change label appearance */ input[type=radio]:checked + label { color: #0DFF92; font-size: 2.35em; } /* Submit button styling */ input[type=submit] { border-radius: 40%; /* Rounded corners */ font-size: 1.35em; /* Larger text */ padding: 15px; /* Inner spacing */ float: right; /* Align right */ background-color:#ee00ff; /* Magenta background */ } /* Submit hover effect */ input[type=submit]:hover { background-color:#0DFF92; /* Green hover */ transition: background 1s linear; /* Slow transition */ -webkit-transition: background 1s linear; } </style> </head> <body> <div class="container"> <form action="[thisurl]"> <h2>Your favorite thing in the world:</h2> <ul> <li> <input type="radio" id="Pizza" name="fave" value="pizza"> <label for="Pizza">Pizza</label> <div class="check"></div> </li> <li> <input type="radio" id="Bacon" name="fave" value="bacon"> <label for="Bacon">Bacon</label> <div class="check"></div> </li> <li> <input type="radio" id="Cats" name="fave" value="Cats"> <label for="Cats">Cats</label> <div class="check"></div> </li> <li> <input type="radio" id="Dogs" name="fave" value="Dogs"> <label for="Dogs">Dogs</label> <div class="check"></div> </li> </ul> <input type="submit" value="Save"> </form> </div> </body> </html>

Selector cheat sheet

The assignment asked you to use these techniques. Here's where each one shows up in the solution.

TechniqueFrom the solutionWhat it does here
Attribute selector [ ]input[type=radio]Targets only the radios, not the submit button
Descendant (space)ul li, li:hover labelAny li in the list; any label inside the hovered row
Adjacent sibling +:checked + labelThe label right after a checked radio
General sibling ~:checked ~ .checkThe circle later in the same row, past the label
Child >ul > liNot needed in this solution. It would match only lis directly inside the ul. Here ul li does the same job, because there are no nested lists.
Pseudo-element ::before.check::beforeCreates the inner dot without extra HTML
Pseudo-classes:checked, :hoverStyle based on state: picked, or under the mouse
positionli relative, .check absolutePlaces each circle inside its own row
z-indexlabel { z-index: 1 }Keeps the label on top so circle clicks reach it

Fix it: common problems

The font looks plain, not like Lato

Your @import line isn't the first line in your <style>. Move it to the very top.

All the circles are piled up in one spot, or near the top of the page

The li is missing position: relative. Without it, the absolute circles are placed against the whole page instead of their own row.

The ring turns green but there's no dot in the middle

Your .check::before rule is missing content: '';. No content line means the pseudo-element is never drawn.

Clicking a word doesn't pick its option

The label's for doesn't match the input's id. Check spelling and capitals: for="Pizza" needs id="Pizza", not id="pizza".

I can pick more than one option

Your radios have different name values. Every radio in the group needs the same name, like name="fave".

The circle turns green but the label doesn't (or the other way around)

Check the order inside each li: input, then label, then the .check div. + needs the label immediately after the input. ~ only looks forward, so the .check must come after the input too.

Clicking right on a circle does nothing

The label is missing z-index: 1, so the circle is covering it. See step 8.

The real browser radio buttons are still showing

Check your selector spelling: input[type=radio], with no spaces inside the brackets. Also make sure the rule has opacity: 0.

Check yourself

Answer in your head first. Then click the question to check.

The real radio is invisible. How does it still get picked?

Its label does it. Clicking a <label for="Pizza"> clicks the input with id="Pizza", and the label fills the whole row.

Why does the circle use ~ instead of +?

The label sits between the input and the circle. + only reaches the very next element (the label). ~ reaches any later sibling, so it finds the circle.

What happens if you move the <input> to after the <div class="check">?

Clicking still picks it, but nothing lights up. Sibling selectors only look forward, so there's no longer a label or circle after the input for the checked rules to style.

Your circle has position: absolute. What is it positioned inside?

Its nearest parent with a position set: the li, because of position: relative.

The inner dot is 15px and the inside of the ring is 25px. What should top and left be to center it?

5px. (25 − 15) ÷ 2 = 5.

Why does a checked option stay green when you hover it?

Specificity. input[type=radio]:checked + label scores (0, 2, 2), which beats li:hover label at (0, 1, 2).

Why opacity: 0 instead of display: none to hide the radio?

display: none removes it for keyboard users. opacity: 0 only hides it from view, so Tab and the arrow keys still work.

Compare your work

Open your version next to the finished preview in step 10 and check each item.

  • The font is Lato (your @import is the first line in the stylesheet).
  • No default browser radios are showing. Each row has a magenta ring on the left.
  • Clicking a word or a circle picks that option, and only one can be picked at a time.
  • The picked option has a thick green ring, a green center dot, and big green text.
  • Hovering a row slides the text right, makes it magenta and bigger, and turns the ring thick and white.
  • Hovering the picked row keeps it green.
  • The Save button is a magenta pill on the right that fades to green on hover.
  • Pressing Tab and the arrow keys still changes the picked option.