HTML Text Elements
Introduction to Text Elements
Text elements are the foundation of content on the web. HTML provides a variety of elements to structure and format text, making it readable, accessible, and SEO-friendly. These elements help convey the meaning and importance of different parts of your content.
Headings
HTML provides six levels of headings, from <h1>
to <h6>
, with <h1>
being the most important and <h6>
the least. Headings create a hierarchical structure for your content.
Heading Elements
<h1>Main Heading</h1>
<h2>Subheading</h2>
<h3>Section Heading</h3>
<h4>Subsection Heading</h4>
<h5>Minor Heading</h5>
<h6>Smallest Heading</h6>
Main Heading
Subheading
Section Heading
Subsection Heading
Minor Heading
Smallest Heading
<h1>
per page, and maintain a logical heading hierarchy. Don't skip heading levels (e.g., don't go from <h2>
to <h4>
).
Paragraphs and Line Breaks
Paragraph Element
The <p>
element defines a paragraph of text. Browsers automatically add margin space before and after paragraphs.
<p>This is a paragraph of text. HTML will automatically wrap the text to fit the available space.</p>
<p>This is another paragraph. Notice the space between paragraphs.</p>
This is a paragraph of text. HTML will automatically wrap the text to fit the available space.
This is another paragraph. Notice the space between paragraphs.
Line Break Element
The <br>
element creates a line break within text. It's an empty element (no closing tag needed).
<p>This text contains<br>a line break.</p>
This text contains
a line break.
<br>
to create space between elements. Use CSS margins or padding instead.
Horizontal Rule
The <hr>
element creates a horizontal line, representing a thematic break in content.
<p>Text before the break.</p>
<hr>
<p>Text after the break.</p>
Text before the break.
Text after the break.
Text Formatting Elements
Semantic Formatting
These elements add semantic meaning to text:
Element | Description | Example | Output |
---|---|---|---|
<em> |
Emphasized text (typically italic) | <em>Emphasized text</em> |
Emphasized text |
<strong> |
Important text (typically bold) | <strong>Important text</strong> |
Important text |
<mark> |
Marked/highlighted text | <mark>Highlighted text</mark> |
Highlighted text |
<cite> |
Title of a work | <cite>The Great Gatsby</cite> |
The Great Gatsby |
<dfn> |
Definition term | <dfn>HTML</dfn> |
HTML |
<abbr> |
Abbreviation with title attribute | <abbr title="HyperText Markup Language">HTML</abbr> |
HTML (hover to see) |
Presentational Formatting
These elements primarily affect presentation (use CSS for styling when possible):
Element | Description | Example | Output |
---|---|---|---|
<b> |
Bold text (without importance) | <b>Bold text</b> |
Bold text |
<i> |
Italic text (without emphasis) | <i>Italic text</i> |
Italic text |
<u> |
Underlined text | <u>Underlined text</u> |
Underlined text |
<s> |
Strikethrough text | <s>Strikethrough text</s> |
|
<small> |
Smaller text | <small>Smaller text</small> |
Smaller text |
<sub> |
Subscript text | H<sub>2</sub>O |
H2O |
<sup> |
Superscript text | 2<sup>3</sup> = 8 |
23 = 8 |
Quotations and Citations
Blockquote
The <blockquote>
element is used for extended quotations that form a separate block of text.
<blockquote cite="https://www.example.com/source">
<p>The only way to learn a new programming language is by writing programs in it.</p>
<footer>— Dennis Ritchie</footer>
</blockquote>
The only way to learn a new programming language is by writing programs in it.
Inline Quote
The <q>
element is used for shorter, inline quotations.
<p>As Shakespeare wrote, <q>To be or not to be, that is the question.</q></p>
As Shakespeare wrote, To be or not to be, that is the question.
Citation
The <cite>
element is used to reference a creative work, such as a book, movie, or song.
<p>My favorite book is <cite>The Lord of the Rings</cite> by J.R.R. Tolkien.</p>
My favorite book is The Lord of the Rings by J.R.R. Tolkien.
Code and Preformatted Text
Inline Code
The <code>
element is used to display inline code snippets.
<p>The <code>console.log()</code> function outputs to the browser console.</p>
The console.log()
function outputs to the browser console.
Preformatted Text
The <pre>
element preserves both spaces and line breaks. It's often used with <code>
for code blocks.
<pre>
This text preserves
spaces and
line breaks.
</pre>
This text preserves spaces and line breaks.
Code Blocks
Combining <pre>
and <code>
creates formatted code blocks.
<pre><code>
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet('World'));
</code></pre>
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet('World'));
Best Practices for Text Elements
- Use semantic elements - Choose elements based on their meaning, not just appearance.
- Maintain heading hierarchy - Use headings in order (h1, h2, h3...) without skipping levels.
- Keep paragraphs focused - Each paragraph should cover a single idea or topic.
- Use
<strong>
and<em>
- Prefer these over<b>
and<i>
for their semantic value. - Avoid using
<br>
for spacing - Use CSS margins and padding instead. - Use proper quotation elements -
<blockquote>
for block quotes and<q>
for inline quotes. - Include the
cite
attribute - When using quotations, include the source when possible. - Use
<abbr>
withtitle
- Provide the full form of abbreviations for better accessibility.