HTML Fundamentals (2)
HTML Fundamentals (2)
Structuring HTML
There are many layout HTML elements that helps to structure the file in an ordered manner. Specifically speaking, the most commonly seen ones are:
<main>is for content unique to this page. Use<main>only once per page, and put it directly inside . Ideally this shouldn’t be nested within other elements.<article>encloses a block of related content that makes sense on its own without the rest of the page (for example, a single blog post. If you use your dev tool in your browser on this page, you may find the whole blog content is wrapped within a<article>tag.).<section>is very similar to<article>, it is used to group a segment of the page that have a uniform function(like a mini map or a paragraph). You can break<article>up into<section>s and vice versa, depending on the context.<aside>contains supplementary information than is indirectly related to the main content(like glossary entries, author biography or links)
In the MDN web docs you can usually see contribution links wrapped in
<aside>at the bottom of the page:
<header>represents the introductory section of a page or a section. It can contain logos, navigations or headings. If it is a child of<body>it defines the global header of a webpage, but if it’s a child of an<article>or<section>it defines a specific header for that section.
Differing
<header>from<title>and headings
- the
<title>is the title of the whole webpage, and is not shown in the body. It is the child of<head>.- the headings(
<h1>-<h6>) are child of<body>, they can be wrapped in<header><header>is a semantic container element (just like<div>but with semantic meaning)
<nav>contains navigational links of the page, including secondary link.<footer>the footer element of the page
None semantic wrappers
<div>is a block level wrapper<span>is an inline level wrapper
They are very convenient to use, but make sure you only use them when there is no better wrapper in place. For readability and search engine compatability, we now promotes semantic html.
<br> and <hr>
<br>is a line breaker, it is a self-closing tag.<hr>is a thematic break element,it looks like a horizontal line in the file(just like***in markdown in this blog). Semantically, it denotes a change of topic or section.
It is also a self-closing tag.
HTML Tags in Detail
Links <a>
To create a hyperlink, we use <a> with its href attribute:
1 | <a href=""> |
We can add hyperlink to almost any element, including block level elements:
1 | <a href=""> |
title Attribute
We can use the title attribute of <a> to give additional information to the user when the pointer hovers on the link:
like this.
target Attribute
If the link is to open a new site, we can decide whether to navigate the user to a new tab or just open it in the current tab usingtarget attribute:
- To open links in a new tab, we simply set
target="_blank"
download Attribute
Sometimes we want to make the browser download the file in href instead of directing to a new site. To do this, we need to specify the download attribute.
1 | <a href="" download="file name">download file</a> |
- The
downloadattribute can be blank, only when there is thedownloadattribute will the browser enforce a download. - We can set the default name of the downloaded file.
Filepath
In order to create a hyperlink to one document, we need the filepath of the target.
- Absolute URL: what we type in the browser to open a website:
An absolute URL should at least have these parts:
- Protocol:
http://orhttps:// - Domain Name: like
github.com - Path to specific page: like
github.com/about/team.html - Query String: like
github.com/search?q=emulisy - Fragment:
github.com/docs/index.html#section1
- Protocol:
- Relative Filepath: A project may have multiple directories. Using relative path, we can reference documents under our project:
- Lower level directory:
1
2
3
4
5project/
├── index.html
└── assets/
└── css/
└── themes/If the project is shaped like this, inside index.html we want to refer to documents inside css or assets folder, we just type
css/document.cssorassets/documentTo refer to same or lower directories, we just type in the folder and the document name, separated with
/. - Higher level directory:
1
2
3
4
5
6project/
├── index.html
└── assets/
└── css/
└── style.css
└── themes/If we want to refer to index.html inside the style.css, we can type
../index.htmlTo refer to higher level directories, we add
../to indicate looking up one level. So to go up three levels, we type../../../folder/document
- Lower level directory:
These file path is very important when creating links or adding images, audios and videos, as these resources are usually not presented directly in the html file.
Image <img>
This is a void element, so it can’t have any child or closing tag, unlike <a> you can put almost anything in it.
Using <img> we need two essential properties:
src=: the source root of the imagealt=: dictates the content of the image, and is shown when the image can’t be loaded.
Tip: Don’t point the
srcof the image to another website without permission, as this cost extra bandwidth to others when delivering the image to your page. Also, doing so leaves you no control over the image you referred to.
width and height
Usually we may want to start handling the visual effect of the page in CSS, however it is best practice to set the size of the image directly using width and height properties as they provide the aspect-ratio and enforce the browser to reserve space for the image.
Captions <figure> and <figcaption>
Although we can import the image and use <p> to write captions for the figure, it is hard for screen reader to associate the figure with the caption. However, it can be done using <figure> and <figcaption> element.
1 | <img src="figure.jpg" alt="figure" width="400" height="400"> |
1 | <figure> |
Videos <video>
This element is very much like the img, it also needs a src= attribute to find the video document.
controls: Usually we want the user to be able to control the video itself, to do so we only need to add the controls attribute, and the browser will render the video with controls for us.
1 | <video src="file.mp4" controls> |
You may notice the paragraph inside <video>, this is a fallback content. It works just like the altin <img>, when the video is not supported, these contents will be shown on the page.
Multiple Format Support
For videos and audios, they need to be encoded using different codecs, and not all browsers support the same codecs, therefore we end up with compatability issues that some video and audio format may not be available on certain browsers.
1 | <video controls> |
To resolve compatability issues, we can introduce multiple formats by extracting the src= attribute. We can put the different source roots in separate <source/> tags.
In this case, the browser will play the first one that it has a supporting codec, and eventually print the fallback text if no such codec is available.
Other attributes
1 | <video |
Audios <audio>
Introducing audios are almost the same as introducing videos:
1 | <video controls> |
You can provide transcript of the audio using <track>
Tables <table> <th> <td> <tr>
We can create tables in this format:
1 | <table> |
Each row of the table is wrapped inside a <tr> table row, and within each row we can put in cells, which is wrapped in <td> table data.
<th> is a special table data, as they are usually the first row of the table, table headers. By default, they are bold and centered.
Tables of different shape
Sometimes, we want the table to shape differently, not just one column with several corresponding rows. We want rows or columns to span multiple cells.
To do so, we need rowspan and colspan attributes:
1 | <table> |
This table looks like this:
Button <button>
A single button is created using <button>:
1 | <button>This is a button</button> |
Usually the button itself is meaningless, so we need to use form or javascript to add certain effect to the button. like
1 | const button = document.querySelector(button); |
Form
A basic form contains several elements:
- A
<form></form>that wraps everything. Any data inside this is considered from the same form. - Some
<label><input>or<select>element that allows the user to choose or input data. - A
<button>to submit the data
Tip: we can use
<section><div>or even<li>to group the form elements, but a specialized wrapper for the job is<filedset>
<form>
This tag have two frequently used attributes:
action: contains path to the page where we want to submit the datamethod: contains the HTTP request method for the data transmission, likegetpost
<input>
This void element enables user to input data.
1 | <input type="text" name="name" id="name" value="please type here" required /> |
type: there are MANY types of input, ranging from text, passwords to checkboxes, dates or even range widget. You can visit here for the complete options.nameid: they are all identifiers for the data to be submittedrequired: decide whether the form can be submitted with or without this inputvalue: The initial state of the input element, varies according to the type. like for “text” or “password”, this defines the pre-filled text. But for “button” or “checkbox”, this defines the label of the input.
<label>
<label> provides extra information, notably identifiers for the input. They are associated with certain inputs explicitly or implicitly.
- Explicit:The label associate with the input via
1
2<label for="name">Name (required):</label>
<input type="text" name="name" id="name" required />forattribute andidof the input element. - Implicit:The input are nested inside the label.
1
2
3
4<label>
Name (required):
<input type="text" name="name" required />
</label>
<button>
When a button is inside a form, the default behaviour is to submit the form.
Just like the <input>, button also have type attribute:
type="submit": since this is the default behaviour you don’t really have to specify this.type="reset": reset the form to initial state, doing so erases all user input.type="button": makes the button the same as the button outside the form, clicking it have no effect.
<select> <option>
These tags are for creating drop-down menus:
1 | <label for="portion">Portion size:</label> |
Other form items:
Multiline text input:
1
2<label for="comments">Any other comments:</label>
<textarea id="comments" name="comments" rows="5" cols="33"></textarea>Using
<textarea>, they behave just like<input type="text">, but allows multiline entry.Radio buttons:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26<fieldset>
<legend>Education level</legend>
<!-- Use this to add subtitle for the form selector-->
<input
type="radio"
id="choice1"
name="education"
value="bachelor"
checked />
<!-- use checked if you want this to be selected initially-->
<label for="choice1">Bachelor</label>
<input
type="radio"
id="choice2"
name="education"
value="master" />
<label for="choice2">Master</label>
<input
type="radio"
id="choice3"
name="education"
value="doctor"
disabled />
<!-- Use disabled to make this option unselectable-->
<label for="choice3">Doctor</label>
</fieldset>
Checkbox:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20<fieldset>
<legend>To do list</legend>
<input
type="checkbox"
id="choice1"
name="feedCat" />
<label for="choice1">Feed my cat</label>
<input
type="checkbox"
id="choice2"
name="playWithCat" />
<label for="choice2">Play with my cat</label>
<input
type="checkbox"
id="choice3"
name="work"
checked />
<!-- Use checked to pre-select the box-->
<label for="choice3">Go to work</label>
</fieldset>


