Chapter 2 lab Chapter 3 lab Chapter 4 lab Chapter 5 lab Chapter 6 lab Chapter 7 lab Chapter 8 lab Chapter 9 lab Chapter 10 lab Chapter 12 lab Chapter 13 lab
Chapter 3 · Lab

Lab 3: Cascading Style Sheets

A hands-on CSS lab: inline, embedded and external styles, ID and class selectors, a full semantic page layout, and a CSS animation. Each task has HTML and CSS panes with a live preview.

Learning outcomes

  • Explain the different ways of applying Cascading Style Sheets to customise HTML elements
  • Write commonly used style properties such as font, size and colour
  • Apply external style sheets to standardise the style for all pages in a website
  • Control web page layout with HTML5 semantic elements and CSS
  • Write CSS animations using @keyframes

Your progress

Tasks completed 0/6

Log in to save your lab progress. Log in

1

Task 1 — Inline style sheets

An inline style is written directly inside an HTML tag using the style attribute. It applies to that single element only, and is useful for one-off formatting or for overriding styles defined elsewhere.

  1. Enter the code in the editor and save the file as inline.html.
  2. Open it in the browser to see the effect.
  3. Modify the rule so the heading text is green, then save and refresh.

Multiple declarations are separated by semicolons: style="property: value; property: value;".

Code

inline.html
<!DOCTYPE html>
<html>
<head>
  <title>Inline Styles</title>
</head>
<body>
  <h1 style="color: black; font-family: Arial, sans-serif;">
    This is a styled heading.
  </h1>

  <p style="background-color: #ffff33; border: 1px solid black; padding: 1em;">
    This is a styled paragraph.
  </p>
</body>
</html>

Try it

inline.html
2

Task 2 — Embedded style sheet

An embedded (document-level) style sheet is written inside a <style> element in the <head>. Its rules apply to the whole page.

  1. Save the code as embed.html and open it in the browser.
  2. Redefine the h1 rule to use font-family Verdana, Helvetica, Courier, font-size 12pt, colour yellow, and a background colour of your choice.
  3. Test the result in the browser.

To set multiple properties in a single rule: selector { prop1: val1; prop2: val2; }.

Code

embed.html
<!DOCTYPE html>
<html>
<head>
  <title>Embedded Styles</title>
  <style type="text/css">
    h1 {
      color: blue;
      background-color: black;
      padding: 10px;
    }
    p {
      color: red;
      padding: 1em;
    }
  </style>
</head>
<body>
  <h1>This is a styled heading.</h1>
  <p>This is a styled paragraph.</p>
</body>
</html>

Try it

embed.html
3

Task 3 — External style sheet

An external style sheet lives in its own .css file and is linked from one or more HTML pages — the most powerful way to control a whole site from a single location.

  1. Rewrite the styles from Task 2 into a file called mystyle.css (do not include the <style> tags).
  2. Create external.html and link the stylesheet in the <head>: <link rel="stylesheet" href="mystyle.css">.
  3. Open external.html — the output should match embed.html.
  4. Validate your CSS at jigsaw.w3.org/css-validator.

In this editor the CSS pane is injected into the page so the preview works without a real file — the <link> line is what you use in your project.

Code

external.html
<!DOCTYPE html>
<html>
<head>
  <title>External Styles</title>
  <!-- Link the external stylesheet -->
  <link rel="stylesheet" href="mystyle.css">
</head>
<body>
  <h1>This is a styled heading.</h1>
  <p>This is a styled paragraph.</p>
</body>
</html>
mystyle.css
/* mystyle.css */
h1 {
  color: blue;
  background-color: black;
  padding: 10px;
}

p {
  color: red;
  padding: 1em;
}

Try it

external.html
mystyle.css
4

Task 4 — ID selectors and classes

