summaryrefslogtreecommitdiff
path: root/web/html.md
blob: e82634d6fd801ded0990efb4998120cc99da7103 (plain) (blame)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
---
layout: web
title: A Quick Guide to Modern HTML
---

# A Quick Guide to Modern HTML

> This guide is a work in progress!

Modern HTML is a remarkably simple and coherent standard.

## Semantic

- [`<html>`][html]: The root of an HTML document.
  - There should exist one and only one `<html>` element in a document.
- [`<body>`][body]: The content of an HTML document.
  - There may only exist one `<body>` element in a document.
- [`<h1>, <h2>, <h3>, <h4>, <h5>, <h6>`][heading elements]: Various heading elements.
- [`<p>`][p]: A paragraph. Text should typically be wrapped in a `<p>`.
- [`<hr/>`][hr]: A thematic break. Typically styled as a horizontal rule.
- [`<br/>`][br]: A line break.
- [`<wbr/>`][wbr]: A word break *opportunity*.
  - Browsers will prefer breaking there when necessary. This is useful for ex. German and other languages with long words.

<details>
<summary>default styles</summary>

```css
html {
  display: block;
}
```

```css
body {
  display: block;
  margin: 8px;
}
```

```css
h1, h2, h3, h4, h5, h6 {
  display: block;
  font-weight: bold;
}

h1 {
  font-size: 2em;
  margin-block: .67em;
}

h2 {
  font-size: 1.5em;
  margin-block: .83em;
}

h3 {
  font-size: 1.17em;
  margin-block: 1em;
}

h4 {
  font-size: 1.00em;
  margin-block: 1.33em;
}

h5 {
  font-size: 0.83em;
  margin-block: 1.67em;
}

h6 {
  font-size: 0.67em;
  margin-block: 2.33em;
}
```

```css
p {
  display: block;
  margin-block: 1em;
}
```

```css
hr {
  display: block;
  color: gray;
  border: 1px inset;
  margin-block: 0.5em;
  margin-inline: auto;
  overflow: hidden;
}
```

```css
br {
  display-outside: newline;
}
```

```css
wbr {
  display-outside: break-opportunity;
}
```

</details>

- [`<main>`][main]: The main content of the body of a document.
    - There should exist at most one visible `<main>` element in a document.
- [`<header>`][header]: A section of a document intended for treatment as a header.
- [`<footer>`][footer]: A section of a document intended for treatment as a footer.
- [`<aside>`][aside]: A portion of a document only indirectly related to the main content.
- [`<article>`][article]: A self-contained composition within a document.
- [`<nav>`][nav]: A section of the document that provides navigation links.
- [`<section>`][section]: A generic standalone section of the document.
- [`<div>`][div]: The generic block container. `display: block`.
- [`<span>`][span]: The generic inline container. `display: inline` (default).

<details>
<summary>default styles</summary>

```css
main, section, aside, header, footer, article, nav, div {
  display: block;
}

span {
  /* display: inline; */
}
```

</details>

## Stylistic

- [`<b>`][b] or [`<strong>`][strong]: <b>bold</b> text
- [`<i>`][i] or [`<em>`][em]: <i>italic</i> text
- [`<u>`][u]: <u>underlined</u> text
- [`<s>`][s]: <s>struckthrough</s> text
- [`<q>`][q]: <q>quoted</q> text
- [`<small>`][small]: <small>small</small> text
- [`<sub>`][sub]: <sub>sub</sub>text
- [`<sup>`][sup]: <sup>super</sup>text
- [`<code>`][code]: <code>text styled like code</code>
- [`<pre>`][pre]: <pre style="display: inline; font-size: 0.8em;">preformatted text</pre>

<details>
<summary>default styles</summary>

```css
b, strong {
  font-weight: bolder;
}
```

```css
i, em {
  font-style: italic;
}
```

```css
u {
  text-decoration: underline;
}
```

```css
s {
  text-decoration: line-through;
}
```

```css
q:before {
  content: open-quote;
}

q:after {
  content: close-quote;
}
```

```css
small {
  font-size: smaller;
}
```

```css
sub {
  vertical-align: sub;
  font-size: smaller;
}
```

```css
sup {
  vertical-align: super;
  font-size: smaller;
}
```

```css
code {
  font-family: monospace;
}
```

```css
pre {
  display: block;
  font-family: -moz-fixed;
  white-space: pre;
  margin-block: 1em;
}
```

