HTML. Unit 5. Images.

Introduction

In the beginning, the Web was just text, and it was really quite boring. Fortunately, it wasn’t too long before the ability to embed images (and other more interesting types of content) inside web pages was added. There are other types of multimedia to consider, but it is logical to start with the humble <img> element, used to embed a simple image in a webpage. In this unit we’ll look at how to use it in depth, including the basics, annotating it with captions using <figure>.

How do we put an image on a webpage?

In order to put a simple image on a webpage, we use the <img> element. This is a void element (meaning that it has no text content or closing tag) that requires a minimum of one attribute to be useful: src (sometimes spoken as its full title, source). The src attribute contains a path pointing to the image you want to embed in the page, which can be a relative or absolute URL, in the same way as href attribute values in <a> elements.

So for example, if your image is called dinosaur.jpg and it sits in the same directory as your HTML page, you could embed the image like so:

<img src="dinosaur.jpg" />

If the image was in an images subdirectory, which was inside the same directory as the HTML page (which Google recommends for SEO/indexing purposes), then you’d embed it like this:

<img src="images/dinosaur.jpg" />

Search engines also read image filenames and count them towards SEO. Therefore, you should give your image a descriptive filename; dinosaur.jpg is better than img835.jpg.

You also could embed the image using its absolute URL, for example:

<img src="https://www.example.com/images/dinosaur.jpg" />
...
<img src="https://developer.mozilla.org/static/img/favicon144.png" />

Alternative text

The next attribute we’ll look at is alt. Its value is supposed to be a textual description of the image, to be used by search engines, and also in situations where the image cannot be seen/displayed or takes a long time to render because of a slow internet connection. Keep in mind that an alt attribute’s value should clearly and concisely describe the image’s content. For example, our above code could be modified like so:

<img src="images/dinosaur.jpg"
     alt="The head and torso of a dinosaur skeleton;
          it has a large head with long sharp teeth" />
...
<img src="https://developer.mozilla.org/static/img/favicon144.png"
     alt="MDN logo" />

The easiest way to test your alt text is to purposely misspell your filename. If for example our image name was spelled dinosoooor.jpg, the browser wouldn’t display the image, and would display the alt text instead.

So, why would you ever see or need alt text? It can come in handy for a number of reasons:

  • The user is visually impaired, and is using a screen reader to read the web out to them. In fact, having alt text available to describe images is useful to most users.
  • As described above, the spelling of the file or path name might be wrong.
  • The browser doesn’t support the image type. Some people still use text-only browsers, such as Lynx, which displays the alt text of images.
  • You may want to provide text for search engines to utilize; for example, search engines can match alt text with search queries.
  • Users have turned off images to reduce data transfer volume and distractions. This may be especially common on mobile phones, and in countries where bandwidth is limited or expensive.

What exactly should you write inside your alt attribute? It depends on why the image is there in the first place. In other words, what you lose if your image doesn’t show up:

  • Content. If your image provides significant information, provide the same information in a brief alt text – or even better, in the main text which everybody can see. Don’t write redundant alt text. How annoying would it be for a sighted user if all paragraphs were written twice in the main content? If the image is described adequately by the main text body, you can just use alt="".
  • Link. If you put an image inside <a> tags, to turn an image into a link, you still must provide accessible link text. In such cases you may, either, write it inside the same <a> element, or inside the image’s alt attribute – whichever works best in your case.
  • Text. You should not put your text into images. If your main heading needs a drop shadow, for example, use CSS for that rather than putting the text into an image. However, If you really can’t avoid doing this, you should supply the text inside the alt attribute.

Essentially, the key is to deliver a usable experience, even when the images can’t be seen. This ensures all users are not missing any of the content. Try turning off images in your browser and see how things look. You’ll soon realize how helpful alt text is if the image cannot be seen.

Width and height

You can use the width and height attributes to specify the width and height of your image. We could do this:

<img src="images/dinosaur.jpg"
     alt="The head and torso of a dinosaur skeleton;
          it has a large head with long sharp teeth"
     width="400"
     height="340" />

However, you shouldn’t alter the size of your images using always HTML attributes. If you set the image size too big, you’ll end up with images that look grainy, fuzzy, or too small, and wasting bandwidth downloading an image that is not fitting the user’s needs. The image may also end up looking distorted, if you don’t maintain the correct aspect ratio. You should use an image editor to put your image at the correct size before putting it on your webpage, but if you do need to alter an image’s size, you should use CSS instead.

Image titles

As with links, you can also add title attributes to images, to provide further supporting information if needed. In our example, we could do this:

<img src="images/dinosaur.jpg"
     alt="The head and torso of a dinosaur skeleton;
          it has a large head with long sharp teeth"
     title="A T-Rex on display in the Manchester University Museum" />

This gives us a tooltip on mouse hover, just like link titles. However, it should not be used as a replacement of the text inside the alt attribute. The title has a number of accessibility problems, mainly based around the fact that screen reader support is very unpredictable and most browsers won’t show it unless you are hovering with a mouse (so e.g. no access to keyboard users).

Proposed exercise: Embedding images

Create a web page to display several images. You have to use the basic <img> tag and the src attribute to point to the URL of each individual image. Por example, you can use images like these ones:

https://developer.mozilla.org/static/img/favicon144.png

https://raw.githubusercontent.com/mdn/learning-area/master/html/multimedia-and-embedding/images-in-html/dinosaur_small.jpg

https://validator.w3.org/images/w3c.png

You must also add some alt text to each image, and check that they work by misspelling the image URL’s. And finally experiment with different values of width and height to see what the effect is.

Image link

This example builds upon the previous ones, showing how to turn the image into a link. To do so, nest the <img> tag inside the <a>. You should use the alternative text to describe the resource the link is pointing to, as if you were using a text link instead:

<a href="https://developer.mozilla.org">
  <img src="https://developer.mozilla.org/static/img/favicon144.png"
       alt="Visit the MDN site" />
</a>

Proposed exercise: Top ten pages