Besides HTML tag selectors, you can define your own ID selectors (#name — for a single, unique element) and class selectors (.name — for a group of elements).

  1. Save the HTML as selectors.html and the CSS as selectstyle.css.
  2. Link the stylesheet and open the page in the browser.
  3. Add another element with class="center" and confirm it is centred.

Code

selectors.html
<!DOCTYPE html>
<html>
<head>
  <title>ID and Class Selectors</title>
  <link rel="stylesheet" href="selectstyle.css">
</head>
<body>
  <div id="main">
    <h1 class="center">Bicycle Dot Com</h1>
    <p class="dot">Welcome to Bicycle.com.</p>
    <p>Browse our <span class="dot">bicycle collections</span>.</p>
    <p>To order, please use the contact form.</p>
    <p class="center">(c) 2025 Bicycle Dot Com</p>
  </div>
</body>
</html>
selectstyle.css
/* selectstyle.css */
#main {
  background-color: #cccccc;
  padding: 1em;
}

.dot {
  color: red;
  font-weight: bold;
}

.center {
  text-align: center;
  font-weight: bolder;
}

Try it

selectors.html
selectstyle.css
5

Task 5 — Page layout with HTML5 elements and CSS

Almost every site shares the same basic structure: Header, Navigation, Main Content, Sidebar and Footer. HTML5 gives us semantic elements for each, and CSS lays them out.

  1. Create layout.html with the semantic structure shown.
  2. Each <article> contains a <figure> (image + <figcaption>), an <hgroup> and a paragraph.
  3. Add the <aside> with a "Popular Recipes" section and contact details.
  4. Style the page with the CSS provided and test it in the browser.

The images use inline placeholders so they render in the preview; in your project you would use images/bokchoi.jpg and so on.

Code

layout.html
<!DOCTYPE html>
<html>
<head>
  <title>Yoko's Kitchen</title>
  <link rel="stylesheet" href="layout.css">
</head>
<body>
  <div class="wrapper">
    <header>
      <h1>Yoko's Kitchen</h1>
      <nav>
        <ul>
          <li><a href="#" class="current">home</a></li>
          <li><a href="#">classes</a></li>
          <li><a href="#">catering</a></li>
          <li><a href="#">about</a></li>
          <li><a href="#">contact</a></li>
        </ul>
      </nav>
    </header>

    <section class="courses">
      <article>
        <figure>
          <img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='290' height='180'%3E%3Crect width='290' height='180' fill='%23cfe0c3'/%3E%3C/svg%3E"
               alt="Bok Choi" width="290" height="180">
          <figcaption>Bok Choi</figcaption>
        </figure>
        <hgroup>
          <h2>Japanese Vegetarian</h2>
          <h3>Five week course in London</h3>
        </hgroup>
        <p>A five week introduction to traditional Japanese vegetarian meals, teaching you a selection of rice and noodle dishes.</p>
      </article>

      <article>
        <figure>
          <img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='290' height='180'%3E%3Crect width='290' height='180' fill='%23e8d5b7'/%3E%3C/svg%3E"
               alt="Teriyaki sauce" width="290" height="180">
          <figcaption>Teriyaki Sauce</figcaption>
        </figure>
        <hgroup>
          <h2>Sauces Masterclass</h2>
          <h3>One day workshop</h3>
        </hgroup>
        <p>An intensive one-day course looking at how to create the most delicious sauces for use in a range of Japanese cookery.</p>
      </article>
    </section>

    <aside>
      <section class="popular-recipes">
        <h2>Popular Recipes</h2>
        <a href="#">Yakitori (grilled chicken)</a>
        <a href="#">Tsukune (minced chicken patties)</a>
        <a href="#">Okonomiyaki (savory pancakes)</a>
        <a href="#">Mizutaki (chicken stew)</a>
      </section>
      <section class="contact-details">
        <h2>Contact</h2>
        <p>Yoko's Kitchen<br>27 Red Street<br>Shoreditch<br>London E2</p>
      </section>
    </aside>

    <footer>
      &copy; 2025 Yoko's Kitchen
    </footer>
  </div>
