Learn HTML and CSS: Specificity
Specificity
Frank's nephew once wrote `#playbill p { color: grey; }`. Now Frank can't get the dates red, however often he puts `.date` at the bottom.
The heaviest selector wins
When selectors differ, their specificity decides first: how precisely they point at an element. Only in a tie does the order count.
You count three things: ids, then classes (attributes and pseudo-classes too), then elements. #playbill p has 1 id and 1 element: 1-0-1. .date has 1 class: 0-1-0.
Compare from left to right: the first number that differs decides. So one id beats ten classes. The cascade under the page shows why the date stays grey.
#playbill p { color: #6b5b5b;}.date { color: #7a1c1c;}Why !important is no solution
With !important after a value, a declaration beats everything, even an id. It looks like a quick fix.
But anyone who wants to change something later then needs !important too, and with two !importants specificity decides again. That starts an arms race that makes your CSS unreadable.
Better: make your selector just heavy enough, or make the rule that's too heavy lighter.
#playbill p { color: #6b5b5b;}.date { color: #7a1c1c !important;}Try it yourself
Make the link of the active menu item (class active) gold (#d4a017). Watch out: basis.css already colours the menu links with #menu a.
/* 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