Datatypes of HTML; why HTML is great for structure

HTML is often bashed by people for being a bad language. People, often used to XML, talk about the lack of good elements for marking up things like authors and dates. While I agree that HTML would need a few more tags that help with marking the meaning of things I do think HTML is good at structure. This article is a attempt to show HTML’s power by mapping how different datatypes in the common programming languages map to elements in HTML.

Arrays, Lists, Vectors

All programming languages have some way of storing a list of elements. While they are called different names in different languages and support different ways of adding elements they are very similar. HTML supports two kinds of single element lists, the ordered and the unordered list. When selecting which one of them you should use you just have to ask yourself: “Does this list make sense in another order?”. If it does it’s an unordered list, if not it’s an ordered one.


Unordered list:

  • Blue item
  • Gray item
  • Pink item
  • ...

Ordered list:

  1. Item 1
  2. Item 2
  3. Item 3
  4. ...

2D-Arrays, Datasets

Most languages also support some way of arranging your data in a two dimensional grid. The ordinary way is just letting people nest lists inside each other and get several dimensions like that. HTML has recognized the need for a simple way of marking up two-dimensional data in an easy way and therefore added the table element. Since two dimensions make the data a bit more complex to understand the HTML authors decided to add a few elements that describe what columns and rows mean. Below is a table with the essential elements for metadata added.

Table 1: Prices for various apples in Swedish stores
Kind of apple Color Price
Granny Smith Light Green 3 SEK
Golden Delicious Yellow 6 SEK
McIntosh Golden 30 SEK

Trees

Sometimes you want to have a tree of data; it might be to display a nice view of your historical ancestors or simply the probabilities of something happening. Many programming languages have decided not to include a certain type for this and instead let programmers build their own type with the help of nodes and pointers. HTML instead relies on nested lists for this purpose. The example below shows a nested navigation menu in two levels:

  • Home
  • About this site
  • Sections
    • Blue Fishes
      • Light blue Fishes
      • Dark blue Fishes
    • Black Fishes
    • Red Fishes
  • Contact us

Worth a note is that lists only can contain list items, not other lists. This means that all sublist needs to be wrapped inside a list item like I have done above (the fishes are inside of Sections list item).

Hashtables, Dictionaries

Moving on we find the more advanced types of data. Quite often in programming we want to map a string to another string. This can be the case if we are building a dictionary or even if we want to store the price of different products. To me both of the above examples are cases where I would use a HTML definition list. Definition lists also has a way to mark that certain items might map to several other. By setting two dts after each other you make them both apply to the following dd.

I’ve made an example below of how to markup a list of properties.

Weight:
45 kg
Length:
12 m
Colors:
Blue
Red
White
Cost:
500 SEK

Definition lists where originally made to just store definitions (as the name suggests) but it’s my opinion that they can be used for more than that. HTML lacks a lot of semantics and this is a way to extend it a bit. Feel free to leave a comment if you disagree.

 

It’s my opinion that HTML is good at marking up structure. Sure there are some types of data that can’t be represented with HTML but the most common types are easy to do. Do you have more types to add to the list? Leave a comment!

4 responses to “Datatypes of HTML; why HTML is great for structure

  1. Good points! And of course, there are the basic, but important, heading tags (H1, H2, H3). And the blockquote and cite tags, when used correctly, that is.

  2. @Greg Reimer: Interesting article! I think the kind of thinking we both apply works great to programmers. We need to remember the rest too though.

    @Dennis: Agreed, but those does not have an equivalent in programming other than perhaps ordinary variables. They are of course very important to remember.

Comments are closed.