summaryrefslogtreecommitdiff
path: root/writing_minimal_html5_documents_is_fun.html
blob: d91c18576fbeffc43cce0f6d0be0c20bf89bed03 (plain)
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
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="aki">
<meta name="tags" content="minimal, valid, html5, web page, document">
<meta name="published-on" content="2020-08-03T18:18:00+02:00">
<link rel="icon" type="image/png" href="favicon.png">
<link rel="stylesheet" type="text/css" href="style.css">

<title>Writing Minimal HTML5 Documents is Fun</title>

<header>
<nav><a href="https://ignore.pl">ignore.pl</a></nav>
<time>3 August 2020</time>
<h1>Writing Minimal HTML5 Documents is Fun</h1>
</header>

<article>
<p>Yes, I know how it sounds to have both "HTML" and "minimal" in one sentence. I think it's possible to accomplish
that and I'll show you how. Before we start, let's set some rules:</p>

<ul>
<li>Document syntax must be valid. From the very beginning to the very end of it - all valid HTML5.
<li>Element hierarchy should be shallow and not exceed 5 levels. This only includes what is written and not how browser
interprets it.
</ul>

<p>Sounds good. Keeping those rules in mind, the shortest document we can produce is:</p>

<pre>
&lt;!doctype html&gt;
&lt;title&gt;Hello&lt;/title&gt;
</pre>

<p>In case of doubts consult <a href="https://validator.w3.org/nu/">W3's checker</a>. You must insert the code yourself,
as they don't support this kind of links.</p>

<img src="writing_minimal_html5_documents_is_fun-1.png" alt="html5 logo">

<p>Now then, that's not quite useful, but it clearly indicates that we can skip tags. We can skip a lot of tags and
the document will remain valid. First of all, you can skip <code>head</code> and <code>body</code> elements entirely,
as long as you keep the content in the document in a nice sequence and the division between the meta information and the
actual body is easily deducible:

<pre>
&lt;!doctype html&gt;
&lt;html lang="en"&gt;
&lt;meta charset="utf-8"&gt;
&lt;link rel="stylesheet" href="style.css"&gt;

&lt;title&gt;Hello&lt;/title&gt;

&lt;h1&gt;Hello&lt;/h1&gt;
&lt;p&gt;Lorem ipsum dolor sit amet.
</pre>

<p>There are a few points of interests in this example:

<dl>
<dt><code>&lt;html lang="en"&gt;</code>
<dd><code>html</code> element can be omitted entirely but it's suggested to use its <code>lang</code> attribute to
specify intended language of the document. Missing <code>lang="en"</code> is not an error; it's a warning, but the
attribute is quite helpful for browsers, search engines and therefore users.
<dt><code>&lt;meta charset="utf-8"&gt;</code>
<dd><code>meta</code> element with <code>charset</code> is not needed, but is suggested.
<dt><code>&lt;p&gt;Lorem...</code>
<dd>Ending tag for <code>p</code> may be omitted in the usual cases.
<dt><code>&lt;/h1&gt;</code>
<dd>Ending tag for <code>h1</code> (and other headings) must be present just like in case of <code>title</code> element.
</dl>

<p>The rule that applies to <code>p</code> also applies to e.g. <code>li</code>, <code>dt</code> or <code>dd</code>.
Generally, if I'm not sure if I can omit the ending tag, I ask myself two questions:

<ul>
<li>"Is this element a part of some sequential or well-defined data structure?"
<li>"Could the next element be a child of this element?"
</ul>

<p>Answering those is kind of tricky at the start but one gets used to it. I did, at least. If you are still unsure, you
can refer directly to <a href="https://html.spec.whatwg.org/multipage/syntax.html#optional-tags">the standard</a>.
<p>Let's walk through a longer example, the comments are in-lined in its elements:

<pre>
&lt;!doctype html&gt;
&lt;html lang="en"&gt;
&lt;meta charset="utf-8"&gt;
&lt;link rel="stylesheet" href="style.css"&gt;

&lt;title&gt;Title must have an ending tag&lt;/title&gt;

&lt;article&gt;
&lt;h1&gt;h1 must have one as well&lt;/h1&gt;

&lt;p&gt;This paragraph is good as it is.
&lt;p&gt;Same goes for this one. In-line text styles must have end tags pretty
much &lt;strong&gt;always&lt;/strong&gt;. But hey, it would be weird to
&lt;em&gt;not have them&lt;/em&gt; there, right?&lt;/p&gt;
&lt;img src="image.png" alt="logo or something, dunno"&gt;
&lt;p&gt;Img element can be a child of a paragraph. If you want to make sure that
it is outside of p, then write end tag manually.
&lt;p&gt;Following pre element is considered a block, so you can skip ending tag.

&lt;pre&gt;
On the other hand pre MUST have ending tag.
&lt;/pre&gt;

&lt;p&gt;We're cool here, as everything is closed so far. Let's list some stuff:

&lt;ul&gt;
&lt;li&gt;ul is the same as pre in context of ending tag for the p element.
&lt;li&gt;List elements may omit ending tags.
&lt;li&gt;Same applies to dt, dd that are used in dl
&lt;li&gt;dl and ol follow the same rules as pre and ul. Ending tag is needed.
&lt;/ul&gt;

&lt;table&gt;
&lt;tr&gt;&lt;td&gt;As    &lt;td&gt;you     &lt;td&gt;see
&lt;tr&gt;&lt;td&gt;table &lt;td&gt;insides &lt;td&gt;are
&lt;tr&gt;&lt;td&gt;cool  &lt;td&gt;without &lt;td&gt;end tags.
&lt;/table&gt;

&lt;p&gt;But only insides. Table itself must have end tag.
Same goes for article element:

&lt;/article&gt;
</pre>

<p>That's about it. In example above the deepest element in hierarchy was:
<code>html</code>/<code>article</code>/<code>table</code>/<code>tr</code>/<code>td</code>.
That's 5 visible levels. Of course, behind the scenes there are more including <code>tbody</code> and <code>body</code>,
but that's acceptable in our case. As per requirements, the presented document is valid.

<p>I think that adding some more personal restrictions can make the document more readable in plain text. Some users may
appreciate it. Consider adding empty lines where it feels necessary, adding or skipping indention, and so on.

<p>In case of problems refer to:
<ul>
<li><a href="https://html.spec.whatwg.org/multipage/syntax.html#optional-tags">HTML standard</a>
<li><a href="https://developer.mozilla.org/pl/">MDN web docs</a>
<li><a href="https://validator.w3.org/nu/">W3 nu HTML checker</a>
</ul>
</article>
<script src="https://stats.ignore.pl/track.js"></script>