HTML Basics

Info: This course is based on the HTML specification and can differ from older specifications like XHTML and HTML

HTML is an XML subset. This means it is composed out of tags which can contain attributes.

Tags

An HTML-tag indicator starts with < and ends with >, for example: <body>.

There are two types of HTML-tags:

  • Non self-enclosing tags
  • Self-enclosing tags

Non self-enclosing tags

Non self-enclosing tags exist out of two parts:

  1. An opening part: <tag>
  2. And a closing part: </tag>. The closing part is identified by the forward slash (/) before the tag-name.

These opening and closing tags can contain plain text and/or additional HTML markup.

<tag> {{content}} </tag>`

Example: <strong>Bold Font</strong> (This tag formats its content in a bold font: Bold Font)

The whole (start + content + end) is an HTML element.

Self-enclosing tags

A self-enclosing tag has no content. So the closing part is left of:

<tag>

Example: <br> (this will insert a newline into your HTML)

Sometimes you may see self-closing tags used like <tag />, this trailing tag is optional since HTML5 and can be left of.

Attributes

Attributes modify the behaviour of a tag.

For example the a-tag converts a piece of text into a clickable link.

<a>My text to click</a>

The href-attribute defines where the link should point to:

<a href="http://go-here-when-clicked.com">My text to click</a>

Attributes are also used to modify the appearance of a tag. Later in this course we’ll see more detailed examples of this.

A valid HTML document

A valid HTML5 document requires a bit of boilerplate:

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Your webpages metadata -->
</head>
<body>
    <!-- your webpage specific content -->
</body>
</html>

This minimal markup tells the browser to treat the document as a HTML5 document.

The document head

The head-tag allows the developer to define meta-data about the webpage. It is a wrapper around multiple other tags.

<head>
    <!-- meta tags here -->
</head>

The head-tag may only be defined once in the complete document.

Everything defined within the head element will not be visible in the HTML document. The head content can have an effect on the appearance of the page but it’s content will not be visualised.

Title

The title tag sets the web-page title. This title is displayed by the browser in the browser-tab.

<head>
    <title>My web-page's title...</title>
</head>

Style

We will address styling later in this course but for now it is sufficient to know that style information should be included in the head of a web-page.

Raw style

The style-tag allows to include raw CSS rules in the documents

<head>
    <style type="text/css">
        /* style information here */
    </style>
</head>

External style sheets

The link-tag allows to external style sheets into the document. (Do not confuse this tag with the a-tag…).

<head>
    <link href="/link/to/file.css" type="text/css" rel="stylesheet">
</head>

We will only ever include CSS-files to style our web-pages. The provided attributes in the example are required to include a CSS-file and avoid browser quirks.

The document body

The body-tag should wrap all the content to be displayed.

<body>
    <!-- all displayed tags and content go here -->
</body>

Exercise:

  • Create a valid HTML-document with
    • Document title: Hello World
    • Content: Hello World from the my first web-page

HTML: Hello World

Create a text file name hello-world.html

gedit hello-world.html

With contents:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Hello World</title>
</head>
<body>
    Hello World from my first web-page.
</body>
</html>

Open the local HTML file in the browser.

firefox hello-world.html

Headers: H*

The purpose of a header is to indicate the start of a new block and add an appropriate heading.

The hn-tags come in 6 variations. From to highest order header h1 to the lowest h6.

The browser auto-formats these headers accordingly from largest to smallest font-size.

html-intro/headers | src

<h1>Header 1</h1>
<h2>Header 2</h2>
<h3>Header 3</h3>
<h4>Header 4</h4>
<h5>Header 5</h5>
<h6>Header 6</h6>
html-intro/headers

Exercise:

  • Create a header for each Hn-tag

Containers

The purpose of these types of tags is to wrap other content. Why the content should be wrapped can vary:

  • To indicate semantic meaning (new paragraph, a quote, …)
  • To position and/or style the contents in the container.

They are also referred to as block-elements

Paragraphs p

The p-tag encloses a blob of related text into a paragraph

html-intro/p-tag | src

Content before...
<p>
   Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
   eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
   voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita
   kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</p>
Content after...
html-intro/p-tag

Generic container div

The div defines a division in the document. It is used a lot to wrap some content and apply styles.

It has no special styles by default

html-intro/div-tag | src

Content before...
<div>
   Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
   eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
   voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita
   kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</div>
Content after...
html-intro/div-tag

Pre-formatted text: pre

The pre keeps all white-space in the element (in contrast to all the other elements). The text is also displayed in a monospaced font.

html-intro/pre-tag | src

Content before...
<pre>
   Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
   eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
   voluptua.

   At vero eos et accusam et justo duo dolores et ea rebum. Stet clita
   kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</pre>
Content after...
html-intro/pre-tag

Blockquote blockquote

The blockquote-tag is used to denote some block of text as a quote from another source.

html-intro/blockquote-tag | src

Content before...
<blockquote>
   Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
   eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
   voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita
   kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</blockquote>
Content after...
html-intro/blockquote-tag

Horizontal line: hr

The hr isn’t really a container, it can’t contain other elements, but it is a block element. The tag inserts a line into the document. This can be used to split / separate sections.

html-intro/hr-tag | src

Content before...
<hr>
Content after...
html-intro/hr-tag

Exercise:

  • Create a block of text an wrap it in.
    • No tags
    • div tags
    • p tags
    • blockquote tags
    And notice the difference

Inline tags

These tags are inline because they do not start a new block (identified by new lines) as the previous tags.

Their purpose is either to give a specific style and semantic meaning to an element or to extend a certain functionality to the element.

The a is used to link to other web-pages.

In order the function, the href-attribute is required on the a-element.

html-intro/a-tag | src

<a href="http://google.com">Link to google</a>
html-intro/a-tag

Exercise:

  • Create links to
    • google.com
    • howest.be
    • php.net
    • github.com

A newline: br

The br insert a newline into the document.

html-intro/br-tag | src

This is the first line.
This is the second line, but in html all white space is replaced by a single
space...<br> The "br"-tag however instructs the browser to continue on a new
line...<br><br>Cool right?
html-intro/br-tag

Inline preformat: code

The code-tag is to inline elements what pre is to block elements. It will preserve white space and add a monospaced font in an inline way.

html-intro/code-tag | src

This is <code>preformatted text</code> inline...
html-intro/code-tag

Emphasise text: em

The em-tag allows to emphasise certain text.

html-intro/em-tag | src

This is <em>emphasised</em> inline...
html-intro/em-tag

Small text: small

The small-tag indicates the browser to use a smaller font-size to visualise this content.

html-intro/small-tag | src

This is <small>small</small> inline...
html-intro/small-tag

Inline wrap text: span

The span-tag is a generic wrapper. It has no special semantic meaning. The span-tag is mostly used to style/target some text.

html-intro/span-tag | src

This is <span>span</span> inline...
html-intro/span-tag

Strike text: strike

The strike-tag is used to strike through some text.

html-intro/strike-tag | src

This is <strike>strike</strike> inline...
html-intro/strike-tag

Bold text: strong

The strong-tag can be used to make text bold.

html-intro/strong-tag | src

This is <strong>strong</strong> inline...
html-intro/strong-tag

Inline quote text: q

The q-tag is used to indicate certain inline text was quoted from an external source.

html-intro/quote-tag | src

This is <q>quote</q> inline...
html-intro/quote-tag

Image img

The img-tag can be used to inert images into the markup. The src-attribute is required and specifies the location of the image. The location can be a local path, this means a path form the current script directory to the image location or a full (absolute) URL path.

html-intro/img-tag | src

<img src="/assets/img/php-elephant.png" alt="A local location" height="100px">

<img
   src="https://raw.githubusercontent.com/asoete/howest-webtechnology/master/docs/assets/img/php-elephant.png"
   alt="A complete URL to an image"
   height="100px">
html-intro/img-tag

Exercise:

  • Make a web-page with links to:

    • google.com
    • howest.be
    • GitHub.com
  • Print the following text so the sentences are broken up as below.

    HTML is a markup language browser understand to format documents.
    CSS is a way to style this markup.
    PHP is a programming language.
    It is used to dynamically generate HTML-markup.
  • Print the following text so hello world is emphasised.

    Let’s emphasise hello world in this sentence.

  • Print the following text so hello world is smaller

    Let’s make hello world smaller in this sentence.

  • Print the following text so hello world is bold

    Let’s make hello world bold in this sentence.

  • Print the following text so hello world is crossed off.

    Let’s strike hello world in this sentence.

Multi element markup

Some elements don’t make any sense on their own. They should be part of a larger elements-group.

Fieldset

fieldset is a container with an (optional) header (legend ).

html-intro/fieldset | src

<fieldset>
   <legend>This is a header / legend </legend>
   This is the contents of the fieldset container.
</fieldset>
html-intro/fieldset

This container is often used to group related from items together.

Lists

A HTML-list is composed of li-tags enclosed by an ul or ol-tag.

Unordered lists ul

html-intro/list-unordered | src

<ul>
   <li>list item 1</li>
   <li>list item 2</li>
   <li>list item 3</li>
</ul>
html-intro/list-unordered

Ordered lists ol

html-intro/list-ordered | src

<ol>
   <li>list item 1</li>
   <li>list item 2</li>
   <li>list item 3</li>
</ol>
html-intro/list-ordered

Exercise:

  • Make an unordered list with your name, age and gender as items
  • Make your name bold, age emphasised and gender quoted.
  • Make a top 3 ranked list of your favorite dishes
  • Add a fourth dish, but with smaller font .

Tables

A simple table is composed out of:

  • a table wrapper: table
  • rows: tr
  • header cells th
  • and normal cells td

html-intro/simple-table | src

<table>
   <tr>
      <th>Firstname</th>
      <th>Lastname</th>
      <th>Age</th>
   </tr>
   <tr>
      <td>John</td>
      <td>Doe</td>
      <td>21</td>
   </tr>
   <tr>
      <td>Jane</td>
      <td>Doe</td>
      <td>18</td>
   </tr>

</table>
html-intro/simple-table

Exercise:

  • Make a table with two columns: name and score
  • Add 3 rows
    • Jan -> 12
    • Piet -> 15
    • Boris -> 7
  • Make the names also headers
  • Add a column ‘passes’ and add a V if the number is larger than 10 and an X otherwise.

Attributes

As already seen with the a-tag, attributes can modify the behaviour of an HTML-element.

The a-tag requires the href-attribute to be set. Otherwise the browser has no clue where to take the user on a click.

The attributes are also often used to modify the appearance of an element.

Commonly used attributes:

Class

The class attribute holds a space separated list class-names. The element is member of all the classes specified in the attribute. These classes can be used to style a group of elements the same way.

For example all the elements which are member of the same class (have the same class-name in the class attribute) should have the text colour set to red…

<p class="class1" >...</p>
<p class="class1 class-two" >...</p>

Id

The id-attribute lets you assign a unique identifier to an element.

This identifier should be unique for the whole page and thus occur only once.

<p id="unique-identifier" >...</p>

If the id is specified in the URL prefixed by a pound symbol (#), the element will be automatically scrolled into view.

<!-- http://WWW.example.com/script.php#chapter -->

<p id="chapter1">
Scroll into view...
</p>

Style

The style attribute can be used to apply CSS-rules to a single element.

Generally speaking you should not set the styles via this tag. A dedicated style block in the head of page or an external style sheet are better, more scalable, options. It can however come in handy in this introduction to HTML and CSS.

<p style="background: red; color: green;">...</p>

See HTML and CSS for more info about styling an element.

Title

The title attribute lets you assign a title to an element. This title will be displayed as a tooltip when hovering with the mouse over this element.

html-intro/title-attribute | src

<p title="Some title for this element">Hover over me...</p>
html-intro/title-attribute

Exercises

Exercise:

Create a HTML page resembling your CV.

Example Solution:

Exercise:

Add a table of contents (TOC) to the previous page (cv.html).

The TOC should contain all headers, clicking a header should take the user to this header/section

Example Solution:

Exercise:

Re-create the HTML skeleton of a wiki page: https://en.wikipedia.org/wiki/FASTA_format

Example Solution: