HTML - Basic HTML Part 3
Instructors: Jess, Carmen, and Eda
Basic HTML
Before this session, please:
- Work through the steps from Build a Cat Photo App and record any questions or new concepts
- Complete Build a Recipe Page in your own time over the next few days. This is your first creative exercise, so feel free to show off your recipe page in Discord when you’re done!
- Read HTML Fundamentals: What are Div Elements and When Should You Use Them? and take notes
- Read HTML Fundamentals: What Are IDs and Classes, and When Should You Use Them? and take notes
- Read HTML Fundamentals: What Are HTML Entities, and What Are Some Common Examples? and take notes
During this session, we’ll:
- Review core concepts
- We’ll talk through Build a Recipe Page and answer your questions so you’re prepared to complete it solo over the next few days
- Review notes
- Review notes
- Review notes
After this session, please:
- Read HTML Fundamentals: What Is the Role of the Script Element in HTML, and How Can It Be Used to Link to External JavaScript Files? and take notes
- Work through the steps from Build a Bookstore Page and record any questions or new concepts
- Read Understanding How HTML Affects SEO: What Is the Role of the Meta Description, and How Does It Affect SEO? and take notes
- Read Understanding How HTML Affects SEO: What Is the Role of Open Graph Tags, and How Do They Affect SEO? and take notes
- Complete Build a Travel Agency Page over the next few days, with your own creative approach to the lab!
Instructor notes
Build a Cat Photo App
This one is a workshop rather than a lab, so you’ll be guided step-by-step instead of debugging on your own. Take your time with it and don’t worry if you find yourself going back to earlier steps to re-read instructions! That’s part of how this sticks.
If you get stuck, the most common issues are forgetting to close a tag and forgetting which attribute belongs to which element (remember: href is for anchors, src is for images).
HTML Fundamentals: What are Div Elements and When Should You Use Them?
So far we’ve been using elements that have specific meanings: <p> are for paragraphs, <a> are for links, <img/> are for images. The div element is different: it doesn’t mean anything on its own. It’s a generic container.
Div element: a container element used to group other HTML elements together. The most important thing to know about a div is that it has no semantic meaning. It doesn’t tell the browser, search engines, or assistive technologies anything about what’s inside it. It’s just a wrapper.
Semantics: the meaning of words or phrases in a language. HTML is a language, and most of its elements have semantic meaning. A <p> means “this is a paragraph”, an <h1> means “this is the most important heading on the page”. A <div> doesn’t say anything like that.
When to use a div: when you want to group elements together purely so you can apply shared CSS styles to them, and there’s no more meaningful element that fits.
When NOT to use a div: when there’s a semantic element that describes what you’re grouping. For example, if you’re dividing your page into distinct sections, use the <section> element instead. The browser will understand it as a section, and so will screen readers and search engines.
1<section>
2 <h2>Mammals</h2>
3 <p>Mammals are warm-blooded animals with fur or hair.</p>
4 <ul>
5 <li>Lion</li>
6 <li>Elephant</li>
7 <li>Dolphin</li>
8 </ul>
9</section>
The lecture is pretty firm on this: divs are common in real-world code, but you should be careful not to overuse them. We’ll meet more semantic grouping elements (<header>, <footer>, <nav>, <article>, <aside>) as we go.
Questions:
- If a div has no semantic meaning, why do I see them used so often on real websites?
- What’s the difference in how a screen reader treats a
<div>versus a<section>?
HTML Fundamentals: What Are IDs and Classes, and When Should You Use Them?
IDs and classes are two attributes you can add to almost any HTML element. They don’t change anything visible on their own. Instead, they give you a way to point at specific elements so you can style them with CSS or interact with them using JavaScript later on.
ID: a unique identifier for a single element on your page. You add it with the id attribute:
1<h1 id="title">Movie Review Page</h1>
In CSS, you target an ID using the hash symbol (#):
1#title {
2 color: red;
3}
The hash tells the browser “find the element whose ID is title and apply these styles to it”. An ID should only ever appear once per page. Using the same ID more than once leads to unwanted issues with both styling and JavaScript.
Rules for ID values:
- Must be unique on the page
- Cannot contain spaces (browsers will treat the space as the end of the value)
- Should only contain letters, digits, underscores and dashes
So
id="main-heading"is fine, butid="main heading"is not.
Class: a label that can be shared across multiple elements. You add it with the class attribute:
1<div class="box"></div>
In CSS, you target a class using a dot (.):
1.box {
2 width: 100px;
3 height: 100px;
4}
Classes are designed to be reused. You can give the same class to as many elements as you like, and you can give a single element multiple classes by separating them with spaces:
1<div class="box red-box"></div>
2<div class="box blue-box"></div>
This element has two classes: box and red-box. Both sets of CSS rules will apply to it.
Recap:
- Use a class when you want to apply the same set of styles to many elements
- Use an ID when you need to target one specific element that should be unique on the page
Questions:
- What characters am I allowed to use in an ID or class name?
- What happens if I accidentally give two elements the same ID?
HTML Fundamentals: What Are HTML Entities, and What Are Some Common Examples?
Some characters are tricky to put in your HTML directly because they already mean something to the browser. The clearest example: if you type < into your HTML, the parser will assume you’re starting a tag. So how do you write a less-than sign and have it actually appear on the page?
HTML entity (also called a character reference): a set of characters used to represent a reserved character in HTML. The browser sees the entity in your code and converts it into the actual character when displaying the page.
There are three types of character references:
1. Named character references
Start with an ampersand (&) and end with a semicolon (;), with a name in between. Examples:
<displays as<>displays as>&displays as&"displays as"©displays as© displays as a non-breaking space So to display the text<p>learning is fun</p>on the page (rather than have it parsed as a paragraph element), you’d write:
1<p>learning is fun</p>
2. Decimal numeric character references
Start with &#, followed by one or more decimal digits, ending with a semicolon. Examples:
<displays as<©displays as©®displays as®
3. Hexadecimal numeric character references
Start with &#x, followed by one or more ASCII hex digits, ending with a semicolon. The x is what tells the parser the digits that follow are hexadecimal. Examples:
<displays as<€displays as€Ωdisplays asΩYou don’t need to memorise specific numeric codes, they get looked up. But you do need to know that all three types exist and recognise their syntax.
Questions:
- When should I use
versus a regular space? - If UTF-8 encoding (which we set up yesterday) supports all these characters anyway, why do entities still exist?