</body>
</html>
layout.css
/* layout.css */
body {
  background-color: #f9f5f0;
  font-family: Georgia, Times, serif;
  color: #333;
  margin: 0;
}

header, section, footer, aside, nav, article, figure, figcaption {
  display: block;
}

.wrapper {
  width: 940px;
  max-width: 96%;
  margin: 20px auto;
  border: 2px solid #e0d6c8;
  background-color: #ffffff;
}

header {
  background-color: #0b1f3a;
  color: #ffffff;
  padding: 20px;
}

header h1 {
  margin: 0;
}

nav {
  clear: both;
  background-color: #aeaca8;
}

nav ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

nav li {
  display: inline;
}

nav a {
  color: #ffffff;
  padding: 5px 10px;
  text-decoration: none;
}

nav a:hover,
nav a.current {
  color: #000000;
  background-color: #ffffff;
}

section.courses {
  float: left;
  width: 640px;
  max-width: 70%;
  border-right: 1px solid #eeeeee;
}

article {
  clear: both;
  overflow: auto;
}

figure {
  float: left;
  width: 290px;
  padding: 0;
  margin: 20px;
}

figcaption {
  font-size: 90%;
}

hgroup {
  margin-top: 40px;
}

aside {
  width: 220px;
  float: left;
  padding: 0 0 0 20px;
}

aside section a {
  display: block;
  padding: 10px;
  border-bottom: 1px solid #eeeeee;
  color: #de6581;
  text-decoration: none;
}

aside section a:hover {
  color: #985d6a;
  background-color: #efefef;
}

aside h2 {
  padding: 30px 0 10px 0;
  color: #de6581;
}

footer {
  clear: both;
  font-size: 80%;
  padding: 10px 20px;
  background-color: #aeaca8;
  color: #ffffff;
}

Try it

layout.html
layout.css
6

Task 6 — CSS animation

A CSS animation lets an element gradually change from one style to another. You define the steps with @keyframes, then attach them with the animation properties.

  1. Run the code and watch the two boxes.
  2. Change animation-duration and animation-iteration-count and observe.
  3. The first box uses steps() to jump between frames — the technique used for sprite-sheet animation (the lab's walking character uses steps(6) and animates background-position).
PropertyPurpose
@keyframesDefines the animation steps
animation-nameLinks the element to a @keyframes rule
animation-durationHow long one cycle takes
animation-delayWait before starting
animation-iteration-count1, 3 or infinite
animation-directionnormal / reverse / alternate
animation-timing-functionease / linear / steps(n)
animation-fill-modenone / forwards / backwards / both

Code

animation.html
<!DOCTYPE html>
<html>
<head>
  <title>CSS Animation</title>
  <link rel="stylesheet" href="animation.css">
</head>
<body>
  <h1>CSS Animation</h1>

  <h2>Sprite-style animation (steps)</h2>
  <div class="strip"></div>

  <h2>Keyframe animation</h2>
  <div class="pulse"></div>
</body>
</html>
animation.css
/* animation.css */
body {
  font-family: Arial, sans-serif;
  margin: 20px;
}

/* steps() jumps between frames - like a sprite sheet */
.strip {
  width: 100px;
  height: 100px;
  background-image: repeating-linear-gradient(
    90deg,
    #2563eb 0 100px,
    #0ea5a4 100px 200px,
    #f59e0b 200px 300px
  );
  background-size: 300px 100px;
  animation-name: walk;
  animation-duration: 0.9s;
  animation-timing-function: steps(3);
  animation-iteration-count: infinite;
  animation-direction: normal;
}

@keyframes walk {
  to { background-position: -300px 0; }
}

/* A classic keyframe animation */
.pulse {
  width: 100px;
  height: 100px;
  border-radius: 16px;
  background: #7c3aed;
  animation: pulse 1.2s ease-in-out infinite alternate;
}

@keyframes pulse {
  from { transform: scale(1); opacity: 0.6; }
  to   { transform: scale(1.2); opacity: 1; }
}

Try it

animation.html
animation.css