Create a web page showing an ordered list (<ol>) of the top ten web pages you like. You must use images to create links to each page, and set the height attribute to the same value for all the images so that all of them are the same height. For example:

<ol>
  <li>
    <a href="https://developer.mozilla.org">
      <img src="https://developer.mozilla.org/static/img/favicon144.png" 
           alt="MDN site" height="100" />
    </a>
  </li>
  <li>
    <a href="https://validator.w3.org/">
      <img src="https://validator.w3.org/images/w3c.png" 
           alt="Markup Validation Service" height="100" />
    </a>
  </li>
  ...
</ol>

Annotating images with figures and figure captions

Speaking of captions, there are a number of ways that you could add a caption to go with your image. For example, there would be nothing to stop you from doing this:

<div class="figure">
  <img src="images/dinosaur.jpg"
       alt="The head and torso of a dinosaur skeleton;
            it has a large head with long sharp teeth"
       width="400"
       height="340">

  <p>A T-Rex on display in the Manchester University Museum.</p>
</div>

This is ok, since it contains the content you need, and can be nicely styled using CSS. But there is a problem here: there is nothing that semantically links the image to its caption, which can cause problems for screen readers. For example, when you have 50 images and captions, which caption goes with which image?

A better solution is to use the HTML5 <figure> and <figcaption> elements. These are created for exactly this purpose: to provide a semantic container for figures, and to clearly link the figure to the caption. Our above example could be rewritten like this:

<figure>
  <img src="images/dinosaur.jpg"
       alt="The head and torso of a dinosaur skeleton;
            it has a large head with long sharp teeth"
       width="400"
       height="340">

  <figcaption>A T-Rex on display in the Manchester University Museum.</figcaption>
</figure>

The <figcaption> element tells browsers, and assistive technology that the caption describes the other content of the <figure> element. From an accessibility viewpoint, captions and alt text have distinct roles. Captions benefit even people who can see the image, whereas alt text provides the same functionality as an absent image. Therefore, captions and alt text shouldn’t just say the same thing, because they both appear when the image is gone. Try turning images off in your browser and see how it looks.

Also keep in mind that a figure could be made of several images, a code snippet, audio, video, equations, a table, or something else. We will go through this in another unit.

Proposed exercise: Figures with captions

Create a web page to display several images you like, using the <figure> element, and add a caption to each one using the <figcaption> element, as it is done in the example below:

<figure>
  <img src="https://developer.mozilla.org/static/img/favicon144.png"
       alt="Mozilla developer website">
  <figcaption>MDN Logo</figcaption>
</figure>

<figure>
  <img src="https://validator.w3.org/images/w3c.png"
       alt="W3C Validator">
  <figcaption>W3C Logo</figcaption>
</figure>

...

Proposed exercise: Top ten films

Create a new web page showing an ordered list (<ol>) of the top ten films you like. In this case you must use figures with captions to create links to each page. Set the height attribute to the same value for all the images so that all of them are the same height. For example:

<ol>
  <li>
    <a href="https://en.wikipedia.org/wiki/Steve_Jobs_(film)">
      <figure>
        <img src="https://upload.wikimedia.org/wikipedia/commons/4/49/SteveJobsMacbookAir.JPG" 
             alt="Steve Jobs and Macbook Air" height="100" />
        <figcaption>Steve jobs</figcaption>
      </figure>
    </a>
  </li>
  <li>
    <a href="https://en.wikipedia.org/wiki/2001:_A_Space_Odyssey_(film)">
      <figure>
        <img src="https://upload.wikimedia.org/wikipedia/en/0/09/2001child2.JPG" 
             alt="Baby, space and Earth" height="100" />
        <figcaption>2001: A Space Odyssey</figcaption>
      </figure>
    </a>
  </li>
  ...
</ol>

Quiz

Test your skills with this quiz about images and some other concepts related to this unit.

HTML. Unit 4. Lists.

Introduction

Now let’s turn our attention to lists. Lists are everywhere in life —from your shopping list to the list of directions you subconsciously follow to get to your house every day, to the lists of instructions you are following in these tutorials! Lists are everywhere on the web, too, and we’ve got three different types to worry about.

Unordered lists (<ul> element)

Unordered lists are used to mark up lists of items for which the order of the items doesn’t matter. Let’s take a shopping list in plain text as an example:

milk
eggs
bread
hummus

In HTML, unordered lists can be displayed like this:

  • milk
  • eggs
  • bread
  • hummus

To get that result and display the bulleted list, every unordered list starts off with a <ul> element (this wraps around all the list items). The last step is to wrap each list item in a <li> (list item) element:

<ul>
  <li>milk</li>
  <li>eggs</li>
  <li>bread</li>
  <li>hummus</li>
</ul>

Ordered lists (<ol> element)

Ordered lists are lists in which the order of the items does matter. Let’s take a set of directions in plain text as an example:

Drive to the end of the road
Turn right
Go straight across the first two roundabouts
Turn left at the third roundabout
The school is on your right, 300 meters up the road

In HTML, ordered lists can be displayed like this:

  1. Drive to the end of the road
  2. Turn right
  3. Go straight across the first two roundabouts
  4. Turn left at the third roundabout
  5. The school is on your right, 300 meters up the road

The markup structure is the same as for unordered lists, except that you have to wrap the list items in an <ol> element, rather than <ul>:

<ol>
  <li>Drive to the end of the road</li>
  <li>Turn right</li>
  <li>Go straight across the first two roundabouts</li>
  <li>Turn left at the third roundabout</li>
  <li>The school is on your right, 300 meters up the road</li>
</ol>

Proposed exercise: Hummus recipe

At this point you have all the information you need to mark up a recipe page example. Using the text below, create a new file and use the proper tags to improve the format of the hummus recipe:

Note: You must use <h1> for the main title, <h2> for the other titles, <p> for the paragraphs, <ul> for the ingredients, and <ol> for the instructions.
Quick hummus recipe

This recipe makes quick, tasty hummus, with no messing. It has been adapted from a number of different recipes that I have read over the years.

