HTML: text, links and images

Lesson 2 of 17 · HTML, CSS and JavaScript

A page comes to life when the text gets emphasis, when it links to other pages and when it has images. This lesson covers the elements for formatting text, links (absolute and relative paths, anchors) and images with the required description.

Before you start: required prior knowledge – your first web page.
How to learn: do the tasks at the end on your own in your editor first; open the hint and the solution only after that. Every example has the code and a live result below it; type the code into your own file and change it.

Formatting text

<p><strong>strong</strong> – important text (bold)</p>
<p><em>em</em> – emphasized text (italic)</p>
<p><mark>mark</mark> – highlighted with a marker</p>
<p>H<sub>2</sub>O and x<sup>2</sup> – subscript and superscript</p>
<p><code>code</code> – a piece of code</p>
<blockquote>blockquote – a longer, indented quotation</blockquote>
<p>Special characters: &lt; &gt; &amp; &copy;</p>

Result:

The browser would take the characters <, > and & as part of the HTML, so they are written as &lt;, &gt;, &amp;.

The site's folders

my_site/
├── index.html          ← home page
├── about_me.html
├── contact.html
├── css/
│   └── style.css
└── images/
    ├── logo.png
    └── volleyball.jpg

From index.html an image is referenced as images/logo.png. Write file names in lower case, without spaces and without special letters (my_image.jpg, not My Image.JPG) – servers distinguish upper and lower case, and spaces break addresses.

Images

<img src="https://picsum.photos/id/1025/300/180" alt="A dog wrapped in a blanket" width="300" height="180">
<figure>
  <img src="missing.png" alt="This description is shown because the image does not exist" width="200">
  <figcaption>Figure 1: figure and figcaption connect an image and its caption</figcaption>
</figure>

Result:

AttributeMeaning
srcthe path to the image (relative or absolute)
alta required description: a screen reader for blind users reads it, it is shown when the image cannot be loaded and it helps search engines
width, heightthe dimensions in pixels – the browser knows in advance how much space to leave, so the page does not "jump" while it loads
loading="lazy"the image is loaded only when it gets close to the screen
FormatWhat for
JPGphotographs
PNGimages with a transparent background, screenshots
SVGlogos and icons – vector graphics, sharp at any size
WebP / AVIFmodern formats – smaller files

Video and embedded content

<video src="video/experiment.mp4" controls width="400"></video>

<!-- A YouTube video: on YouTube Share → Embed, then copy the iframe -->
<iframe src="https://www.youtube.com/embed/VIDEO_ID" title="Video title" allowfullscreen></iframe>

The iframe element shows another page inside yours. That is also how the results of the examples are shown on this page.

Practice tasks

Each task has an example, a hint and a solution. The solutions have been checked in the browser.

TASK 01Difficulty: easy

A mini gallery with links

Make a page with a table of contents at the top (three anchors: Nature, City, Links), two sections with one image and caption each (figure) and a section with two links to external sites that open in a new tab.

Common mistakes and tips
  • The image is not shown: check the path and the upper/lower case in the file name (Logo.PNG ≠ logo.png on a server).
  • Every image must have an alt; if it is only decoration, write it empty: alt="".
  • Paths with C:\Users\... work only on your computer – always use relative paths.
  • The text of a link should describe its destination, not "click here".

Check your knowledge

Open the quiz

Text, links and images

1. Which attribute gives the address of a link?




2. What is the alt attribute of an image for?




3. What does the path ../index.html mean?




4. Which format is best for a logo that must be sharp at any size?