</details>

## Metadata

Several HTML elements provide *metadata* about the document in question. They are invisible and removed from the render tree (`display: none`), and only serve to provide information to the browser.

- [`<head>`][head]: The document metadata container.
  - There should exist one and only one `<head>` element in a document.
- [`<title/>`][title]: The title of the document to be displayed.
- [`<base href="" target=""/>`][base]: the base URL for all relative URLs to use.
  - There should exist no more than one `<base>` element in a document.
- [`<link .../>`][link]: Establishes relationships between the current document and other external resources.
  - This is used for a variety of purposes: site icons, external stylesheets, ...
- [`<meta .../>`][meta]: Generic metadata.
- [`<style ...>`][style]: Generic metadata.
- [`<script ...>`][script]: Generic metadata.

<details>
<summary>default styles</summary>

```css
head, title, base, link, meta, style, script {
  display: none;
}
```

</details>

```css
<style>
</style>
```

```css
<script>
</script>
```

`<template>`, `<slot>`

## Content

So far, the HTML elements we have looked at have been purely semantic, and devoid of actual content.

- [`<a href="" rel="" type="" target="" download="">`][a]: An external link to arbitrary content.
- [`<img src="" width="" height="" alt="" .../>`][img]: Embedded image content.
- [`<audio src="" ...>`][audio]: Embedded audio content.
- [`<video src="" width="" height="" ...>`][video]: Embedded video content.
- [`<track src="" ...>`][track]: Subtitles for audio/video content.
- [`<canvas width="" height="">`][canvas]: A target for drawing interactive graphics.
- [`<iframe width="" height="" ...>`][iframe]: Embedded arbitrary HTML content.

<details>
<summary>default styles</summary>

```css
a {
  color: #0000EE;
  text-decoration: underline;
  cursor: pointer;
}
```

```css
img {
  /* object-fit: fill; */
}
```

```css
video {
  object-fit: contain;
}
```

```css
iframe {
  border 2px inset;
}
```

</details>

## Lists

HTML supports *ordered* and *unordered* lists.

- [`<ul>`][ul]: An *unordered* list. Displayed with bullet points by default.
- [`<ol type="a" start=1 reversed>`][ol]: An *ordered* list. Displayed as a numbered list by default.
- [`<li value=1>`][li]: A *list element*. Nested within either `<ul>` or `<ol>`.

<details>
<summary>default styles</summary>

```css
li {
  display: list-item;
  text-align: match-parent;
}
```

```css
ul, menu {
  display: block;
  counter-reset: list-item;
  margin-block: 1em;
  padding-inline-start: 40px;
  list-style-type: disc;
}
```

```css
ol {
  display: block;
  counter-reset: list-item;
  margin-block: 1em;
  padding-inline-start: 40px;
  list-style-type: decimal;
}
```

</details>

Both ordered and unordered lists should have only `<li>`s as their directly nested elements.

On ordered lists, the `type` attribute indicates the numbering type, and can be one-of `a` (lowercase letters), `A` (uppercase letters), `i` (lowercase Roman numerals), `I` (uppercase Roman numerals), or `1` (Arabic numerals, default). The `start` attribute indicates the ordinal at which to start labeling at, and must be an Arabic numeral, even when `type` is not the default. The presence of the `reversed` attribute indicates that a list should be labelled in descending order. As usual, these properties all may be overridden with targeted CSS.

Within ordered lists, the `value` attribute on an `li` indicates that the ordering should jump to the specified ordinal and continue from there. Like `start`, it takes an Arabic numeral, irrespective of ordering style.

There are also a third type of lists available: *description lists*. These do not use the `<li>` element and are generally left unused. We discuss them in a future section.

## Hypertext

- [`<form ...>`][form]: A section that can perform interactive HTTP requests.
- [`<button ...>`][button]: An interactive element to be activated by the user.
- [`<input .../>`][input]: An interactive element that takes user input.
- [`<textarea ...>`][textarea]: An interactive element for multi-line user input.
- [`<label for="idx">`][label]: A label for an element (typically `<input>`) in a user interface.
- [`<meter ...>`][meter]: A fractional/scalar value within a known range.
- [`<select ...>`][select]: An interactive element that provides a list of options.
  - [`<option label="" value="" selected disabled>`][option]: A menu option within a `<select>`.
  - [`<optgroup label="" disabled>`][optgroup]: A grouping of options within a `<select>`.

