Learn HTML and CSS: Pseudo-classes
Pseudo-classes
On the Curtain Up site nothing happens when you move the mouse over a button. "A theatre without lighting," sighs Frank. And the table of performances reads like one grey wall.
A state
A pseudo-class picks an element in a certain state. It starts with a colon, right after the selector.
:hover applies while the mouse is on the element. :focus applies when the element has been chosen with the keyboard: anyone who moves through the links with the Tab key can see where they are.
On a computer you can try it straight away: move the mouse over a ticket button. On a phone hover barely exists, so never put anything important in a hover alone.
.button:hover { background-color: #b8860b;}A position
Other pseudo-classes pick an element by its position among its brothers and sisters. li:first-child is the first <li> of its list, li:last-child the last.
:nth-child(3) is the third. With :nth-child(even) you pick the even ones, with :nth-child(odd) the odd ones: 1, 3, 5.
That's how you give every other row of a table a tint, without a class on each row.
tr:nth-child(even) { background-color: #f6ead8;}Try it yourself
Give every button (.button) the background #b8860b while the mouse is on it, and also when it's chosen with the Tab key. At rest it stays as it is.
/* Your CSS goes here. */<!DOCTYPE html><html lang="en"> <head> <title>Curtain Up drama club</title> <link rel="stylesheet" href="basis.css"> <link rel="stylesheet" href="style.css"> </head> <body> <header> <h1>Curtain Up</h1> <p class="slogan">Drama club in Grantham since 1952</p> <nav id="menu"> <ul> <li><a href="index.html">Home</a></li> <li class="active"><a href="playbill.html">Playbill</a></li> <li><a href="join.html">Join us</a></li> <li><a href="contact.html">Contact</a></li> </ul> </nav> </header> <main> <section id="playbill"> <h2>On the playbill</h2> <article class="play"> <h3>The Miser</h3> <p class="date">14 to 16 March</p> <p>A farce after <a href="https://en.wikipedia.org/wiki/Molière">Molière</a>, about a man who loves his money more than his children.</p> <blockquote> <p>I haven't laughed this much in years.</p> </blockquote> <a class="button" href="https://tickets.curtainup.example/miser">Tickets</a> </article> <article class="play"> <h3>Murder at the Rectory</h3> <p class="date">9 to 11 May</p> <p>Everyone has an alibi, except the cat.</p> <a class="button" href="https://tickets.curtainup.example/rectory">Tickets</a> </article> </section> <section id="dates"> <h2>Performances</h2> <table> <tr> <th>Day</th> <th>Time</th> </tr> <tr> <td>Friday 14 March</td> <td>8 pm</td> </tr> <tr> <td>Saturday 15 March</td> <td>8 pm</td> </tr> <tr> <td>Sunday 16 March</td> <td>3 pm</td> </tr> </table> </section> <section id="downloads"> <h2>To read</h2> <ul> <li><a href="programme.pdf">The programme</a></li> <li><a href="rules.pdf">The club rules</a></li> <li><a href="https://en.wikipedia.org/wiki/The_Miser">About The Miser</a></li> <li><a href="mailto:boxoffice@curtainup.example">Email the box office</a></li> </ul> </section> </main> <footer> <p>The Crown Hall, 21 Church Street, Grantham</p> </footer> </body></html>body { margin: 0; font-family: system-ui, sans-serif; background: #fbf6ee; color: #2b1d1d;}header { padding: 20px 16px 14px; background: #5c1515; color: #f5e6c8;}header h1 { margin: 0;}.slogan { margin: 4px 0 12px;}#menu ul { display: flex; flex-wrap: wrap; gap: 4px 18px; margin: 0; padding: 0; list-style: none;}#menu a { color: #f5e6c8;}main { padding: 8px 16px 24px;}h2 { color: #7a1c1c;}a { color: #7a1c1c;}.play { margin: 12px 0; padding: 12px 16px; border-radius: 16px; background: #ffffff;}.play h3 { margin: 0 0 4px;}blockquote { margin: 8px 0; padding-left: 12px; border-left: 3px solid #d4a017;}.button { display: inline-block; padding: 6px 16px; border-radius: 999px; background-color: #7a1c1c; color: #ffffff; text-decoration: none;}table { border-collapse: collapse;}th,td { padding: 6px 10px; border: 1px solid #e6d3c3; text-align: left;}footer { padding: 12px 16px; background: #5c1515; color: #f5e6c8;}Ready for more?
The full lesson has 6 exercises, from easy to challenging, with hints, points and a streak you can build.
Start the lesson