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
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.
Enter the code in the editor and save the file as inline.html.
Open it in the browser to see the effect.
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.
Save the code as embed.html and open it in the browser.
Redefine the h1 rule to use font-family Verdana, Helvetica, Courier, font-size 12pt, colour yellow, and a background colour of your choice.
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.
Rewrite the styles from Task 2 into a file called mystyle.css (do not include the <style> tags).
Create external.html and link the stylesheet in the <head>: <link rel="stylesheet" href="mystyle.css">.
Open external.html — the output should match embed.html.
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>
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).
Save the HTML as selectors.html and the CSS as selectstyle.css.
Link the stylesheet and open the page in the browser.
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>
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.
Create layout.html with the semantic structure shown.
Each <article> contains a <figure> (image + <figcaption>), an <hgroup> and a paragraph.
Add the <aside> with a "Popular Recipes" section and contact details.
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.
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.
Run the code and watch the two boxes.
Change animation-duration and animation-iteration-count and observe.
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).