<details>
<summary>default styles</summary>

```css
form {
  display: block;
}
```

```css
button {
  letter-spacing: initial;
  word-spacing: initial;
  line-height: initial;
  text-transform: initial;
  text-indent: initial;
  text-shadow: initial;
  text-align: center;
  display: inline-block;
  box-sizing: border-box;
}
```

```css
input {
  letter-spacing: initial;
  word-spacing: initial;
  line-height: initial;
  text-transform: initial;
  text-indent: initial;
  text-shadow: initial;
  text-align: initial;
}
```

```css
input[type="button"], input[type="reset"], input[type="submit"], {
  text-align: center;
}
```

```css
input[type="button"], input[type="checkbox"], input[type="color"], input[type="file"],
input[type="image"], input[type="radio"], input[type="reset"], input[type="search"], input[type="submit"] {
  box-sizing: border-box;
}
```

```css
input[type="button"], input[type="color"], input[type="reset"], input[type="submit"] {
  display: inline-block;
}
```

```css
input[type="hidden"] {
  display: none !important;
}
```

```css
textarea {
  letter-spacing: initial;
  word-spacing: initial;
  line-height: initial;
  text-transform: initial;
  text-indent: initial;
  text-shadow: initial;
  text-align: initial;
  box-sizing: border-box;
  white-space: pre-wrap;
}
```

```css
select {
  letter-spacing: initial;
  word-spacing: initial;
  line-height: initial;
  text-transform: initial;
  text-indent: initial;
  text-shadow: initial;
  text-align: initial;
  box-sizing: border-box;
}
```

</details>

## Assorted

There are various assorted HTML elements that don't neatly fit into a particular category.

- [`<details name="" open>`][details]
  - [`<summary>`][summary]: A caption for a corresponding `<details>` element.
- [`<map name="">`][map]: Defines an image map with `<area>` for an `<img>`.
  - [`<area>`][area]: Defines clickable areas within a `<map>` element.
- [`<noscript>`][noscript]
- [`<dialog open>`][dialog]
- [`<marquee ...>`][marquee]

Okay. That's everything important. 75 tags or so? I dunno. Manageable!

---

## Tables

- [`<table>`][table]: A table. Structured data.
- [`<thead>`][thead]: A row of cells acting as a header.
- [`<tbody>`][tbody]: The cells that compose the body of the table.
- [`<tfoot>`][tfoot]: A row of cells acting as a footer.
- [`<tr>`][tr]: A row of cells.
- [`<td colspan=1 rowspan=1 headers="">`][td]: Data cells. `colspan` and `rowspan` indicate their size in the table. `headers` indicates the ids of the corresponding header cells.
- [`<th scope="" colspan=1 rowspan=1 headers="">`][th]: Header cells. `colspan` and `rowspan` indicate their size in the table. `headers` indicates the ids of the corresponding header cells. `scope` may be one-of `row`, `col`, `rowgroup`, `colgroup`, and indicates the set of cells the header corresponds to.
- [`<col span=1>`][col]: A column within a table. Columns span over atop rows.
- [`<colgroup span=1>`][colgroup]: A group of columns within a table.
- [`<caption>`][caption]: The title of a table.

The sheer quantity of tags for tables is quite intimidating.
In general, tables are *far* less used than they once were, thanks to improvements in the layout capabilities of CSS.
You can pretty much just ignore them.

## Less Useful Elements

- [`<fieldset form="" name="" disabled>`][fieldset]: A grouping of controls within a `<form>`.
  - [`<legend>`][legend]: A caption for a corresponding `<fieldset>` element.
- [`<output name="" form="id" for="ida idb...">`][output]:
  An element into which the result of a user action is to be injected.
- [`<figure>`][figure]: A wrapper for some arbitrary self-contained content.
  - [`<figcaption>`][figcaption]: An optional caption for a corresponding `<figure>`.
- [`<picture>`][picture]: Embedded image content. Contains multiple `<source>` elements and a fallback `<img>`.
  - [`<source/>`][source]: A media resource for a `<picture>`, `<audio>`, or `<video>` element.

- [`<dl>`][dl]: A *description list*. Consists of terms (`<dt>`) and descriptions (`<dd>`). Ugly as sin, and scarcely used.
- [`<dt>`][dt]: Specifies a *term* entry in a description list.
- [`<dd>`][dd]: Specifies a *description* entry in a description list.
- [`<address>`][address]: An element indicating its contents are contact information.
- [`<blockquote cite="url">`][blockquote]: An element indicating its contents are an extended quotation.
- [`<data value="data">`][data]: An element indicating its content has corresponding data, in the `value` attribute.
- [`<search>`][search]:
  An element indicating its contents are related to searching.