Hummus is a delicious thick paste used heavily in Greek and Middle Eastern dishes. It is very tasty with salad, grilled meats and pitta breads.

Ingredients

1 can (400g) of chick peas (garbanzo beans)
175g of tahini
6 sundried tomatoes
Half a red pepper
A pinch of cayenne pepper
1 clove of garlic
A dash of olive oil

Instructions

Remove the skin from the garlic, and chop coarsely
Remove all the seeds and stalk from the pepper, and chop coarsely
Add all the ingredients into a food processor
Process all the ingredients into a paste
If you want a coarse "chunky" hummus, process it for a short time
If you want a smooth hummus, process it for a longer time

For a different flavour, you could try blending in a small measure of lemon and coriander, chili pepper, lime and chipotle, harissa and mint, or spinach and feta cheese. Experiment and see what works for you.

Storage

Refrigerate the finished hummus in a sealed container. You should be able to use it for about a week after you've made it. If it starts to become fizzy, you should definitely discard it.

Hummus is suitable for freezing; you should thaw it and use it within a couple of months.

Nesting lists

It is perfectly ok to nest one list inside another one. You might want to have for example a sublist sitting below a top-level bullet:

  • first item
  • second item
    • second item first subitem
    • second item second subitem
    • second item third subitem
  • third item
<ul>
  <li>first item</li>
  <li>second item     
  <!-- Look, the closing </li> tag is not placed here! -->
    <ul>
      <li>second item first subitem</li>
      <li>second item second subitem</li>
      <li>second item third subitem</li>
    </ul>
  <!-- Here is the closing </li> tag -->
  </li>
  <li>third item</li>
</ul>

And you also might want to have an ordered list inside an unordered list:

  • first item
  • second item
    1. second item first subitem
    2. second item second subitem
    3. second item third subitem
  • third item
<ul>
  <li>first item</li>
  <li>second item
  <!-- Look, the closing </li> tag is not placed here! -->
    <ol>
      <li>second item first subitem</li>
      <li>second item second subitem</li>
      <li>second item third subitem</li>
    </ol>
  <!-- Here is the closing </li> tag -->
  </li>
  <li>third item</li>
</ul>

Let’s take a look now at the second list from our recipe example, and we will understand how useful nested lists can be:

<ol>
  <li>Remove the skin from the garlic, and chop coarsely.</li>
  <li>Remove all the seeds and stalk from the pepper, and chop coarsely.</li>
  <li>Add all the ingredients into a food processor.</li>
  <li>Process all the ingredients into a paste.</li>
  <li>If you want a coarse "chunky" hummus, process it for a short time.</li>
  <li>If you want a smooth hummus, process it for a longer time.</li>
</ol>

Since the last two bullets are very closely related to the one before them (they read like sub-instructions or choices that fit below that bullet), it might make sense to nest them inside their own unordered list and put that list inside the current fourth bullet. This would look like so:

<ol>
  <li>Remove the skin from the garlic, and chop coarsely.</li>
  <li>Remove all the seeds and stalk from the pepper, and chop coarsely.</li>
  <li>Add all the ingredients into a food processor.</li>
  <li>Process all the ingredients into a paste.
    <ul>
      <li>If you want a coarse "chunky" hummus, process it for a short time.</li>
      <li>If you want a smooth hummus, process it for a longer time.</li>
    </ul>
  </li>
</ol>

Proposed exercise: Hummus recipe

Using the previous proposed exercise of the hummus recipe, update the second list to use nested items to get the following result:

  1. Remove the skin from the garlic, and chop coarsely.
  2. Remove all the seeds and stalk from the pepper, and chop coarsely.
  3. Add all the ingredients into a food processor.
  4. Process all the ingredients into a paste.
    • If you want a coarse “chunky” hummus, process it for a short time.
    • If you want a smooth hummus, process it for a longer time.

Proposed exercise: Shopping list

Write your own shopping list, with at least 25 products. You must use types and subtypes, by wrapping the items in nested lists as the following example:

  • Bread
  • Milk
  • Coffee
  • Tea
    • Black tea
    • Green tea
  • Fruit
    • Oranges
    • Apples
<ul>
  <li>Bread</li>
  <li>Milk</li>
  <li>Coffee</li>
  <li>Tea
    <ul>
      <li>Black tea</li>
      <li>Green tea</li>
    </ul>
  </li>
  <li>Fruit
    <ul>
      <li>Oranges</li>
      <li>Apples</li>
    </ul>
  </li>
</ul>

Proposed exercise: Things to do

Create a new file containing a list of things to do, with at least 25 items. You must use nested lists mixing things and instructions, for example, by inserting ordered lists inside an unordered list, more or less like the example below:

  • Feed cat
    1. Wash the bowl
    2. Open cat food
    3. Deliver it to the cat
  • Wash car
    1. Vacuum interior
    2. Wash exterior
    3. Wax exterior
  • Grocery shopping
    1. Plan meals
    2. Clean out fridge
    3. Make list
    4. Go to the store
<ul>
   <li>Feed cat
     <ol>
       <li>Wash the bowl</li>
       <li>Open cat food</li>
       <li>Deliver it to the cat</li>
     </ol>
   </li>
   <li>Wash car
     <ol>
       <li>Vacuum interior</li>
       <li>Wash exterior</li>
       <li>Wax exterior</li>
     </ol>
   </li>
   <li>Grocery shopping
     <ol>
       <li>Plan meals</li>
       <li>Clean out fridge</li>
       <li>Make list</li>
       <li>Go to the store</li>
     </ol>
   </li>
</ul>

Description lists (<dl> element)

We have walked through how to mark up basic lists. Now we are going to mention the third type of list, which you will occasionally come across: description lists. The purpose of these lists is to mark up a set of items and their associated descriptions, such as terms and definitions, or questions and answers. Let’s look at an example of a set of terms and definitions:

soliloquy

In drama, where a character speaks to themselves, representing their inner thoughts or feelings and in the process relaying them to the audience (but not to other characters.)

monologue

In drama, where a character speaks their thoughts out loud to share them with the audience and any other characters present.

