Learn HTML For Beginners : Text Formatting in HTML

Learn HTML For Beginners Text Formatting in HTML

Text formatting in HTML refers to the process of applying visual enhancements and structural elements to text content within an HTML document. It allows you to control the appearance and organization of text to improve readability and convey meaning effectively.

HTML provides a range of elements and attributes that enable you to format text in various ways. Some common text formatting options include:


Headings in HTML are used to define the hierarchical structure and organization of a document's content. They are represented by <h1> to <h6> tags, with <h1> being the highest level (main heading) and <h6> being the lowest level (subheading).

Headings serve several purposes:

a. Document Structure

Headings help organize the content of a document, indicating the main sections and subsections. They provide a logical structure that aids in navigation and understanding.

b. Semantic Meaning

Each heading level carries semantic meaning. <h1> represents the main heading of a page, typically used for the title or main topic. <h2> represents section headings, <h3> represents sub-sections within sections, and so on. This semantic structure improves accessibility and allows assistive technologies to interpret the content accurately.

c. Visual Hierarchy

Headings have different sizes and styles by default, with <h1> being the largest and most prominent, and <h6> being the smallest. This visual distinction helps users quickly scan and understand the relative importance of different sections within the document.

Usage example:

<h1>Main Heading</h1>
<h2>Section 1</h2>
<p>This is the content of section 1.</p>
<h2>Section 2</h2>
<p>This is the content of section 2.</p>
<h3>Subsection 2.1</h3>
<p>This is the content of subsection 2.1.</p>

In this example, the <h1> represents the main heading of the document, while <h2> and <h3> represent sections and subsections respectively. The corresponding content follows each heading, providing a clear structure to the document.


Paragraphs in HTML are used to group and present blocks of text. They are represented by the <p> element. Paragraphs allow you to structure and separate textual content into meaningful units, making it easier to read and comprehend.

Here's how paragraphs are used in HTML:

<p>This is a paragraph of text.</p>
<p>This is another paragraph.</p>

In the example above, we have two paragraphs. Each paragraph is enclosed within the opening <p> tag and the closing </p> tag. The content placed between these tags is considered part of the paragraph.

Key points about paragraphs in HTML:

  1. Text Segmentation: Paragraphs help break down text into manageable chunks, enhancing readability. They are often used to represent discrete blocks of content.
  2. Default Styling: By default, browsers add vertical spacing (margins) before and after paragraphs to visually separate them from surrounding content. The exact styling can be further customized using CSS (Cascading Style Sheets).
  3. Block-Level Element: The <p> element is a block-level element, meaning it starts on a new line and occupies the full width available within its parent container. This behavior distinguishes paragraphs from inline elements such as <span>.
  4. Nesting Limitation: Paragraphs cannot contain other block-level elements, like headings or other paragraphs. If you need to group multiple elements together within a paragraph, you can use <div> or other appropriate container elements.
  This is a paragraph with <strong>bold text</strong> and
  <a href="#">a link</a>.
  This is a <span>span</span> within a paragraph.

In the above example, the first paragraph contains a <strong> element for bold text and an <a> element for a link. The second paragraph has a <div> element within it, which is allowed but disrupts the paragraph structure.

Paragraphs are fundamental for organizing and presenting textual content in HTML, providing structure, readability, and flexibility in styling and layout.

Bold and Italic Text

Bold and italic text formatting in HTML allows you to emphasize specific parts of your content. It can be used to highlight keywords, titles, or important phrases. HTML provides two main elements for text formatting: <strong> and <em> (which can also be represented by <b> and <i> respectively).

Here's how you can use bold and italic text formatting in HTML:

1. <strong> or <b>

These elements are used to make text appear in a bold or strong emphasis style. By default, browsers render text within these tags as bold.
<p>This is <strong>bold text</strong> using &lt;strong&gt;.</p>
<p>This is <b>bold text</b> using &lt;b&gt;.</p>
In the example above, both <strong> and <b> tags are used to create bold text within paragraphs.

2. <em> or <i>

These elements are used to make text appear in an italic or emphasized style. Browsers typically render text within these tags as italicized.
<p>This is <em>italic text</em> using &lt;em&gt;.</p>
<p>This is <i>italic text</i> using &lt;i&gt;.</p>
In the example above, both <em> and <i> tags are used to create italic text within paragraphs.

