The body of a page is built from headings, paragraphs and smaller pieces of formatted text.
Heading Tags
HTML has six levels of headings, from <h1> (the most important and largest) to <h6> (the least important and smallest). Headings give the structure of the page, like the chapters and sections of a book. Search engines and screen readers use them to understand the page, so there should be one <h1>, and levels should not be skipped.
Definition: Block element. An HTML element that starts on a new line and takes the full available width, such as p, h1 and div, as opposed to an inline element that flows within a line.
Paragraph and Line Tags
<p> marks a paragraph, which the browser displays with space before and after. Browsers ignore extra spaces and line breaks in the source, so <br> is used to force a line break, and <hr> draws a horizontal rule (line) to separate sections. <pre> displays text exactly as written, keeping spaces and line breaks, which is useful for code and poems.
Body Tag and Its Attributes
The <body> tag contains all the visible content. In older HTML it took attributes such as bgcolor, text, link and background, but in HTML5 these are done with CSS.
Text Formatting Tags
<b> shows text in bold, and <strong> marks it as important (also shown bold). <i> shows italics, and <em> marks emphasis. <u> underlines, <mark> highlights, <small> makes text smaller, <del> shows deleted text with a line through it, and <ins> shows inserted text. <sub> and <sup> give subscripts and superscripts, as in H<sub>2</sub>O and x<sup>2</sup>. <blockquote> marks a long quotation, <q> a short one, <abbr> an abbreviation and <code> program code. <span> is a generic inline container used with CSS.
Special Characters
Characters that have a meaning in HTML must be written as entities: < for <, > for >, & for &, for a non-breaking space and © for ©.
Table 1.2: Text formatting tags
| Tag | Effect | Example |
|---|---|---|
| <strong>, <b> | Bold | <strong>Note</strong> |
| <em>, <i> | Italic | <em>very</em> |
| <mark> | Highlight | <mark>key</mark> |
| <sub>, <sup> | Subscript, superscript | H<sub>2</sub>O, x<sup>2</sup> |
| <del>, <ins> | Deleted, inserted | <del>old</del> <ins>new</ins> |
Example 1.2: Formatting a short article
Problem: Write the body of a page with a main heading, two subheadings, a paragraph with bold and italic words, a chemical formula and a horizontal rule.
Solution:
- Use <h1> for the title and <h2> for the subheadings.
- Use <p> with <strong> and <em> inside.
- Write the formula with <sub>.
- Separate the sections with <hr>.
Result: The page shows a structured article with formatted text.
<body>
<h1>Water</h1>
<h2>Formula</h2>
<p>The formula of water is H<sub>2</sub>O.
It is <strong>essential</strong> for <em>all</em> life.</p>
<hr>
<h2>Boiling Point</h2>
<p>Water boils at 100°C at sea level.<br>
It freezes at 0°C.</p>
<p><mark>Remember:</mark> 1 litre of water weighs about 1 kg.</p>
</body>
Common mistakes
- Choosing a heading level for its size instead of its importance.
- Using many <br> tags to create space, instead of paragraphs and CSS.
- Typing < directly in text instead of <.
Key takeaways
- <h1> to <h6> give six levels of headings.
- <p> makes a paragraph, <br> a line break, <hr> a rule.
- <strong> and <em> mark importance and emphasis.
- Special characters are written as entities such as < and &.
Practice questions
- How many heading levels does HTML have?
- What does <pre> do?
- How is a superscript written?
Answers to the practice questions
- Six, from <h1> to <h6>.
- It displays text exactly as written, keeping spaces and line breaks.
- With the <sup> tag, as in x<sup>2</sup>.
Sources
- Official BCA syllabus Web Technologies, course code 25BCA402SEC02, Unit I