aside

In drama, where a character shares a comment only with the audience for humorous or dramatic effect. This is usually a feeling, thought or piece of additional background information

Description lists use a different wrapper than the other list types, since they are delimited by the <dl> tag. In addition each term is wrapped in a <dt> (description term) element, and each description is wrapped in a <dd> (description definition) element. Let’s finish marking up our example:

<dl>
  <dt>soliloquy</dt>
  <dd>In drama, where a character speaks to themselves, representing their inner thoughts or feelings and in the process relaying them to the audience (but not to other characters.)</dd>

  <dt>monologue</dt>
  <dd>In drama, where a character speaks their thoughts out loud to share them with the audience and any other characters present.</dd>

  <dt>aside</dt>
  <dd>In drama, where a character shares a comment only with the audience for humorous or dramatic effect. This is usually a feeling, thought, or piece of additional background information.</dd>
</dl>

The browser will display description lists with the descriptions indented somewhat from the terms. Also note that it is permitted to have a single term with multiple descriptions, for example:

<dl>
  <dt>aside</dt>
  <dd>In drama, where a character shares a comment only with the audience for humorous or dramatic effect. This is usually a feeling, thought, or piece of additional background information.</dd>
  <dd>In writing, a section of content that is related to the current topic, but doesn't fit directly into the main flow of content so is presented nearby (often in a box off to the side.)</dd>
</dl>

Proposed exercise: Coffee, Tea and Chocolate

It’s time to try your hand at description lists. Create a new file and add elements to the raw text below so that it appears as a description list:

Coffee

Coffee is a brewed beverage with a distinct aroma and flavor prepared from the roasted seeds of the Coffea plant.

Tea

Tea is an aromatic beverage commonly prepared by pouring hot or boiling water over cured leaves of the tea plant.

Hot chocolate

Hot chocolate (also known as hot cocoa) is a heated beverage typically consisting of shaved chocolate, melted chocolate or cocoa powder, heated milk or water, and sugar.

Proposed exercise: Bacon, Eggs and Coffee

Create a new file with the text below and add elements to the raw text so that it appears as a description list. You could try using your own terms and descriptions if you like:

Bacon

Cured meat prepared from a pig.

Eggs

Common food and one of the most versatile ingredients used in cooking.

Coffee

The drink that gets the world running in the morning.
A light brown color.

Proposed exercise: Computer hardware

Write a definition list to explain the meaning of the following terms: Central processing unit, monitor, mouse, keyboard, graphics card, sound card, speakers and motherboard.

Quiz

Test your skills with this quiz about text formatting and some other concepts related to this unit.

HTML. Unit 3. Hyperlinks.

What is a hyperlink

Hyperlinks are one of the most important innovations the Web has to offer. They’ve been a feature of the Web since the beginning, and they are what makes the Web a web. Hyperlinks allow us to link documents to other documents or resources, link to specific parts of documents, or make apps available at a web address. Almost any web content can be converted to a link so that when clicked or otherwise activated the web browser goes to another web address. These links are also known as URL’s, or Uniform Resource Locators, text strings that specify where a resource (such as a web page, image, or video) can be found on the Internet.

A URL can point to HTML files, text files, images, text documents, video and audio files, or anything else that lives on the Web. If the web browser doesn’t know how to display or handle the file, it will ask you if you want to open the file (in which case the duty of opening or handling the file is passed to a suitable native app on the device) or download the file (in which case you can try to deal with it later on).

