HTML - Basic HTML Part 4
Instructors: Jess, Carmen, and Eda
Basic HTML
Before 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!
During this session, we’ll:
- Review notes
- Review core concepts
- Review notes
- Review notes
- We’ll talk through Build a Travel Agency Page as a class so you’ll be prepared to complete this creative exercise over the weekend.
After this session, please:
- Read Working with Audio and Video Elements: What Are the Roles of the HTML Audio and Video Elements, and How Do They Work? and take notes
- Preview Build an HTML Music Player and try to work through the steps alone, recording questions and any new concepts
- Preview Build an HTML Video Player and try to work through the steps alone, recording questions and any new concepts
- Complete Build an HTML Audio and Video Player on your own
Instructor notes
Build a Bookstore Page
This workshop is all about putting yesterday’s reading into practice. You’ll be building book cards that display information about different books, and you’ll organize the content using div elements, classes, and IDs. So if you’re feeling shaky on any of those, this is a good chance to get the muscle memory.
HTML Fundamentals: What Is the Role of the Script Element in HTML, and How Can It Be Used to Link to External JavaScript Files?
We’ve already met the link element, which connects an HTML file to a stylesheet. The script element does something similar but for JavaScript.
Script element: an HTML element used to embed executable code. Most of the time that means JavaScript. JavaScript is what we use to add interactivity to web pages, things like games, image sliders, or forms that validate input as the user types.
There are two ways to use the script element:
1. Inline scripts: you write the JavaScript directly between the opening and closing tags.
1<body>
2 <script>
3 alert("Welcome to freeCodeCamp");
4 </script>
5</body>
2. External scripts: you put your JavaScript in a separate file (with a .js extension) and link to it using the src attribute.
1<script src="path-to-javascript-file.js"></script>
The src attribute (short for “source”) tells the browser where to find the external JavaScript file. Note that even when you’re using src and there’s no code between the tags, you still need a closing </script> tag.
Why link to an external file instead of writing JavaScript inline? Even though you technically can write all your JavaScript inside <script> tags directly in your HTML, it is considered best practice to use an external file. The reason is a design principle called separation of concerns.
Separation of concerns: a design principle where you separate your programs into distinct sections, with each section responsible for one concern. In our case, we want to separate our JavaScript code (the behaviour of the page) from our HTML code (the structure of the page). Keeping them in different files makes everything easier to read, easier to maintain, and easier to reuse across multiple pages.
Questions:
- What’s the difference between a link element and a script element, since both connect external files?
- Can I have more than one script tag on the same page?
Understanding How HTML Affects SEO: What Is the Role of the Meta Description, and How Does It Affect SEO?
This is our first peek at SEO, so let’s start with what that even means.
SEO (Search Engine Optimization): the practice of optimizing your web pages so they become more visible and rank higher on search engines like Google. Good SEO is partly about your HTML and partly about your content.
Meta description: a short description of what a page is about, set using a meta element in the <head> of your HTML. It looks like this:
1<meta
2 name="description"
3 content="Discover expert tips and techniques for gardening in small spaces."
4/>
There are two attributes to know:
- The
nameattribute is set to"description". This is what tells browsers, search engines, and other web tools that this metadata is the page’s description. - The
contentattribute is where you actually write the description.
A few things to know:
- Keep it short and concise. Search engines often truncate descriptions based on the layout of the results page, so longer descriptions can get cut off.
- The meta description does NOT appear anywhere on the page itself. The main place you’ll see it is in search engine results, displayed just under the page link, where it helps users decide whether to click.
- A meta description doesn’t directly affect a site’s ranking on search engines. But a strong description tends to result in more traffic, because more people click through.
Questions:
- What happens if I don’t include a meta description at all?
- Should every page on my site have a different meta description, or can I reuse one across the whole site?
Understanding How HTML Affects SEO: What Is the Role of Open Graph Tags, and How Do They Affect SEO?
You know how when you paste a link into Discord, Slack, iMessage, or basically anywhere else, you often get a nice little preview card with a title, a description, and an image? That’s not magic. The site you linked to is telling those apps what to show, using Open Graph tags.
Open Graph (OG) protocol: a system that lets you control how your website’s content appears across social media platforms like Facebook, LinkedIn, and many others. You set OG properties using meta elements inside the <head> of your HTML.
One thing to notice right away: Open Graph tags use the property attribute, not the name attribute that we used for the meta description. The content attribute is still used to hold the actual value.
The four most important OG properties (according to the lecture, these are the ones to always include):
1. og:title is the title shown on the preview card.
1<meta content="freeCodeCamp.org" property="og:title" />
2. og:type describes what kind of content the page is. Common values include website, article, video, and music.
1<meta property="og:type" content="website" />
3. og:image is the image shown on the preview card. This is the one that has the biggest visual impact, so the image should be high quality with good dimensions.
1<meta
2 content="https://cdn.freecodecamp.org/platform/universal/fcc_meta_image.png"
3 property="og:image"
4/>
Facebook’s documentation recommends images that are at least 1200 by 630 pixels for the best display on high resolution devices. The minimum is 600 by 315 pixels.
4. og:url is the URL of the page being shared.
1<meta property="og:url" content="https://www.freecodecamp.org" />
There are many more OG properties (description, audio, video, locale, and so on), but title, type, image, and url are the must-haves.
How does this affect SEO? Open Graph tags don’t directly affect your search ranking. But well-crafted OG properties make your links look more attractive when shared on social media, which leads to higher click-through rates. Higher click-through rates can signal to search engines that your content is relevant and engaging, which helps your SEO indirectly.
Questions:
- What size should my
og:imagebe so it looks good on most platforms? - If I update my Open Graph tags, why does the old preview sometimes still show up when I share the link?
Build a Travel Agency Page
This is your weekend creative exercise. We’ll talk through the user stories together as a class, but here’s what to keep in mind when you go to do it on your own:
The structure you’ll need:
- A full HTML boilerplate (
DOCTYPE,htmlwithlang="en",headwithcharset="utf-8", pagetitle) - A
metadescription for SEO (yes, the thing you just read about!) - An
h1for your travel destinations and a paragraph introducing them - An
h2with the textPackages, plus a paragraph and an unordered list with two items:Group TravelsandPrivate Tours. Each list item’s text should be wrapped in an anchor element. - An
h2with the textTop Itineraries - At least three
figureelements, each containing an anchor element (which wraps animg) and afigcaption
A couple of things to watch for:
- All five anchor elements (the two in the package list plus the three around your figure images) need both an
hrefofhttps://www.freecodecamp.org/learnAND atarget="_blank". Forgettingtarget="_blank"is a common reason this lab fails its tests. - Every
imgneeds analtattribute. The lab provides three image URLs you can use (colosseo.jpg,alps.jpg,sea.jpgfrom the freeCodeCamp CDN), or you can find your own. - The lab description encourages you to give it your own personal style. The user stories define what the page must contain, but the writing, the destinations you pick, and the order of things are up to you. Show off!