Learn HTML and CSS: Class and id
Class and id
"Just the prices in green," says Gino, "not the names of the services." But they're all `<span>`s. How do you pick only a few?
A class
In the HTML the prices have a class: <span class="price">. It's a label you choose yourself.
In CSS you find everything with that label using a dot in front: .price. A class can go on as many elements as you like, even on elements of a different kind.
Watch out: capitals matter. .Price finds nothing.
<!DOCTYPE html><html lang="en"> <head> <title>Snip & Curl hair salon</title> <link rel="stylesheet" href="basis.css"> <link rel="stylesheet" href="style.css"> </head> <body> <header> <h1 id="name">Snip & Curl</h1> <p class="slogan">Hair salon in the heart of Leeds</p> </header> <main> <h2>Our prices</h2> <ul class="price-list"> <li><span class="service">Haircut</span> <span class="price">£28</span></li> <li><span class="service">Wash and blow-dry</span> <span class="price">£22</span></li> <li><span class="service">Colouring</span> <span class="price">£55</span></li> </ul> <section id="booking"> <h2>Book an appointment</h2> <p class="info">Give us a call or just pop in.</p> <a class="button" href="tel:+441134960000">Call us</a> </section> </main> <footer> <p>8 Kirkgate, Leeds</p> </footer> </body></html>.price { color: #2a9d8f; font-weight: bold;}An id
An id is a name for one single element. On this page the heading has id="name" and the booking block id="booking".
In CSS you write an id with a hash: #name. An id appears only once on a page.
So: a class for things that appear more often, an id for something there's only one of.
<!DOCTYPE html><html lang="en"> <head> <title>Snip & Curl hair salon</title> <link rel="stylesheet" href="basis.css"> <link rel="stylesheet" href="style.css"> </head> <body> <header> <h1 id="name">Snip & Curl</h1> <p class="slogan">Hair salon in the heart of Leeds</p> </header> <main> <h2>Our prices</h2> <ul class="price-list"> <li><span class="service">Haircut</span> <span class="price">£28</span></li> <li><span class="service">Wash and blow-dry</span> <span class="price">£22</span></li> <li><span class="service">Colouring</span> <span class="price">£55</span></li> </ul> <section id="booking"> <h2>Book an appointment</h2> <p class="info">Give us a call or just pop in.</p> <a class="button" href="tel:+441134960000">Call us</a> </section> </main> <footer> <p>8 Kirkgate, Leeds</p> </footer> </body></html>#name { color: crimson;}#booking { text-align: center;}Try it yourself
Make all the prices bold and petrol green (#2a9d8f), without changing the services. And give the name at the top, with id name, the colour crimson.
/* Your CSS goes here. */<!DOCTYPE html><html lang="en"> <head> <title>Snip & Curl hair salon</title> <link rel="stylesheet" href="basis.css"> <link rel="stylesheet" href="style.css"> </head> <body> <header> <h1 id="name">Snip & Curl</h1> <p class="slogan">Hair salon in the heart of Leeds</p> </header> <main> <h2>Our prices</h2> <ul class="price-list"> <li><span class="service">Haircut</span> <span class="price">£28</span></li> <li><span class="service">Wash and blow-dry</span> <span class="price">£22</span></li> <li><span class="service">Colouring</span> <span class="price">£55</span></li> </ul> <section id="booking"> <h2>Book an appointment</h2> <p class="info">Give us a call or just pop in.</p> <a class="button" href="tel:+441134960000">Call us</a> </section> </main> <footer> <p>8 Kirkgate, Leeds</p> </footer> </body></html>body { margin: 0; padding: 0 16px;}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