For example, the BBC homepage (https://www.bbc.com/) contains many links that point not only to multiple news stories, but also different areas of the site (navigation functionality), login/registration pages (user tools), and more.

The <a> element

The <a> element (or anchor element) with its href attribute, creates a hyperlink to web pages, files, email addresses, locations in the same page, or anything else a URL can address. Content within each <a> should indicate the link’s destination.

Links also provide implicitly some information about the navigation activity, as they will appear as follows in all browsers by default:

  • An unvisited link is underlined and blue
  • A visited link is underlined and purple
  • An active link is underlined and red

A basic link is created by wrapping the text or other content, inside an <a> element and using the href attribute, also known as a Hypertext Reference, or target, that contains the web address:

<p>
    I'm creating a link to
    <a href="https://www.mozilla.org/en-US/">the Mozilla homepage</a>.
</p>

The code above gives as the following result: I’m creating a link to the Mozilla homepage.

Adding information with the title attribute

Another attribute you may want to add to your links is title. The title contains additional information about the link, such as which kind of information the page contains, or things to be aware of on the web site:

<p>
    I'm creating a link to
    <a href="https://www.mozilla.org/en-US/" 
       title="The best place to find more information about Mozilla's mission and how to contribute">the Mozilla homepage</a>.
</p>

This gives us the following result and hovering over the link displays the title as a tooltip: I’m creating a link to the Mozilla homepage.

Keep in mind that a link title is only revealed on mouse hover, which means that people relying on keyboard controls or touchscreens to navigate web pages will have difficulty accessing title information. If a title’s information is truly important to the usability of the page, then you should present it in a manner that will be accessible to all users, for example by putting it as a regular text.

Proposed exercise: Links with information

Create a web page containing the information below. Use a main <h1> heading, a couple of paragraphs, and links to the pages in the list.

Note: you must also use the title attribute to display information about each link on mouse hover.
Alicante

Amongst the most notable features of the city are the Castle of Santa Bárbara, which sits high above the city, and the port of Alicante. The latter was the subject of bitter controversy in 2006–2007 as residents battled, successfully, to keep it from being changed into an industrial estate.

The Santa Bárbara castle is situated on Mount Benacantil, overlooking the city. The tower (La Torreta) at the top, is the oldest part of the castle, while part of the lowest zone and the walls were constructed later in the 18th century.

URLs and paths

To fully understand link targets, you need to understand URLs and file paths. This section gives you the information you need to achieve this.

A URL, or Uniform Resource Locator is simply a string of text that defines where something is located on the Web. For example, Mozilla’s English homepage is located at https://www.mozilla.org/en-US/.

URLs use paths to find files. Paths specify where the file you’re interested in is located in the filesystem. Let’s look at an example of a directory structure, see the creating-hyperlinks directory.

The root of this directory structure is called creating-hyperlinks. When working locally with a web site, you’ll have one directory that contains the entire site. Inside the root, we have an index.html file and a contacts.html. In a real website, index.html would be our home page or landing page (a web page that serves as the entry point for a website or a particular section of a website.).

There are also two directories inside our root — pdfs and projects. These each have a single file inside them — a PDF (project-brief.pdf) and an index.html file, respectively. Note that you can have two index.html files in one project, as long as they’re in different filesystem locations. The second index.html would perhaps be the main landing page for project-related information.

Same directory

If you wanted to include a hyperlink inside index.html (the top level index.html) pointing to contacts.html, you would specify the filename that you want to link to, because it’s in the same directory as the current file. The URL you would use is contacts.html:

<p>Want to contact a specific staff member?
Find details on our <a href="contacts.html">contacts page</a>.</p>

Moving down into subdirectories

If you wanted to include a hyperlink inside index.html (the top level index.html) pointing to projects/index.html, you would need to go down into the projects directory before indicating the file you want to link to. This is done by specifying the directory’s name, then a forward slash, then the name of the file. The URL you would use is projects/index.html:

<p>Visit my <a href="projects/index.html">project homepage</a>.</p>

Moving back up into parent directories

If you wanted to include a hyperlink inside projects/index.html pointing to pdfs/project-brief.pdf, you’d have to go up a directory level, then back down into the pdf directory. To go up a directory, use two dots — .. — so the URL you would use is ../pdfs/project-brief.pdf:

<p>A link to my <a href="../pdfs/project-brief.pdf">project brief</a>.</p>

You can combine multiple instances of these features into complex URLs, if needed, for example: ../../../complex/path/to/my/file.html.

Proposed exercise: Navigation menu

Create an “index.html” file to link your pages together with a navigation menu to create a multi-page website. This index file will link to all of your pages you have created before. Have a look at the example below so that you can get an idea of how your source code should look like. Additionally, you must also use the title attribute to insert a description of what it is inside each file linked.

Note: From now on, each time you create a new file for any exercise, you should link it from your “index.html” file, and of course, you must validate everything each time you change something in your code or your domain.
<h1>HTML exercises</h1>

<h2>Headings and paragraphs</h2>

<p><a href="headings_paragraphs_1.html">Headings and paragraphs example 1</p>
<p><a href="headings_paragraphs_2.html">Headings and paragraphs example 2</p>
...

<h2>Text formatting</h2>

<p><a href="text_formatting_1.html">Text formatting example 1</p>
<p><a href="text_formatting_2.html">Text formatting example 2</p>
...

Document fragments

It’s possible to link to a specific part of an HTML document, known as a document fragment, rather than just to the top of the document. To do this you first have to assign an id attribute to the element you want to link to. It normally makes sense to link to a specific heading, so this would look something like the following:

<h2 id="mailing_address">Mailing address</h2>

Then to link to that specific id, you’d include it at the end of the URL, preceded by a hash symbol (#), for example:

<p>Want to write us a letter? Use our <a href="contacts.html#mailing_address">mailing address</a>.</p>

You can even use the document fragment reference on its own to link to another part of the same document:

<p>The <a href="#mailing_address">company mailing address</a> can be found at the bottom of this page.</p>

Proposed exercise: Table of contents

Create a page similar to this one: https://en.wikipedia.org/wiki/Alicante (you may choose any page you like containing a large table of contents). You must start with an index, which must be followed by several sections (at least 10), each one preceded with a heading and containing several paragraphs. Also insert several links in each paragraph, as it is done in Wikipedia (fill the title attribute too). Have a look at this example and do something similar on your own:

<h1>Alicante</h1>

<p><a href="#geography">Geography</a></p>
<p><a href="#history">History</a></p>
...

<h2 id="geography">Geography</h2>

Alicante is located in the southeast of the <a href="https://en.wikipedia.org/wiki/Iberian_Peninsula">Iberian Peninsula</a>, on the shores of the <a href="https://en.wikipedia.org/wiki/Mediterranean_Sea">Mediterranean Sea</a>.
...

<h2 id="history">History</h2>

The area around Alicante has been inhabited for over 7000 years. The first tribes of <a href="https://en.wikipedia.org/wiki/Hunter-gatherer">hunter-gatherers</a> moved down gradually from Central Europe between 5000 and 3000 BC. Some of the earliest settlements were made on the slopes of <a href="https://en.wikipedia.org/wiki/Mount_Benacantil">Mount Benacantil</a>.
...

Block level links

As mentioned before, almost any content can be made into a link, even block-level elements. If you have an image you want to make into a link, use the <a> element and reference the image file with the <img> element (we will learn more about using images in a future unit):

<a href="https://www.mozilla.org/en-US/">
  <img src="mozilla-image.png" alt="mozilla logo that links to the mozilla homepage">
</a>

Proposed exercise: Image as a link

Using the image https://iessanvicente.com/ies_san_vicente.jpg, create a link to the url https://iessanvicente.com so that when you click on the picture, you will jump to the web page of IES San Vicente.

Note: you must use the full url of the image as the src attribute of the <img> element.

E-mail links

It is possible to create links or buttons that, when clicked, open a new outgoing email message rather than linking to a resource or page. This is done using the <a> element to mailto:.

In its most basic and commonly used form, a mailto: link simply indicates the email address of the intended recipient. For example:

<a href="mailto:[email protected]">Send email to nowhere</a>

This code results in a link that looks like this: Send email to nowhere.

In fact, the email address is optional. If you omit it and your href is simply “mailto:”, a new outgoing email window will be opened by the user’s email client with no destination address. This is often useful as “Share” links that users can click to send an email to an address of their choosing.

Specifying details

In addition to the email address, you can provide other information. In fact, any standard mail header fields can be added to the mailto URL you provide. The most commonly used of these are “subject”, “cc”, and “body” (which is not a true header field, but allows you to specify a short content message for the new email). Each field and its value is specified as a query term.

Here’s an example that includes a cc, bcc, subject and body:

<a href="mailto:[email protected][email protected]&[email protected]&subject=The%20subject%20of%20the%20email&body=The%20body%20of%20the%20email">
  Send mail with cc, bcc, subject and body
</a>

Keep in mind that the values of each field must be URL-encoded, that is with non-printing characters (invisible characters like tabs, carriage returns, and page breaks) and spaces percent-escaped. Also note the use of the question mark (?) to separate the main URL from the field values, and ampersands (&) to separate each field in the mailto: URL. This is standard URL query notation. Read The GET method to understand what URL query notation is more commonly used for.

Proposed exercise: E-mail links

Create a web page to display the following links (use paragraphs so that each link is shown in a different line):

mailto:
mailto:[email protected]
mailto:[email protected],[email protected]
mailto:[email protected][email protected]
mailto:[email protected][email protected]&subject=This%20is%20the%20subject

Quiz

Test your skills with this quiz about text formatting and some other concepts related to this unit.

HTML. Unit 2. Text formatting.

Introduction

In order to make the structure of our text clearer, we can mark some elements to make them more relevant than other elements. In HTML there are some tags for this purpose:

<b>Bold text</b>
<strong>Important text</strong>
<i>Italic text</i>
<em>Emphasized text</em>
<mark>Marked (or highlighted) text</mark>
<small>Small text</small>
<del>Deleted text</del>
<ins>Inserted text</ins>
<sub>Subscript text</sub>
<sup>Superscript text</sup>

The <b> and <strong> elements

The <b> element represents a piece of bold text to which attention is being drawn, but it does not add any extra importance. On the other hand, the text inside the <strong> element is displayed in bold too, but it also adds a strong importance to the text, which could be meaningfull when the web page is analyzed and processed to extract important words or expressions. This kind of text analysis is done for example by some search engines, which need to know what any page is about to include it in the results when a user is searching for pages containing specific keywords.

Proposed exercise: Important text

Create a web page with the text below using a header and a paragraph, and also add extra importance to several words inside the paragraph:

Doctors without borders

In May 1968, a group of young doctors decided to go and help victims of wars and major disasters. This new brand of humanitarianism would reinvent the concept of emergency aid. They were to become Médecins Sans Frontières (MSF), known internationally in English as Doctors Without Borders.

The <i> and <em> elements

The <i> element defines a part of the text in an alternate voice or mood, and the content inside is usually displayed in italic. It is often used to indicate a technical term, a phrase from another language, a thought, etc. On the other hand, the HTML <em> defines an emphasized text, which is displayed in italic too, but the result is different when a screen reader is used, since the words inside the <em> tag will be pronounced with an emphasis, using verbal stress.

Proposed exercise: Emphasized text

Create a web page with the text below using a header and a couple paragraphs, and also emphasize several words inside the paragraphs:

People first

MSF was officially created on December 22, 1971. At the time, 300 volunteers made up the organization: doctors, nurses, and other staff, including the 13 founding doctors and journalists.

MSF was created on the belief that all people have the right to medical care regardless of gender, race, religion, creed, or political affiliation, and that the needs of these people outweigh respect for national boundaries.

The <del> and <ins> elements

The <del> element is used to define a text which has been deleted from a document. Search engines sometimes use this tag to show the changes inside a website, when some text has been removed after the developer has uploaded a new version.

On the other hand, the <ins> element is used to define a text which has been inserted into a document. Search engines sometimes use this tag to show the changes inside a website, when some text has been replaced after the developer has uploaded a new version.

It depends on the browser how to display the text inside these elements, but most of them will strike a line through deleted text, and will underline the inserted text by default.

Proposed exercise: Deleted and inserted text

Create a page to show several paragraphs where some words or sentences were previously said, but now the authors have changed their minds and want to tell their readers a new version of their thoughts. Using the <del> and <ins> tags, show the previous texts and the new ones in a way that makes clear that the new text is replacing the first one. The result should look like this (you should write at least 10 paragraphs):

The Earth is flat rounded.

On 31 December 2020 the whole world is going to end! carry on as usual.

...

The <mark> element

The <mark> element represents text which is marked or highlighted for reference or notation purposes, due to the marked passage’s relevance or importance in the enclosing context.

Proposed exercise: Highlighted text

Create a web page with the text below using a header and a couple paragraphs, and also highlight several words or figures inside the paragraphs:

Building MSF

Since 1980, MSF has opened offices in 28 countries and employs more than 30,000 people across the world. Since its founding, MSF has treated over a hundred million patients. 

MSF has also maintained itsinstitutional and financial independence, and the organization has continued to be critical of both itself and the broader aid system when appropriate, all in the name of trying to help direct more effective and timely aid to those who need it most.

Proposed exercise: Highlighted text

Create a web page with the following text and highlight the “salamander” word in the last two paragraphs:

Search results for "salamander":

Several species of salamander inhabit the temperate rainforest of the Pacific Northwest.

Most salamanders are nocturnal, and hunt for insects, worms, and other small creatures.

Proposed exercise: Highlighted text

Create a web page with the following heading and paragraph, and mark the text “Rebel spies managed to steal secret plans”:

Star Wars Episode IV - A New Hope

It is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire. During the battle, Rebel spies managed to steal secret plans to the Empire’s ultimate weapon, the DEATH STAR, an armored space station with enough power to destroy an entire planet.

The <sub> and <sup> elements

The <sub> and <sup> elements specify inline text which should be displayed as subscript (or superscript) for solely typographical reasons. Subscripts are typically rendered with a lowered baseline using smaller text, and superscripts are usually rendered with a raised baseline using smaller text.

Proposed exercise: Subscripts

Create a web page to display the following text:

One of my favorite molecules is C8H10N4O2, also known as "caffeine".

The horizontal coordinates' positions along the X-axis are represented as x1 ... xn.

Proposed exercise: Subscripts

Create a page to display the chemical formula of some common compounds, applying subscript formatting to the numbers (you must print each compound in a different paragraph, using the <p> element). The result should look like this (you must add some other compounds on your own):

Oxygen: O2
Water: H2O
Carbon dioxide: CO2
Vinegar: C2H4O2
Baking soda: NaHCO3
Sugar: C12H22O11
Alcohol: C2H5OH
...

Proposed exercise: Superscripts

Create a web page to display the following text:

The Pythagorean theorem is often expressed as the following equation: a2 + b2 = c2

One of the most common equations in all of physics is: E = mc2

The ordinal number "fifth" can be abbreviated in English as follows: 5th

Proposed exercise: Superscripts

Print the squares of the numbers 1 – 25. Each number should be on a separate line (using the <br /> element), next to it the number 2 superscripted, an equal sign and the result. Something like this should be displayed:

12 = 1
22 = 4
...
252 = 625

The <span> element

If we need to mark a text but we consider that none of the existing tags is a good option, we can use the <span> element. Thus, we can isolate a piece of text although there are no changes in the appearence of the text marked when using it on its own (<span>...</span>). The advantage of this tag comes when it is used together with some attributes like class or style, combined with some CSS code. We will work on this deeply in the future, but by now, let’s have a look for example at this code, where we use a <span> element to color only a part of the text inside a paragraph:

<p>My mother has <span style="color:blue">blue</span> eyes.</p>

Proposed exercise: Text color

Using the <p> and <span> elements, create a web page to display the color of the eyes (e.g. blue, brown, green, gray) of some people you know.

Quiz

Test your skills with this quiz about text formatting and some other concepts related to this unit.

HTML. Unit 1. Basic HTML file structure.

Introduction

HTML is the standard markup language for creating web pages. HTML stands for HyperText Markup Language. This language consists on some elements which define some kind of content. An HTML file can be edited by any simple text editor but there are some editors that use colors and tips to help the developer. One of the best free ones is Visual Studio Code. It can be downloaded here.

Elements

Each element has two parts, a start tag and an end tag, and the content inserted in between:

<tagname>Content goes here...</tagname>

The HTML element is defined by everything from the start tag to the end tag. For example:

<h1>My First Heading</h1>
<p>My first paragraph.</p>

Void elements

There are a few elements which are made of only a start tag (usually called “void elements” or “self-closing tags”). A self-closing tag is a special form of start tag with a slash immediately before the closing right angle bracket. These indicate that the element is to be closed immediately, and has no content. For example, in the following code, the void element <br /> is used to print a new line in the middle of a paragraph:

<p>The quick brown fox<br />jumps over the lazy dog.</p>

Attributes

HTML attributes provide additional information about HTML elements. We must keep in mind the following considerations:

  • All HTML elements can have attributes.
  • Attributes are always specified in the start tag
  • Attributes usually come in name/value pairs like: name=”value”

For example, using the following code we can jump to the page https://www.wikipedia.org (the <a> tag defines an hyperlink, and the href attribute specifies the URL of the page where the link goes to):

<a href="https://www.wikipedia.org">Wikipedia</a>

Internet and web pages

So that an end user can get and read the contents of a web site, some basic steps must be followed:

  1. To make web pages, the developer has to create files written in HTML (with any text editor) and place them on a web server (e.g., using an ftp client).
  2. The HTML in any web page tells the browser what it needs to know to display the page. And, if the developer develops the web site in the right way, the pages will even display well on both desktop and mobile devices.
  3. Once the files are put on a web server, any browser can retrieve the web pages over the Internet.
  4. Finally, the end users will use PCs and other devices connected to the Internet, all running web browsers.

The web server

The web server is just a computer connected to the Internet waiting for requests from browsers:

  1. The developer stores HTML, files, pictures, sounds and other file types.
  2. Browsers make requests for HTML pages or other resources, like images.
  3. If the server can locate the resources, it sends them to the browsers.

The web browser

The web browser is just an application which is in charge of displaying websites:

  1. The server delivers web pages sending them to the browsers.
  2. The browsers retrieve the pages.
  3. The browsers display the HTML pages to the end users.

<!doctype> and <html> elements

All HTML documents must start with a document type declaration, meaning we are using the latest version of HTML (HTML5). The source code of the document itself will be enclosed between the tags <html> and </html>:

<!doctype html>
<html lang="es">
...
</html>

The <head> section

The <head> element will mainly contain information which is not displayed on the screen (metadata, links to libraries, etc.). For example:

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="bootstrap.min.css" />
    ...
  </head>  
  ...
</html>

The <meta> element

Metadata

It is important that your webpages contain a list of metadata items in order to provide search engines (e.g., Google, DuckDuckGo, Bing, etc.) with semantic information such as the author, keywords, description, etc. Since this kind of information will not be shown to the user by the browser, metadata items must be placed inside the <head> tag:

<head>
  <meta name="description" content="My first web page" />
  <meta name="keywords" content="HTML, CSS, JavaScript" />
  <meta name="author" content="John Doe" />
</head>

Charset

One of the most important items inside the <head> section is the definition of the charset , so that the browser will be able to correctly display special characters such as á, é, ü, ñ, etc.:

<meta charset="utf-8" />

Viewport

HTML5 introduced a method to let web designers take control over the viewport, through the <meta> tag. The viewport is the user’s visible area of a web page. It varies with the device, and it will be smaller on a mobile phone than on a computer screen.

You should include the following <meta name="viewport"> element inside all of your web pages:

<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />

The <title> element

We can define a title for our document with a tag also in the <head> section. This text will appear in the tab of the browser.

<title>My first page</title>

The <link> element

The <link> element is used to link to external style sheets (CSS):

<link rel="stylesheet" href="mystyle.css" />

The <script> element

The <script> element is used to include or define blocks of client-side JavaScript code:

<!-- Include external JavaScript file -->
<script src="myScript.js"></script>
...
<!-- Define a JavaScript piece of code inside the html file -->
<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "Hello JavaScript!";
}
</script>

The <body> section

Inside the <body> section we must insert the content to be shown by the browser to the user. It will be opened after closing the <head> section and it will be closed before closing the <html> section. For example, using the following code, only the text “Hello!” will be displayed on the screen by the browser:

<!doctype html>
<html lang="es">
<head>
  <meta charset="utf-8" />
  <meta name="description" content="My first web page" />
  <meta name="keywords" content="HTML, CSS, JavaScript" />
  <meta name="author" content="John Doe" />
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
  <title>Sample page</title>
</head>
<body>
  Hello!
</body>
</html>

Proposed exercise: Hellow world

Create a web page to display the text “Hi World, my name is … !” inside the <body> section. You must fill also the <head> section so that the browser tab shows the “Greeting” title. Finally, insert the rest of the <meta> elements (charset, viewport, author, description and keywords), and check the results using a web browser.

Paragraphs and sections

For a better understanding of the text of our web page, we should divide it in paragraphs and sections.

A paragraph can be inserted between the <p> and </p> tags, and there are some tags for the section headings such as: <h1>, <h2>, <h3>, <h4>, <h5> and <h6> (from largest to smallest):

...
<h1>First level heading</h1>
<p>My first paragraph</p>
<p>My second paragraph</p>
<p>My third paragraph</p>

<h2>Second level heading</h2>
<p>My fourth paragraph</p>
<p>My fifth paragraph</p>
...

Proposed exercise: Paragraphs

Create a web page to display several paragraphs enclosed by the <p> and </p> tags, all of them containing several lines of any text you like. Finally, insert the rest of the <meta> elements (charset, viewport, author, description, keywords and title), and check the results using a web browser.

Proposed exercise: Headings and paragraphs

Create a web page to display several headings of different levels using all tags from <h1> to <h6>. Also put some paragraphs (enclosed by the <p> and </p> tags) with any content you like, each one after a heading. Finally, insert the rest of the <meta> elements (charset, viewport, author, description, keywords and title), and check the results using a web browser.

Proposed exercise: Headings and paragraphs

Use the correct HTML code and also open and close the <h1> and <p> tags in the right place to create a web page to display the following text:

Alicante

Alicante is a city on the Valencian Community, Spain. It is the capital of the province of Alacant and a historic Mediterranean port. The population of the city was 330,525 as of 2016, the second-largest in Valencian Community.

Proposed exercise: Headings

Use the correct HTML tags to create a web page containing six headings with the text “Hello”. Start with the most important heading (the largest) and end with the least important heading (the smallest).

Proposed exercise: Headings and paragraphs

Mark up the following text with appropriate tags (<h1>, <h2>, <h3> and <p>). Start with the most important heading (the largest) and end with the least important heading (the smallest) followed by a paragraph:

Valencia

Valencia is the capital of the autonomous community of Valencia and the third-largest city in Spain after Madrid and Barcelona, surpassing 800,000 inhabitants in the municipality. The wider urban area also comprising the neighbouring municipalities has a population of around 1.6 million people.

Geography

Location

Located on the eastern coast of the Iberian Peninsula and the western part of the Mediterranean Sea, fronting the Gulf of Valencia, Valencia lies on the highly fertile alluvial silts accumulated on the floodplain formed in the lower course of the Turia River.

Climate

Valencia has a subtropical Mediterranean climate with mild winters and hot, dry summers. Snowfall is extremely rare; the most recent occasion snow accumulated on the ground was on 11 January 1960.

The <br /> element

The <br /> element produces a line break in text (carriage-return). It is useful for example for writing a poem or an address, where the division of lines is significant. Keep in mind that you should not use <br /> to create margins between paragraphs, since the <p> tag is much more suitable for those cases.

As you can see from the example below, a <br /> element is included at each point where we want the text to break. The text after the <br /> begins again at the start of the next line of the text block:

<p> 
    O’er all the hilltops<br />
    Is quiet now,<br />
    In all the treetops<br />
    Hearest thou<br />
    Hardly a breath;<br />
    The birds are asleep in the trees:<br />
    Wait, soon like these<br />
    Thou too shalt rest.
</p>

Proposed exercise: Poems

Create a web page containing several poems from different authors, like the one below (you must use <h1> and <h2> headings, paragraphs and line breaks):

Note: You should use an <h1> heading for the main heading and <h2> for the rest of the headings. Also keep in mind that you should not use <br /> to create margins between each poem; use paragraphs to enclose each poem, and line breaks for each line.
POEMS

William Shakespeare

Be not afraid of greatness.
Some are born great,
some achieve greatness,
and others have greatness thrust upon them.

...

Proposed exercise: Addresses

Create a document to display several addresses like the one below (you must use <h1> and <h2> headings, paragraphs and line breaks):

Note: You should use an <h1> heading for the main heading and <h2> for the rest of the headings. Also keep in mind that you should not use <br /> to create margins between each address; as before, use paragraphs to enclose each address, and line breaks for each line.
ADDRESSES

Mozilla

331 E. Evelyn Avenue
Mountain View, CA
94041
USA

...

The <pre> element

The <pre> element represents preformatted text which is to be presented exactly as written in the HTML file. The text is typically rendered using a non-proportional (“monospace”) font. Whitespace inside this element is displayed as written:

<pre>
 ----------------------------
< I'm an expert in my field. >
 ----------------------------
         \   ^__^ 
          \  (oo)\_______
             (__)\       )\/\
                 ||----w |
                 ||     ||
</pre>

Proposed exercise: ASCII art

Create a document to display some images drawn using characters, similar to the example provided:

Note: You may find some examples at the following links: https://www.asciiart.eu/ , https://en.wikipedia.org/wiki/ASCII_art , https://asciiart.website/ , https://fsymbols.com/text-art/ , etc.
 ----------------------------
< I'm an expert in my field. > 
 ----------------------------
        \   ^__^
         \  (oo)\_______ 
            (__)\       )\/\ 
                ||----w | 
                ||     ||

Quiz

Test your skills with this quiz about paragraphs, headings, and some other concepts related to this unit.