Learn HTML and CSS: Attribute selectors
Picking by attribute
Frank wants it to be obvious which links open a PDF. The links have no class, and he doesn't fancy giving them all one. But their `href` already gives it away.
An attribute as a selector
A selector in square brackets picks elements by their attributes. a[href="programme.pdf"] finds the link with exactly that href.
Without a value it only checks that the attribute is there: img[alt] finds every image with alt text.
That way you don't need a class: the attribute is there anyway.
<!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>a[href="programme.pdf"] { font-weight: bold;}Starts with, ends with
Often you only know part of the value. Then you put a character before the =:
^= means "starts with": a[href^="https"] finds every link to another website. $= means "ends with": a[href$=".pdf"] finds every PDF. And *= means "contains somewhere".
Put the value in quotes, especially when it has a full stop, a colon or a slash in it.
<!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>a[href$=".pdf"] { font-weight: bold;}Try it yourself
Make every link to a PDF bold, and give every link that opens an email the colour #2a6f4e. The other links stay as they are.
/* 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 5 exercises, from easy to challenging, with hints, points and a streak you can build.
Start the lesson