- [`<time datetime="">`][time]:
  An element indicating a specific time. `datetime` must be set to a machine-readable format.

## Deprecation Fodder

These elements are *really* useless. You shouldn't bother knowing them. They're in the spec, but scarcely used.

- [`<abbr>`][abbr]: An element for displaying abbreviations.
- [`<cite>`][cite]: An element indicating its contents are a citation.
- [`<del>`][del]: An element for displaying deleted text.
- [`<dfn>`][dfn]: An element indicating its contents are a definition.
- [`<ins>`][ins]: An element for displaying inserted text.
- [`<kbd>`][kbd]: An element for displaying keyboard shortcuts.
- [`<mark>`][mark]: An element for displaying highlighted text.
- [`<samp>`][samp]: An element for displaying sample output.
- [`<var>`][var]: An element for displaying mathematical variables.
- [`<datalist>`][datalist]: A wrapper for a set of suggested `<option>`s for an `<input>`.
  - `<datalist>` is not widely used and is not implemented in Firefox.
- [`<embed type="mimetype" src="url"/>`][embed]:
  - An element for generic embedded content.
  - `<object>` is generally slightly preferred, but `<img>`, `<audio>`, `<video>`, and the like are preferred over both of them.
- [`<object type="mimetype" data="url">`][object]:
  - An element for displaying generic embedded content. Still useful for embedded PDFs etc.
  - Generally slightly preferred over `<embed>`, but `<img>`, `<audio>`, `<video>`, and the like are preferred over both of them.
- [`<menu>`][menu]: Officially identical to `<ul>` in syntax and semantics.
  - Doesn't have much of a reason to exist.
- [`<progress value=50 max=100>`][progress]: A worse version of `<meter>`.
  - Like, this is *exactly* `<meter>`, but without the `min`, `low`, `high`, and `optimum` attributes. Why??