It's important to note that the choice between <strong> and <em> is not purely visual. The <strong> element is used to indicate strong importance, while <em> represents emphasis. By default, both elements apply bold and italic styles respectively, but their actual rendering may vary depending on the browser or CSS styles applied.

Additionally, CSS can be used to customize the appearance of bold and italic text, providing more control over styling, such as font weight, font style, and color.

Underline Text

Underlining text in HTML is a way to visually emphasize or highlight specific portions of your content. HTML provides the <u> element to represent underlined text. However, it's worth noting that underlining text for general emphasis is not recommended according to web design best practices, as underlines are commonly associated with hyperlinks.

Here's an example of how to underline text in HTML:

<p>This is <u>underlined text</u> using the &lt;u&gt; tag.</p>
In the example above, the <u> element is used to enclose the text "underlined text," indicating that it should be rendered with an underline style.

It's important to use underlining sparingly and with clear intent, as users generally expect underlined text to be clickable links. In most cases, it's recommended to use alternative techniques, such as using bold or italic styles, changing font color, or applying other visual cues to convey emphasis or importance in your content.

If you wish to add underlines specifically for hyperlinks, it's better to use the <a> element (anchor) along with appropriate CSS styles for link presentation. The default rendering of anchor elements typically includes underlining.

<p>This is a <a href="#">link</a> with an underline.</p>
In this example, the text "link" is enclosed within an <a> element, creating a hyperlink. The default styling for links often includes underlining, which is typically controlled via CSS.

Strikethrough Text

Strikethrough text formatting in HTML allows you to visually indicate that a portion of your content has been deleted, discontinued, or is no longer valid. HTML provides multiple elements that can be used to represent strikethrough text, including the <s> and <strike> elements.

Here's an example of how to use strikethrough text in HTML:

<p>This is <s>strikethrough text</s> using the &lt;s&gt; tag.</p>
<p>This is <strike>strikethrough text</strike> using the &lt;strike&gt; tag.</p>
In the example above, both the <s> and <strike> elements are used to enclose the text "strikethrough text," indicating that it should be rendered with a strikethrough effect.

It's worth noting that the choice of using <s> or <strike> doesn't have any functional difference, as both elements are treated the same by browsers. The selection can depend on personal preference or adherence to specific HTML standards.

Strikethrough text is commonly used to indicate deleted or outdated information, such as in a revision history, price changes, or crossed-out options. However, it's important to use strikethrough text sparingly and with clear intention, as excessive use may make the content harder to read or understand.

As with other text formatting options, the visual appearance of strikethrough text can be further customized using CSS, allowing you to control aspects such as the color, line thickness, or other styling properties.

Subscript and Superscript Text

Subscript and superscript text formatting in HTML allows you to display text in a smaller size and lower position (subscript) or higher position (superscript) than the regular text. This formatting is commonly used to represent mathematical equations, chemical formulas, footnotes, and other specialized notations.

HTML provides the <sub> and <sup> elements to denote subscript and superscript text respectively.

Here's an example of how to use subscript and superscript text in HTML:

<p>H<sub>2</sub>O represents water.</p>
<p>x<sup>2</sup> indicates the square of x.</p>

In the example above, the <sub> element is used to represent subscript text, where "2" is positioned below the baseline, and the <sup> element is used to represent superscript text, where "2" is positioned above the baseline.

It's important to note that subscript and superscript text formatting alters the size and vertical alignment of the text but doesn't affect the semantic meaning. The actual visual representation may vary depending on the browser or CSS styles applied to the elements.

It's worth mentioning that when using subscript or superscript text for mathematical or scientific expressions, you may also consider using specialized libraries or MathML for more complex equations.

If you need to apply subscript or superscript to individual characters or small portions of text within a larger block, you can also use CSS styles:

<p>This is <span style="vertical-align: sub;">subscript</span> and <span style="vertical-align: super;">superscript</span> using CSS styles.</p>

In the above example, the <span> element is used with the CSS vertical-align property set to "sub" or "super" to achieve subscript and superscript text respectively.

CSS allows further customization of subscript and superscript text, such as font size, vertical alignment, and other styling properties, providing more control over the appearance and layout of the text.

Preformatted Text

Preformatted text in HTML is used to display text exactly as it appears in the HTML code, preserving white spaces, line breaks, and indentation. It is commonly used for displaying code snippets, poetry, ASCII art, or any content that requires precise formatting.

