Learn HTML and CSS: Stylesheets
Linking a stylesheet
"So where is that CSS, actually?" asks Gino. "In the HTML?" Not really. The HTML only points to it.
A file of its own
CSS usually lives in a file of its own, a stylesheet. At Snip & Curl it's called style.css.
That stylesheet only works when the HTML points to it, with <link rel="stylesheet" href="style.css"> in the <head>. rel says what kind of file it is, href where it is.
The beauty of it: ten pages with the same <link> share the same stylesheet. One change, and the whole site changes with it.
<!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>h1 { color: purple;}Two other places
CSS can also go in the HTML itself. In a <style> element in the <head>, it only works for that one page.
Or in a style attribute on one element, like <h2 style="color: orange">. That looks handy, but a style like that beats everything in your stylesheet, it's hard to find again, and you have to retype it on every element.
So: CSS in style.css, and no style attributes.
Under the page is the cascade of the second heading: the style attribute wins, h2 { color: teal; } is struck through.
<!DOCTYPE html><html lang="en"> <head> <title>Snip & Curl hair salon</title> <link rel="stylesheet" href="basis.css"> <style> h2 { color: teal; } </style> </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 style="color: orange">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>Try it yourself
The nephew made both subheadings teal with style attributes. Remove them and put the same colour, teal, in style.css.
<!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 style="color: teal">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 style="color: teal">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>/* Your CSS goes here. */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