HTML
====
HTML forms the foundation on which all of the web is built. As the name states (HyperText Markup Language) it is a markup language, that allows you to annotate your content with *tags* that attach semantic or presentational roles to the content. The most important thing when creating HTML documents is to remember that the aim is to markup with roles not with specific styles. Styling is attached later through :doc:`css`.
Core Concepts
-------------
Before we get into actually creating HTML documents, we will briefly look at the two core concepts of HTML: *elements* and *attributes*.
Element
+++++++
The core element for marking up the content is the HTML element. At the basic level an element consists of three parts. The opening tag :code:`` which indicates where the content marked up with that element starts, the content of the element, and the closing tag :code:``:
.. code-block:: html
Element content
You should always use lowercase letters for the *tag names*. While not technically binding, it is the recommended convention and makes your HTML more readable for other developers.
In some cases your element will not have any content. In that case you can put the closing :code:`/` directly at the end of the opening :code:``:
.. code-block:: html
The main use of such self-closing elements is to indicate locations to include content from outside the HTML page (images, videos, stylesheets, ...), as we will see later.
.. warning::
You will sometimes find tutorials that state that in HTML5 you do not need to close your elements. While technically correct, it means that you are leaving the decision on when and where an element is closed to the browser's internal logic. This might not be consistent across browsers and is potentially liable to future change. You should therefor **not do this**. Always explicitly close your elements.
The full power of HTML comes from the ability to nest elements inside other elements as in the following example:
.. code-block:: html
Content in the tag
The important thing when nesting elements is that the elements must form a tree structure. That means that all child elements must be closed before their parent element can be closed. For example the following HTML is invalid, as the :code:`parent-tag` is closed before its :code:`child-tag`:
.. code-block:: html
Content in the tag
In practice, when the browser encounters a situation like this, it attempts to automatically fix this, by adding additional opening and closing tags, resulting in a structure that might be something like this:
.. code-block:: html
Content in the tag
Since it is unlikely that this is what you intended, you should always check that you have correctly nested and closed your elements. Use a decent code editor and that will automatically highlight any such errors.
.. note::
The label *tag* is sometimes also used as a synonym for *element*. *Tag* should really only be applied to the opening or closing :code:`` and *element* to the whole opening/content/closing block. However, you will often read things like "That is contained in a div tag", even though they mean that it is contained in a :code:`
` element. Annoying, but there it is.
Attributes
++++++++++
To provide further flexibility to the markup, elements can be further modified through the inclusion of attributes in the opening tag. Attributes are simply given as :code:`name="value"` pairs inside the opening tag:
.. code-block:: html
Each attribute name can only appear once in a tag. However, you can have multiple attributes in a single element, with the attributes separated from each other by a space:
.. code-block:: html
Attribute values are always string values. Later, when we get to interacting with the HTML through JavaScript we will have to look at transforming values into other data-types, but at the HTML level, all content is text.
Basic Structure
---------------
The basic structure of an HTML document is as follows:
.. code-block:: html
Non-content elements go in here
Displayed content goes in here
Doctype
+++++++
HTML has evolved over the years and for different version, the specifications have defined slightly different interpretations and default settings for how to render the HTML code. The :code:`` directive lets the browser know which HTML version the document is and thus which rules and interpretation to apply when rendering the content. To specify that the latest HTML5 specification is to be applied --- which you should always do --- we use :code:``.
Html
++++
Next comes the :code:`` element. An HTML document **must** only have one root element and this element **must** be the :code:`` element. The :code:`` element **must** only have two child elements, :code:`` and :code:`` in that order.
.. note::
As with all other HTML errors, the browser will never throw an error if you have multiple :code:`` elements or none at all. Instead it will simply try to fix the bugs silently and display what it thought you wanted.
Head
++++
The :code:`` element contains all non-content elements and meta-data about the HTML document. This includes elements for loading things such as stylesheets or JavaScript code, but also meta-data elements that cover aspects such as text charset, document title, authorship, or render settings.
If you place content elements in the :code:`` element, they will be displayed, because of the browser auto-fixing your code, but the HTML is technically still invalid.
Body
++++
The :code:`` element contains all content elements, irrespective of whether these are immediately visible, require loading external content, or a JavaScript content.
Meta-data Elements
------------------
:code:``
The :code:`` element is used to specify meta-data for the HTML document. The three most common uses of the :code:`` element is to specify the encoding used for the HTML content:
.. code-block:: html
to specify how the browser should render the page:
.. code-block:: html
and to specify an author for the page:
.. code-block:: html
The "viewport" setting is the most important one here. It is used to tell mobile browsers that your HTML and styling is smart enough to be able to handle different screen sizes. If you do not specify this, then mobile browsers will generally render your page on a virtual screen of approximately 1024x768 pixels and then let the user navigate that by zooming and panning around. Obviously that is not the most pleasant experience, thus by specifying the viewport as above, we let the browser know that the styling will handle different screen sizes.
Full documentation on the :code:`` element can be found here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta.
:code:``
The :code:`` element is used to specify the page's title, which most browsers display as the window and/or tab title.
.. code-block:: html
Athena - Login
:code:``
The :code:`` element is used to create links between the current document and other URLs. The most common use of this is to load in external CSS stylesheets:
.. code-block:: html
The :code:`rel` attribute is used to define the relationship between the other URL and this document. By specifying "stylesheet" as the value, the browser knows to load the URL specified in :code:`href` and then apply it as a CSS stylesheet to the current document.
There are other elements that can be used in the :code:``, see here for details: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head.
Block Elements
--------------
In the main :code:`` content, HTML elements are split into two groups: *block* elements and *inline* elements. The main distinction between the two is that *block* elements form a vertical structure. After each closing tag of a *block* element, the browser essentially adds a new-line, so that the next element appears below the current one. This is not the case for *inline* elements.
The list of elements here is not complete, but represents the most commonly used and needed elements.
Generic
+++++++
Sometimes it is necessary to group together content, primarily for styling purposes, but there is no specific HTML element that has the necessary semantics, or we explicitly do not want to attach any semantics to the block of content. In that case, the :code:`
` element creates a generic, arbitrary block:
:code:`
`
An example for the use of a :code:`
` element is the following code snippet, which attaches some styling --- via the :code:`class` attribute and the "text-right" styling --- to ensure that the button it contains is aligned to the right:
.. code-block:: html
There are no semantics to this structure, simply a way to mark out content to then apply the styling to, thus :code:`
` is ideal.
Structural
++++++++++
While it is possible to only use :code:`
` to mark up the complete document, this is to be avoided, as for many types of content, more specific HTML elements exist. By using these instead of :code:`
`, the browser "knows" what the semantics are of that part of the content and can react appropriately, in particular it can provide more information to assistive technologies such as screen readers. Structural elements generally do not contain text content themselves, rather they define the logical and semantic structure of the document.
:code:``
The :code:`` element is used to mark out the main content area of the document and in particular distinguish it from any headers, footers, or other supplementary materials
:code:``
The :code:`` element is used to specify content that acts as a header. This can be placed both outside the :code:`` to indicate a header for the whole document, but also within more specific elements inside the :code:`` to specify a header for that area.
:code:`