CiTi · Computer Coding · Year Two · Unit 1
Custom radio buttons with pure CSS, built one step at a time
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.
<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.: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.
Every option is one <li> with exactly three things inside, in this order:
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.First the basics: load the Lato font, make the page dark, color the heading, and center a 500px column.
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.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.
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.
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.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.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.
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.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.”top: 5px; left: 5px.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.
This is the heart of the assignment. When a hidden radio is checked, these rules light up its circle, dot, and label.
| Selector | In plain English |
|---|---|
| input[type=radio]:checked + label | The label that comes right after a checked radio. + is the adjacent sibling selector: same parent, the very next element. |
| input[type=radio]:checked ~ .check | Any .check that comes anywhere after a checked radio. ~ is the general sibling selector: same parent, later in the HTML. |
| input[type=radio]:checked ~ .check::before | The inner dot of that same circle. |
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.
This preview turns off the z-index: 1 from step 3 so you can see the problem it solves.
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.
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 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):
| Rule | Score | Result |
|---|---|---|
| li:hover label | 0, 1, 2 | loses |
| input[type=radio]:checked + label | 0, 2, 2 | wins: label stays green |
| li:hover .check | 0, 2, 1 | loses |
| input[type=radio]:checked ~ .check | 0, 3, 1 | wins: 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.
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.
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.:hover rule. Like the row background in step 9, it fades to green slowly but snaps right back to magenta.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.htmlThe assignment asked you to use these techniques. Here's where each one shows up in the solution.
| Technique | From the solution | What it does here |
|---|---|---|
Attribute selector [ ] | input[type=radio] | Targets only the radios, not the submit button |
| Descendant (space) | ul li, li:hover label | Any li in the list; any label inside the hovered row |
Adjacent sibling + | :checked + label | The label right after a checked radio |
General sibling ~ | :checked ~ .check | The circle later in the same row, past the label |
Child > | ul > li | Not 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::before | Creates the inner dot without extra HTML |
| Pseudo-classes | :checked, :hover | Style based on state: picked, or under the mouse |
position | li relative, .check absolute | Places each circle inside its own row |
z-index | label { z-index: 1 } | Keeps the label on top so circle clicks reach it |
Your @import line isn't the first line in your <style>. Move it to the very top.
The li is missing position: relative. Without it, the absolute circles are placed against the whole page instead of their own row.
Your .check::before rule is missing content: '';. No content line means the pseudo-element is never drawn.
The label's for doesn't match the input's id. Check spelling and capitals: for="Pizza" needs id="Pizza", not id="pizza".
Your radios have different name values. Every radio in the group needs the same name, like name="fave".
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.
The label is missing z-index: 1, so the circle is covering it. See step 8.
Check your selector spelling: input[type=radio], with no spaces inside the brackets. Also make sure the rule has opacity: 0.
Answer in your head first. Then click the question to check.
Its label does it. Clicking a <label for="Pizza"> clicks the input with id="Pizza", and the label fills the whole row.
~ 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.
<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.
position: absolute. What is it positioned inside?Its nearest parent with a position set: the li, because of position: relative.
top and left be to center it?5px. (25 − 15) ÷ 2 = 5.
Specificity. input[type=radio]:checked + label scores (0, 2, 2), which beats li:hover label at (0, 1, 2).
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.
Open your version next to the finished preview in step 10 and check each item.
@import is the first line in the stylesheet).