Jump to main navigation


Tutorial 17.2 - HTML

17 Nov 2018

About

Hyper Text Markup Language (HTML) is the main markup language used for creating pages and information on web browsers. A markup language is a language that combines plain text with some form of syntactically distinguishable tags that define how the regular text elements should be formatted when displayed in a suitable viewer (such as a web browser).

In HTML, most tags are paired into opening (<> and closing ( tags that specify the form of formatting to apply to all the text bounded by the tag pair. There are also empty tags that are unpaired and typically indicate the location of a specific action.

Not only does HTML define the format of text, it also defines the presentation semantics - the overall layout and structure of the document. Furthermore, HTML documents often embed other media and link to other resources as well as other languages (such as Cascading Style Sheets and Javascript) that provide additional and dynamic structural formatting. Collectively, all the marked up content and formatting information are interpreted and rendered by a redering or layout engine.

Since HTML documents are just text files, they can be created in any text editor. Thereafter, they are viewed in a browser. A browser is an application that understands the HTML format and is able to retrieve and render documents and other resources over the Internet as well as local file systems. As of the start of 2013, the major desktop browsers (in order of usage share) where:

  1. Google Chrome
  2. Microsoft Internet Explorer
  3. Mozilla Firefox
  4. Safari
  5. Opera
There are also numerous mobile browsers (those specially designed to run on portable devices such as smart phones and tablets), most of which tend to be restricted to a particular platform.

Whilst HTML is generally governed by certain standards (W3C) that have been established to attempt to ensure compatibility between browsers, such is the popularity of the world wide web (for anything from content consumption, goods and services trading, banking as well as cloud and collaborative computing), browsers compete to be the first to introduce new, more enabling capabilities. Often new capabilities are developed in parallel, thereby resulting in different HTML dialects and associated layout engines and thus incompatibilities.

FeatureGoogle ChromeMicrosoft IEMozilla FirefoxSafariOpera
Layout engineWebkitTridentGeckoWebkitPresto
Operating systemWindows, OSX, LinuxWindowsWindows, OSX, LinuxOSXWindows, OSX, Linux
Web inspector/development toolsYesYesYesYesNo

Document structure

In its simplest form, a HTML document comprises of a head (in which the overall structure and formatting rules are defined) and the body (containing the marked up content). Although HTML documents are plain text files (*.html), they do have a specific structure that must be adhered to. A minimum example is helpful at this point.

Simple example

Copy (and paste) the text (code) in the left hand box below into a text editor. Save the plain text file with an extension of .html and then open this file with a web browser. The result should be similar to that displayed in the right hand bo
HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> <p>The text of the document goes here.</p> </body> </html>
Simple document

The text of the document goes here.

Stepping through the above HTML code line by line:
  1. The first line indicates that the document is a html document. Typically, this line will also include information above what version of HTML to use. For example, <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> would indicate HTML version 4.01
  2. The full HTML document starts with the tag <html> and ends with </html>. Within the <html></html> tags, there are two regions (the head and the body)
  3. The <head></head> tags define the document preamble or header. Text and code in this section are not rendered directly on the screen page - that is, the header is not for document content. Rather, this region is for defining styling rules and linking to other scripts.
  4. The <title></title> tags define a document title. This title is used as document meta data. The title will be displayed on the title bar of the browser rather than being rendered within the screen document (as it is in the header), instead it
  5. The <body></body> tags define the content of the document. Text within the body is usually rendered within the screen document.
  6. The <p></p> tags define a paragraph.

Most tags are in pairs and the tag pairs define the start and end of an environment. End tags are distinguished from start tags by the presence of a / before the tag name.

A HTML tag or tag pair defines an element. In the case of the above example, the <p> pair define a paragraph element.

More about tags and elements

Almost all HTML elements (the entities established by a tag pair and the enclosed content) have attributes. Attributes of elements modify the meaning or context (such as identity) of the element.

Some attributes are appropriate for all elements (these are called global attributes). A table of the most common of these global attributes follows. Other attributes are only applicable to specific elements and will thus be covered in association with the relevant elements.

AttributeDescription
classIndicates the name of a element class for Cascading Style Sheets. Classes are types of elements that share certain characteristics or behaviours.
idIndicates a unique identity for the element. This allows the element to be referred to by other elements.
styleDefines the elements in-line style.

Simple markup

Ordinary text and punctuation

HTML has few (if any) built in grammatical and punctuation rules. In the absence of any user defined markup, plain text will be presented as is. That is, a continuous stream of words.

Carriage returns (line feeds) are ignored by the browser, as are multiple spaces. Such formatting features are achieved via special tags (such as the <br> tag) and entity characters (such as the non-breaking space character, &nbsp;).

Consider the following HTML code and associated result.
HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> This is a rather short sentence. This sentence starts straight after the previous despite carriage returns in the source. A carriage<br>return can be forced<br> however.<br><br> The following three spaces are ignored, however these three&nbsp;&nbsp;&nbsp;entity characters are not. </body> </html>
Simple document This is a rather short sentence. This sentence starts straight after the previous despite carriage returns in the source. A carriage
return can be forced
however. The following three spaces are ignored, however these three   entity characters are not.

To arrange text into paragraphs, it is best to surround all the sentences of a paragraph between a pair of paragraph tags (<p>). The paragraph tags automatically apply extra line spacing above and below the first and last sentence, thereby separating the paragraphs visually.

HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc varius, quam id ullamcorper feugiat, est dui accumsan justo, id pharetra tortor lectus in ante. Morbi molestie sem vel felis accumsan sed convallis libero tempus. Pellentesque a velit ut nunc convallis cursus. Curabitur feugiat, urna eu ultricies tristique, risus orci euismod +\n isi, et sodales nulla neque eget nunc. </p> <p>Fusce interdum lectus eget leo egestas eget consectetur leo iaculis. Aenean egestas dignissim rutrum. Donec sit amet libero est, dapibus vehicula lorem. Nunc varius feugiat enim sed vestibulum. Donec ultrices cursus blandit. </p> </body> </html>
Simple document

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc varius, quam id ullamcorper feugiat, est dui accumsan justo, id pharetra tortor lectus in ante. Morbi molestie sem vel felis accumsan sed convallis libero tempus. Pellentesque a velit ut nunc convallis cursus. Curabitur feugiat, urna eu ultricies tristique, risus orci euismod +\n isi, et sodales nulla neque eget nunc.

Fusce interdum lectus eget leo egestas eget consectetur leo iaculis. Aenean egestas dignissim rutrum. Donec sit amet libero est, dapibus vehicula lorem. Nunc varius feugiat enim sed vestibulum. Donec ultrices cursus blandit.

Paragraph justification

You might have noticed that the paragraphs are left justified within the browser. This is the default behaviour, yet can be altered. Most tags have attributes that can be set to alter some aspect of the tag's behaviour.

Prior to HTML5 (version 5 of the HTML language), text alignment could be modified via the align attribute. This attributes could be set to either left, right, center or justify. For example.

HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> <p align='justify'>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc varius, quam id ullamcorper feugiat, est dui accumsan justo, id pharetra tortor lectus in ante. Morbi molestie sem vel felis accumsan sed convallis libero tempus. Pellentesque a velit ut nunc convallis cursus. Curabitur feugiat, urna eu ultricies tristique, risus orci euismod nisi, et sodales nulla neque eget nunc. </p> <p align='justify'>Fusce interdum lectus eget leo egestas eget consectetur leo iaculis. Aenean egestas dignissim rutrum. Donec sit amet libero est, dapibus vehicula lorem. Nunc varius feugiat enim sed vestibulum. Donec ultrices cursus blandit. </p> </body> </html>
Simple document

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc varius, quam id ullamcorper feugiat, est dui accumsan justo, id pharetra tortor lectus in ante. Morbi molestie sem vel felis accumsan sed convallis libero tempus. Pellentesque a velit ut nunc convallis cursus. Curabitur feugiat, urna eu ultricies tristique, risus orci euismod +\n isi, et sodales nulla neque eget nunc.

Fusce interdum lectus eget leo egestas eget consectetur leo iaculis. Aenean egestas dignissim rutrum. Donec sit amet libero est, dapibus vehicula lorem. Nunc varius feugiat enim sed vestibulum. Donec ultrices cursus blandit.

A more modern and flexible approach to making such a style change involves engaging the element's style attribute. We will return to styles in the Cascading Style Sheets section.

HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> <p style='text-align:justify;'>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc varius, quam id ullamcorper feugiat, est dui accumsan justo, id pharetra tortor lectus in ante. Morbi molestie sem vel felis accumsan sed convallis libero tempus. Pellentesque a velit ut nunc convallis cursus. Curabitur feugiat, urna eu ultricies tristique, risus orci euismod nisi, et sodales nulla neque eget nunc. </p> <p style='text-align:justify;'>Fusce interdum lectus eget leo egestas eget consectetur leo iaculis. Aenean egestas dignissim rutrum. Donec sit amet libero est, dapibus vehicula lorem. Nunc varius feugiat enim sed vestibulum. Donec ultrices cursus blandit. </p> </body> </html>
Simple document

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc varius, quam id ullamcorper feugiat, est dui accumsan justo, id pharetra tortor lectus in ante. Morbi molestie sem vel felis accumsan sed convallis libero tempus. Pellentesque a velit ut nunc convallis cursus. Curabitur feugiat, urna eu ultricies tristique, risus orci euismod nisi, et sodales nulla neque eget nunc.

Fusce interdum lectus eget leo egestas eget consectetur leo iaculis. Aenean egestas dignissim rutrum. Donec sit amet libero est, dapibus vehicula lorem. Nunc varius feugiat enim sed vestibulum. Donec ultrices cursus blandit.

Text formatting

Font styles and sizes

Brief changes to font styles is within a block of text can be effective at emphasizing or applying different meanings to characters. Common text modifier styles are: italic, bold, strikethrough and underline.

Similarly, different font families are also used to imply different meaning. For example, courier font, roman or serif font, sans serif font.

HTML has an element called <em> that emphasizes (usually italics) the text within the curly braces. Further control of font styles is provided via either Cascading Style Sheets or the depreciated <font> tag as described in the following table. There are also a couple of other common styles that have dedicated tags.

Again, the scope of pure HTML font modifiers is rather limited. Cascading Style Sheets offer a much more flexible means to alter text formatting. Consequently many of the tags featured in the following tables have been are depreciated in HTML 4.01 and not defined at all within HTML5. Universally supported tags are indicated by a *.

TagFont typeHTML5 Support
<i>..</i>italic font*
<em>..</em>emphasized font*
<b>..</b>bold font*
<u>..</u>underlined font*
<strike>..</strike>strikethrough font*
<font face="roman">..</font>roman or serif font. This is the default font.
<font face="sans-serif">..</font>sans serif font. This is an Arial or Helvetica like font.
<font face="courier">..</font>courier or typewriter font. This is typically used to illustrate code

The above elements can also be nested within one another for example to produce bold, italic text (<b><i>bold, italic text</b></i>).

There are a (depreciated) couple of tags for altering font sizes (and positions) in aesthetic increments.

TagFont sizeHTML5 Support
<small>..</small>small font
<big>..</big>big font
<sub>..</sub>subscript font*
<sup>..</sup>superscript font*

Special characters

The following characters within HTML have special meaning (to define the markup) and thus do not render literally.
& < >			
  
These characters must instead by included via a HTML character reference. There are also HTML character references for other non-standard characters. Alternatively, characters can be indicated via their Unicode number codes. The following table provides a small selection of the most common characters.
HTML character referenceUnicode numberResult
&amp;&#38;&
&lt;&#60;<
&gt;&#62;>
The following table illustrates ligature support with HTML reference characters and Unicode numbers.
HTML character reference(s)Unicode number(s)Result(s)
&Agrave; &Aacute; &Acirc; &#192; &#193; &#194; À Á Â
&Atilde; &Auml; &Aring; &#195; &#196; &#197; Ã Ä Å
&AElig; &#198; Æ
&Ccedil; &#199; Ç
&Egrave; &Eacute; &Ecirc; &Euml; &#200; &#201; &#202; &#203; È É Ê Ë
&Igrave; &Iacute; &Icirc; &Iuml; &#204; &#205; &#206; &#207; Ì Í Î Ï
&ETH; &#208; Ð
&Ntilde; &#209; Ñ
&Ograve; &Oacute; &Ocirc; &#210; &#211; &#212; Ò Ó Ô
&Otilde; &Ouml; &Oslash; &#213; &#214; &#216; Õ Ö Ø
&Ugrave; &Uacute; &Ucirc; &Uuml; &#217; &#218; &#219; &#220; Ù Ú Û Ü
&Yacute; &#221; Ý
&THORN; &#222; Þ
&szlig; &#223; ß
&agrave; &aacute; &acirc; &#224; &#225; &#226; à á â
&atilde; &auml; &aring; &#227; &#228; &#229; ã ä å
&aelig; &#230; æ
&ccedil; &#231; ç
&egrave; &eacute; &ecirc; &euml; &#232; &#233; &#234; &#235; è é ê ë
&igrave; &iacute; &icirc; &iuml; &#236; &#237; &#238; &#239; ì í î ï
&eth; &#240; ð
&ntilde; &#241; ñ
&ograve; &oacute; &ocirc; &#242; &#243; &#244; ò ó ô
&otilde; &ouml; &oslash; &#245; &#246; &#248; õ ö ø
&ugrave; &uacute; &ucirc; &uuml; &#249; &#250; &#251; &#252; ù ú û ü
&yacute; &yuml; &#253; &#255; ý ÿ
&thorn; &#256; þ

Headings

There are six hierarchical levels of headings defined in HTML. These, numbered h1...h6 represent headings, subheadings, subsubheadings etc. The default styles (font and sizes) of these headings have been chosen to reflect the hierarchy of the headings.
HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> <h1>Main heading</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc varius, quam id ullamcorper feugiat, est dui accumsan justo, id pharetra tortor lectus in ante. Morbi molestie sem vel felis accumsan sed convallis libero tempus. Pellentesque a velit ut nunc convallis cursus. Curabitur feugiat, urna eu ultricies tristique, risus orci euismod nisi, et sodales nulla neque eget nunc. </p> <h2>Sub heading</h2> <p> Fusce interdum lectus eget leo egestas eget consectetur leo iaculis. Aenean egestas dignissim rutrum. Donec sit amet libero est, dapibus vehicula lorem. </p> <h3>Sub subheading</h3> <p>Nunc varius feugiat enim sed vestibulum. Donec ultrices cursus blandit. </p> </body> </html>
Simple document

Main heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc varius, quam id ullamcorper feugiat, est dui accumsan justo, id pharetra tortor lectus in ante. Morbi molestie sem vel felis accumsan sed convallis libero tempus. Pellentesque a velit ut nunc convallis cursus. Curabitur feugiat, urna eu ultricies tristique, risus orci euismod nisi, et sodales nulla neque eget nunc.

Sub heading

Fusce interdum lectus eget leo egestas eget consectetur leo iaculis. Aenean egestas dignissim rutrum. Donec sit amet libero est, dapibus vehicula lorem.

Sub subheading

Nunc varius feugiat enim sed vestibulum. Donec ultrices cursus blandit.

Comments

Within HTML, comments (blocks of text intended only as notes for the developer) are included by placing the comment between <!-- and --> tags. For example:
HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> <p> Fusce interdum lectus eget leo egestas eget consectetur leo iaculis. Aenean egestas dignissim rutrum. Donec sit amet libero est, dapibus vehicula lorem. <!-- this is a comment, note how it does not break the flow of text rendered in the screen document) --> Nunc varius feugiat enim sed vestibulum. Donec ultrices cursus blandit. </p> </body> </html>
Simple document

Fusce interdum lectus eget leo egestas eget consectetur leo iaculis. Aenean egestas dignissim rutrum. Donec sit amet libero est, dapibus vehicula lorem. Nunc varius feugiat enim sed vestibulum. Donec ultrices cursus blandit.

Cascading Style Sheets (css)

apply multiple styling rules to a single tag the formatting tags illustrated earlier are now discouraged as they fix the presentation rules with the content. This impedes flexibility. Cascading Style Sheets, allow styling rules to be defined in a separate layer (in the head or an external file), so that styles can be altered and reflected by changes throughout the entire document. Decoupling the content from the presentation rules allows for a change in presentation styles without requiring any changes to the content markup. This is in the true spirit of markup - content should be authored with very minimal styling. The styling rules are defined separately and therefore can be altered independently.

Lists

There are three basic list elements available within HTML:
  • Ordered lists (<ol>...</ol>) for numbered lists
  • Un-ordered lists (<ul>...</ul>) for un-numbered, bullet lists
  • Definition lists (<dl>...</dl>) for descriptive or definition lists
Within ordered and un-ordered lists, the individual list items are placed within <li>...</li> tag pairs
HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> <ol> <li>This is a numbered list</li> <li>Note how numbering is automatic</li> <li>There is meaning in the order of the points</li> </ol> <ul> <li>This is a bulletted list</li> <li>The order of these points is not important</li> <li>Last point</li> </ul> </body> </html>
Simple document
  1. This is a numbered list
  2. Note how numbering is automatic
  3. There is meaning in the order of the points
  • This is a bulletted list
  • The order of these points is not important
  • Last point

Nested lists

Lists can be nested within lists. That is, a list item can be another list, in which case, the new items (sub-items) are indented.
HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> <ol> <li>This is a numbered list</li> <li>This item has sub-items <ul> <li>the sub-items are bulletted items</li> <li>or could be another number list</li> </ul> </li> <li>There is meaning in the order of the points</li> </ol> <br/> <ol> <li>This is another numbered list</li> <li>This item has sub-items <ol> <li>the sub-items are another number list</li> <li>notice how the counter is reset</li> </ol> </li> <li>There is meaning in the order of the points</li> </ol> </body> </html>
Simple document
  1. This is a numbered list
  2. This item has sub-items
    • the sub-items are bulletted items
    • or could be another number list
  3. There is meaning in the order of the points

  1. This is another numbered list
  2. This item has sub-items
    1. the sub-items are another number list
    2. notice how the counter is reset
  3. There is meaning in the order of the points

Definition lists

Definition or description lists display a set of emphasized (bold) words or small phrase along with their associated descriptions (indented on a new line).

The list itself is bounded by a pair of <dl>...<\dl> tags.

The terms are enclosed in <dt>...<\dt> tags and the definitions enclosed in <dd>...<\dd> tags.
HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> <dd> <dt>Term 1</dt> <dd>a definition of Term 1</dd> <dt>A phrase</dt> <dd>a description of the phrase</dd> </dd> </body> </html>
Simple document
Term 1
a definition of Term 1
A phrase
a description of the phrase

Graphics

Modern browsers are able to display a large number of graphical image types:
  • compressed bitmap images - which store images as a pixel array - the most popular of which are JPEG, GIF and PNG. These are best for photographs.
  • scalable vector graphics (SNG) - which represent images as a series of coordinates and mathematically describable shapes. These are best for diagrams and text (particularly if they require resizing).

Images

Bitmapped images are included via the <img> tag. This tag is unusual in that it is not paired. Instead of operating on the plain text content between a pair of tags, the <img> tag provides a place holder for an image (resource) as well as a link to where the resource is located.

Whilst images can be hard-coded into the HTML document, more typically, the document mealy provides a link (via the src= parameter) to where the image (resource) is located. The browser takes care of retrieving the resource, determining the type of resource (and thus graphics driver) and renders the resource according to the location of the image place marker.

HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> The image <img src="http://www.flutterbys.com.au/stats/images/Chrome.png"> is placed exactly where the image tag appeared in text. As no sizing information is provided, the image will be rendered according to its natural dimensions. </body> </html>
Simple document The image is placed exactly where the image tag appeared in text. As no sizing information is provided, the image will be rendered according to its natural dimensions.

By default the image is placed inline (as if it was a text character, albeit a large one) with the bottom of the image flush with the baseline of the surrounding text.

There are numerous other parameters that can be used to control various aspects of the dimensions and positioning of an image, although as with other elements, the best control is via Cascading Style Sheets.

Nevertheless, there are two important parameters (in addition to src). These are:

  • alt - this parameter defines an alternative (text only) description of the image for visually impaired persons as well as text-only browsers.
  • title

Tables

A table element is of course used to present tabular information. Within a table element (<table>...</table>) there are rows (<tr>...</tr>), within the rows are cells (<td>...</td>), and within cells are the content.

Tables can appear anywhere and can even be nested within other tables. Furthermore, tables can also be used to arrange/align content within the HTML document (although yet again, CSS is a more sophisticated solution)..

HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> <table> <tr> <td>Column A</td><td>Column B</td><td>Column C</td> </tr> <tr> <td>Category 1</td><td>High</td><td>100.00</td> </tr> <tr> <td>Category 2</td><td>High</td><td>80.50</td> </tr> </table> </body> </html>
Simple document
Column AColumn BColumn C
Category 1High100.00
Category 2High80.50

Table column headings

Often it is useful to be able to clearly distinguish the first row as headings. The <th>...</th> tag pair can be used instead of <td>...</td> tags and the enclosed content is centered and emphasized with bold font.
HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> <table> <tr> <th>Column A</th><th>Column B</th><th>Column C</th> </tr> <tr> <td>Category 1</td><td>High</td><td>100.00</td> </tr> <tr> <td>Category 2</td><td>High</td><td>80.50</td> </tr> </table> </body> </html>
Simple document
Column AColumn BColumn C
Category 1High100.00
Category 2High80.50

Table caption

Table captions are used to provide a description of the table content. Strictly, the caption should appear above the table and contain sufficient detail to allow the table and its caption to "stand alone" from the rest of the document.

Sensibly enough, caption elements are supported via the <caption>..</caption> tag pair. Unfortunately however, it does not automatically prepend the caption with a prefix and incrementing counter (such as Table 1:).

HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> <table> <caption>This is a description of the table.</caption> <tr> <th>Column A</th><th>Column B</th><th>Column C</th> </tr> <tr> <td>Category 1</td><td>High</td><td>100.00</td> </tr> <tr> <td>Category 2</td><td>High</td><td>80.50</td> </tr> </table> </body> </html>
Simple document
This is a description of the table.
Column AColumn BColumn C
Category 1High100.00
Category 2High80.50

Cell padding and cell spacing

Cell padding and cell spacing define the amount of space within a cell and between cells respectively. By default, they are both set to 2px, yet cab be altered with the cellpadding= and cellspacing= parameters.

  • When the cellpadding= and/or cellspacing= parameter(s) are applied to the <table> element, all cells of the table are affected.
  • When the cellpadding= and/or cellspacing= parameter(s) are applied to a <tr> (row) element, just the cells in that row are affected.
  • When the cellpadding= and/or cellspacing= parameter(s) are applied to a <td> or <th> (cell) element, only this cell will be affected.
HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> <table cellspacing=0 cellpadding=0> <caption>This is a description of the table.</caption> <tr> <th>Column A</th><th>Column B</th><th>Column C</th> </tr> <tr> <td>Category 1</td><td>High</td><td>100.00</td> </tr> <tr> <td>Category 2</td><td>High</td><td>80.50</td> </tr> </table> </body> </html>
Simple document
This is a description of the table.
Column AColumn BColumn C
Category 1High100.00
Category 2High80.50

Content alignment

By default, cell contents are horizontally aligned to the left and vertically aligned in the center of the cell. The horizontal and vertical alignments can be respectively altered with the align= and valign= parameters of either the table (affects all cells), row (affects all cells in the row) or cell (affect only that cell).
  • horizontal alignment can be either align="left", align="center" or align="right"
  • vertical alignment can be either valign="top", valign="middle" or valign="bottom"
HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> <table cellspacing=0 cellpadding=0> <caption>This is a description of the table.</caption> <tr> <th>Column A</th><th>Column B</th><th align="right">Column C</th> </tr> <tr> <td>Category 1</td><td>High</td><td align="right">100.00</td> </tr> <tr> <td>Category 2</td><td>High</td><td align="right">80.50</td> </tr> </table> </body> </html>
Simple document
This is a description of the table.
Column AColumn BColumn C
Category 1High100.00
Category 2High80.50

Spanning multiple rows or columns

Having a cell span multiple columns is achieved with the colspan= parameter to the cell (<td>) tag. When doing so, it is common to also adjust the text alignment such that the content is centered within the new multicolumn cell.
HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> <table cellspacing=0 cellpadding=0> <caption>This is a description of the table.</caption> <tr> <th></th><th colspan=2 align="center">Multi</th> </tr> <tr> <th>Column A</th><th>Column B</th><th>Column C</th> </tr> <tr> <td>Category 1</td><td>High</td><td>100.00</td> </tr> <tr> <td>Category 2</td><td>High</td><td>80.50</td> </tr> </table> </body> </html>
Simple document
This is a description of the table.
Multi
Column AColumn BColumn C
Category 1High100.00
Category 2High80.50

CSS and tables

In the following example, a new table class (called "elegantTable") is defined and CSS is used to make the following modifications from the standard table to this new table class:
  • Initially remove all cell borders, make the cellpadding equal to 2px and the border-spacing equal to 0px
  • Specify a 1px solid black line above and below the header row
  • Give the header row more padding and make the font slightly larger and bold
  • Give the data rows a bit more padding
  • Specify a 1px solid black line below the last data row
  • Specify that the contents of the second data cell in each row (= second column) should be centered
  • Specify that the contents of the last data cell in each row (= last column) should be right aligned
HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> <script type="text/css"> table.elegantTable { border:0px; padding:2px; margin:2px; border-spacing:0px; } table.elegantTable th { border: 0px; border-top: 1px solid #000000; border-bottom: 1px solid #000000; padding:3pt; display:table-header; color:black; font-size:1.1em; font-weight: bold; } table.elegantTable td { border: 0px solid #000000; padding:3px; margin:2px; } table.elegantTable tr:last-child td { border-bottom: 1px solid #000000; } table.elegantTable td+td { text-align:center; } table.elegantTable tr &gt; td:last-of-type { text-align:right; } </script> </head> <body> <table class="elegantTable"> <caption>This is a description of the table.</caption> <tr> <th>Column A</th><th>Column B</th><th>Column C</th> </tr> <tr> <td>Category 1</td><td>High</td><td>100.00</td> </tr> <tr> <td>Category 2</td><td>High</td><td>80.50</td> </tr> </table> </body> </html>
Simple document
This is a description of the table.
Column AColumn BColumn C
Category 1High100.00
Category 2High80.50

Referencing (links)

Links are a prominent feature of the Internet. Browsing is essentially a process of following hyperlinks (links) between resources. These links can point to other resources, bookmarks within the current resources (page), or other content that can be downloaded.

A hyperlink is specified via an <a>...</a> element. The href= parameter indicates the URL (Universal Resource Locator) of a resource, and the content between the <a>...</a> tag pair provides a clickable description of the resource or instruction.

The following example provides a link to the official R website.

HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> For more information on the R Graphical and Statistical Environment, go to <a href="www.r-project.org">here</a>. </body> </html>
Simple document For more information on the R Graphical and Statistical Environment, go to here.

Absolute and relative links

The above example featured an absolute link. The link provided the full path or URL of the resource. However, if the resource resides within the same directory tree as the source HTML document (either in the same directory, or a nearby directory), then the link can be a path relative to the current location.

For example to link to a resource called info.html:

  • if the linked resource is in the same directory as the source HTML, then the hyperlink reference could be href="info.html"
  • if the linked resource is in a directory called about within the same directory as the source HTML, then the hyperlink reference could be href="about/info.html"
  • if the linked resource within the parent directory of the source HTML, then the hyperlink reference could be href="../info.html"
  • if the linked resource is in a sibling directory (directory that shares a same root/parent as the current directory) called about, then the hyperlink reference could be href="../about/info.html"

Links to bookmarks within a page

The name parameter of any element provides a bookmark to which a link can refer. To link to a bookmark, the unique bookmark name is prefixed by a # character within the href= parameter string. Bookmarks are also not restricted to the same page - they can be bookmarks to specific locations within other pages.

The following example provides a section element with a name of intro, and this is later referred to with a link. Note, as this is a very small example, the link will not appear to work (since the screen does not need to move in order to ensure that the linked section is on the screen).

HTML codeResult in browser
<!DOCTYPE html> <html> <head> <title> Simple document </title> </head> <body> For more information on the R Graphical and Statistical Environment, go to <a href="www.r-project.org">here</a>. </body> </html>
Simple document

Introduction

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent a velit quis ante dignissim dignissim eget vitae tellus. Duis eget neque tellus, eu elementum leo. Nullam quis velit in magna bibendum dictum. Curabitur tincidunt cursus tellus, in egestas augue porta ut.

Methods

Phasellus facilisis porttitor elit, vel pretium felis volutpat in. Praesent euismod sagittis tortor, eget varius nisi consequat eget. Sed facilisis aliquet accumsan. Maecenas aliquam, dolor id hendrerit viverra, lacus tortor elementum nunc, quis commodo ligula orci vel augue. Suspendisse dolor purus, volutpat vel viverra vitae, laoreet blandit nulla. In eros ligula, scelerisque id tempus nec, pulvinar vitae felis. Morbi tempor viverra orci, quis elementum metus lobortis sed. Curabitur sit amet ante massa here.

Further reading