[b]: https://developer.mozilla.org/HTML/Element/b
[strong]: https://developer.mozilla.org/HTML/Element/strong
[i]: https://developer.mozilla.org/HTML/Element/i
[em]: https://developer.mozilla.org/HTML/Element/em
[u]: https://developer.mozilla.org/HTML/Element/u
[s]: https://developer.mozilla.org/HTML/Element/s
[q]: https://developer.mozilla.org/HTML/Element/q
[small]: https://developer.mozilla.org/HTML/Element/small
[sub]: https://developer.mozilla.org/HTML/Element/sub
[sup]: https://developer.mozilla.org/HTML/Element/sup
[code]: https://developer.mozilla.org/HTML/Element/code
[pre]: https://developer.mozilla.org/HTML/Element/pre
[html]: https://developer.mozilla.org/HTML/Element/html
[body]: https://developer.mozilla.org/HTML/Element/body
[heading elements]: https://developer.mozilla.org/HTML/Element/Heading_Elements
[p]: https://developer.mozilla.org/HTML/Element/p
[hr]: https://developer.mozilla.org/HTML/Element/hr
[br]: https://developer.mozilla.org/HTML/Element/br
[wbr]: https://developer.mozilla.org/HTML/Element/wbr
[main]: https://developer.mozilla.org/HTML/Element/main
[section]: https://developer.mozilla.org/HTML/Element/section
[aside]: https://developer.mozilla.org/HTML/Element/aside
[header]: https://developer.mozilla.org/HTML/Element/header
[footer]: https://developer.mozilla.org/HTML/Element/footer
[article]: https://developer.mozilla.org/HTML/Element/article
[nav]: https://developer.mozilla.org/HTML/Element/nav
[div]: https://developer.mozilla.org/HTML/Element/div
[span]: https://developer.mozilla.org/HTML/Element/span
[head]: https://developer.mozilla.org/HTML/Element/head
[title]: https://developer.mozilla.org/HTML/Element/title
[base]: https://developer.mozilla.org/HTML/Element/base
[link]: https://developer.mozilla.org/HTML/Element/link
[meta]: https://developer.mozilla.org/HTML/Element/meta
[style]: https://developer.mozilla.org/HTML/Element/style
[script]: https://developer.mozilla.org/HTML/Element/script
[a]: https://developer.mozilla.org/HTML/Element/a
[img]: https://developer.mozilla.org/HTML/Element/img
[audio]: https://developer.mozilla.org/HTML/Element/audio
[video]: https://developer.mozilla.org/HTML/Element/video
[track]: https://developer.mozilla.org/HTML/Element/track
[canvas]: https://developer.mozilla.org/HTML/Element/canvas
[iframe]: https://developer.mozilla.org/HTML/Element/iframe
[ul]: https://developer.mozilla.org/HTML/Element/ul
[ol]: https://developer.mozilla.org/HTML/Element/ol
[li]: https://developer.mozilla.org/HTML/Element/details
[form]: https://developer.mozilla.org/HTML/Element/form
[button]: https://developer.mozilla.org/HTML/Element/button
[input]: https://developer.mozilla.org/HTML/Element/input
[textarea]: https://developer.mozilla.org/HTML/Element/textarea
[label]: https://developer.mozilla.org/HTML/Element/label
[meter]: https://developer.mozilla.org/HTML/Element/meter
[select]: https://developer.mozilla.org/HTML/Element/select
[option]: https://developer.mozilla.org/HTML/Element/option
[optgroup]: https://developer.mozilla.org/HTML/Element/optgroup
[details]: https://developer.mozilla.org/HTML/Element/details
[summary]: https://developer.mozilla.org/HTML/Element/summary
[map]: https://developer.mozilla.org/HTML/Element/map
[area]: https://developer.mozilla.org/HTML/Element/area
[noscript]: https://developer.mozilla.org/HTML/Element/noscript
[dialog]: https://developer.mozilla.org/HTML/Element/dialog
[marquee]: https://developer.mozilla.org/HTML/Element/marquee
[table]: https://developer.mozilla.org/HTML/Element/table
[thead]: https://developer.mozilla.org/HTML/Element/thead
[tbody]: https://developer.mozilla.org/HTML/Element/tbody
[tfoot]: https://developer.mozilla.org/HTML/Element/tfoot
[tr]: https://developer.mozilla.org/HTML/Element/tr
[td]: https://developer.mozilla.org/HTML/Element/td
[th]: https://developer.mozilla.org/HTML/Element/th
[col]: https://developer.mozilla.org/HTML/Element/col
[colgroup]: https://developer.mozilla.org/HTML/Element/colgroup
[caption]: https://developer.mozilla.org/HTML/Element/caption
[fieldset]: https://developer.mozilla.org/HTML/Element/fieldset
[legend]: https://developer.mozilla.org/HTML/Element/legend
[output]: https://developer.mozilla.org/HTML/Element/output
[figure]: https://developer.mozilla.org/HTML/Element/figure
[figcaption]: https://developer.mozilla.org/HTML/Element/figcaption
[picture]: https://developer.mozilla.org/HTML/Element/picture
[source]: https://developer.mozilla.org/HTML/Element/source
[dl]: https://developer.mozilla.org/HTML/Element/dl
[dt]: https://developer.mozilla.org/HTML/Element/dt
[dd]: https://developer.mozilla.org/HTML/Element/dd
[address]: https://developer.mozilla.org/HTML/Element/address
[blockquote]: https://developer.mozilla.org/HTML/Element/blockquote
[data]: https://developer.mozilla.org/HTML/Element/data
[search]: https://developer.mozilla.org/HTML/Element/search
[time]: https://developer.mozilla.org/HTML/Element/time
[abbr]: https://developer.mozilla.org/HTML/Element/abbr
[cite]: https://developer.mozilla.org/HTML/Element/cite
[del]: https://developer.mozilla.org/HTML/Element/del
[dfn]: https://developer.mozilla.org/HTML/Element/dfn
[ins]: https://developer.mozilla.org/HTML/Element/ins
[kbd]: https://developer.mozilla.org/HTML/Element/kbd
[mark]: https://developer.mozilla.org/HTML/Element/mark
[samp]: https://developer.mozilla.org/HTML/Element/samp
[var]: https://developer.mozilla.org/HTML/Element/var
[datalist]: https://developer.mozilla.org/HTML/Element/datalist
[embed]: https://developer.mozilla.org/HTML/Element/embed
[object]: https://developer.mozilla.org/HTML/Element/object
[menu]: https://developer.mozilla.org/HTML/Element/menu
[progress]: https://developer.mozilla.org/HTML/Element/menu