In HTML, the <pre> element is used to enclose preformatted text.

Here's an example of how to use preformatted text in HTML:

    This is an example of preformatted text.
    It maintains white spaces,
    line breaks,
    and indentation.

In the example above, the <pre> element is used to enclose the block of text. The text is rendered exactly as it appears between the opening <pre> and closing </pre> tags, preserving the formatting.

Key points to note about preformatted text:

a. Whitespace Preservation

Preformatted text keeps the exact spacing, indentation, and line breaks defined in the HTML source code. This is especially useful when displaying code or content with significant formatting requirements.

b. Monospace Font

By default, preformatted text is displayed using a monospace font, such as Courier or Consolas. This ensures that each character occupies the same amount of horizontal space, making it suitable for displaying code snippets.

c. Escape HTML Entities

Since preformatted text retains whitespace and line breaks, care must be taken with HTML entities. For example, to display the less-than sign ("<") within preformatted text, it should be escaped as &lt;.

d. Scrollbars

If the preformatted text exceeds the available space, the browser may display horizontal or vertical scrollbars to allow users to navigate through the content.

It's important to use the <pre> element appropriately, reserving it for cases where maintaining exact spacing and formatting is necessary. For general text content, it's recommended to use regular paragraphs or appropriate block-level elements, allowing the browser to handle the text flow and responsive layout automatically.


Blockquotes in HTML are used to visually distinguish quoted or cited content from the surrounding text. They provide a way to indicate that a specific portion of the content is a quotation, excerpt, or citation from another source. Blockquotes are particularly useful for displaying attributed text, such as famous quotes, excerpts from articles, or testimonials.

In HTML, the <blockquote> element is used to enclose blockquoted content. Here's an example of how to use blockquotes:

  <p>This is the quoted text.</p>
    <cite>Author or source of the quote</cite>

In the example above, the <blockquote> element is used to define the blockquoted content. The actual quoted text is enclosed within the <p> (paragraph) tags. The <footer> element is optional but commonly used to provide additional information about the quote, such as the author or the source of the quote. The <cite> element is used to indicate the author or source.

When rendered by the browser, blockquoted content is typically indented or visually set apart from the surrounding text, giving it a distinctive appearance.

Blockquotes can also be nested, allowing for multiple levels of quotations within one another:

  <p>This is the first level of quoting.</p>
    <p>This is the second level of quoting.</p>

In this example, the outer <blockquote> element represents the first level of quoting, while the inner <blockquote> element represents the second level of quoting.

It's worth noting that the appearance of blockquotes can be further customized using CSS, allowing you to adjust aspects such as margins, borders, background color, and font styles to match the design of your website.

Blockquotes provide a clear visual indication of quoted content, allowing readers to differentiate it from the rest of the text and understand its attribution.

Line Breaks

Line breaks in HTML are used to insert a line break within a block of text, forcing the content to start on a new line. Line breaks are useful when you want to add vertical spacing or create a new line without creating a new paragraph or block-level element.

In HTML, the <br> element is used to insert a line break.

Here's an example of how to use line breaks in HTML:

<p>This is the first line.<br>This is the second line.</p>

In the example above, the <br> element is placed between the two lines of text within the <p> (paragraph) element. When rendered by the browser, it creates a line break, resulting in the first line and second line appearing on separate lines.

Key points to note about line breaks:

  1. Self-Closing Element: The <br> element is self-closing, meaning it doesn't have a closing tag. It can be written as <br> or <br/>. Both forms are valid in HTML.
  2. Inline Element: The <br> element is an inline element, which means it doesn't create a new block-level container. It is typically used within block-level elements like paragraphs, headings, or within text content.
  3. Spacing and Styling: Line breaks add a small amount of vertical spacing between lines, but the exact spacing can be further adjusted using CSS. By default, browsers provide some vertical spacing between lines when <br> elements are used.
It's important to use line breaks judiciously and in appropriate contexts. Generally, line breaks should be used when you want to break a line within a paragraph or block of text, but not necessarily create a new paragraph. For larger structural changes in content layout or separation, consider using appropriate block-level elements like <p>, <div>, or other semantic elements.

Additionally, for more complex text formatting or alignment, CSS properties like margins, padding, or flexbox/grid layout can provide better control and consistency.

Posting Komentar

Posting Komentar