Chapter 210m

HTML Basic

Learn basic HTML with practical examples of Headings, Paragraphs, Links, Images, Buttons, Attributes, and complete HTML Pages.

In the previous chapter, you learned how to create, save, and open an HTML File using a Code Editor.

Now let's explore some basic HTML Examples!

HTML becomes easier to understand when you practice writing Code and observing its Output.

In this chapter, you will learn how to create a basic HTML Document and add common Elements such as Headings, Paragraphs, Links, Images, and Buttons.

You will also learn how to view HTML Source Code and understand the difference between HTML Tags and Attributes.

A Basic HTML Document

Every HTML Page starts with a basic Document Structure.

Example:

html
<!DOCTYPE html>
<html>
<head>
    <title>My First HTML Page</title>
</head>
<body>
    <h1>Welcome to W3PrimeCode</h1>
    <p>This is my first HTML page.</p>
</body>
</html>

Example Explained

CodeDescription
\<!DOCTYPE html>Declares that the Document uses HTML5.
\<html>Defines the Root Element of the HTML Document.
\<head>Contains Document Metadata.
\<title>Defines the Browser Tab Title.
\<body>Contains the main visible Page Content.
\<h1>Defines a top-level Heading.
\<p>Defines a Paragraph.

Important: The content inside <body> is generally what users see on the Web Page.

HTML Headings

HTML Headings are used to define titles and section headings on a Web Page.

HTML provides six levels of Headings, from <h1> to <h6>.

  • <h1> represents the highest-level Heading.
  • <h6> represents the lowest-level Heading.

Example:

html
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>

Actual Heading sizes depend on the Browser's default Styles or the CSS applied to the Page.

Remember: Choose Heading Levels according to the structure and hierarchy of your Content, not simply to make text larger or smaller.

HTML Paragraphs

The <p> Element defines a Paragraph.

A Paragraph is commonly used to display blocks of text, such as descriptions, explanations, or introductions.

Example:

html
<p>This is my first paragraph.</p>
<p>This is my second paragraph.</p>

Browsers normally display Paragraphs as separate blocks with default spacing.

You can create as many Paragraphs as needed.

HTML Links

HTML Links allow users to navigate from one Web Page to another.

Links are created using the <a> Element.

Basic Syntax:

html
<a href="URL">Link Text</a>

Example:

html
<a href="https://www.example.com">
    Visit Example Website
</a>

Example Explained

  • <a> defines a Hyperlink.
  • href is an Attribute that specifies the Link Destination.
  • Visit Example Website is the clickable Link Text.
  • </a> closes the Element.

Important: Always use meaningful Link Text so users can understand where the Link will take them.

HTML Images

The <img> Element is used to embed an Image in an HTML Page.

Unlike many HTML Elements, <img> is a Void Element and does not require an End Tag.

Basic Syntax:

html
<img src="image.jpg" alt="Image description">

Example:

html
<img
    src="https://www.w3.org/Icons/w3c_home"
    alt="W3C Logo"
    width="100"
>

Expected Output:

The Browser displays the Image if the specified Image Resource is available.

Example Explained

AttributeDescription
srcSpecifies the Image Location.
altProvides alternative text describing the Image.
widthSpecifies the displayed width of the Image.

**Why is alt important?**

The alt Attribute provides a text alternative when an Image cannot be displayed and helps users of assistive technologies understand the Image's purpose.

For informative Images, write meaningful alternative text.

HTML Buttons

The <button> Element creates a Button on a Web Page.

Example:

html
<button type="button">Click Me</button>

Important: This Button does not perform a custom action by itself. You can use JavaScript to define what should happen when a user clicks it.

You will learn more about interactive Buttons in later tutorials.

HTML Line Breaks

The <br> Element inserts a Line Break within text.

It is a Void Element and does not need a Closing Tag.

Example:

html
<p>
    Hello World!<br>
    Welcome to HTML.
</p>

Use <br> when a Line Break is part of the Content, such as in an address or poem.

Do not use repeated <br> Elements simply to create spacing between Page Sections. CSS is better suited for Layout and Spacing.

HTML Horizontal Rules

The <hr> Element represents a thematic break between sections of Content.

Browsers commonly display it as a horizontal line.

Example:

html
<h2>HTML Tutorial</h2>
<p>Learn HTML step by step.</p>

<hr>

<h2>CSS Tutorial</h2>
<p>Learn how to style web pages.</p>

HTML Attributes

HTML Attributes provide additional information or configuration for Elements.

They are normally written inside the Start Tag.

Example:

html
<a href="https://www.example.com">Visit Website</a>

Here, href is an Attribute.

Another Example:

html
<p title="This is a tooltip">
    Move your mouse over this paragraph.
</p>

The title Attribute can provide additional advisory information. Many desktop Browsers display it as a Tooltip when the user hovers over the Element.

General Syntax:

html
<tagname attribute="value">Content</tagname>

Most Attributes use a Name and Value.

You will study HTML Attributes in greater detail in a later chapter.

How to View HTML Source Code

You can inspect the HTML Source Code of many Web Pages using your Browser.

Method 1: View Page Source

On most desktop Browsers:

  1. Open a Web Page.
  2. Right-click an empty area.
  3. Select View Page Source.

You can also commonly use:

  • Windows/Linux: Ctrl + U
  • macOS: Option + Command + U in supported Browsers

This displays the HTML Source returned for the Page.

Method 2: Inspect Element

Modern desktop Browsers include Developer Tools.

To inspect a specific Element:

  1. Right-click the Element.
  2. Select Inspect.
  3. Examine the HTML in the Elements Panel.

Note: View Page Source and Inspect are not exactly the same. View Page Source typically shows the original HTML Response, while Inspect displays the current DOM, which JavaScript may have modified.

Complete Practical Example

Let's combine several HTML Elements into one Web Page.

Example:

html
<!DOCTYPE html>
<html>
<head>
    <title>My HTML Practice</title>
</head>
<body>

    <h1>Welcome to My Website</h1>

    <p>I am learning HTML with W3PrimeCode.</p>

    <h2>About HTML</h2>

    <p>HTML is used to structure web content.</p>

    <hr>

    <h2>Useful Link</h2>

    <a href="https://www.example.com">
        Visit Example Website
    </a>

    <h2>Sample Image</h2>

    <img
        src="https://www.w3.org/Icons/w3c_home"
        alt="W3C Logo"
        width="100"
    >

    <h2>Button Example</h2>

    <button type="button">Click Me</button>

</body>
</html>

Expected Output:

The Browser displays:

  • A main Heading.
  • Two informational Paragraphs.
  • Section Headings.
  • A thematic break.
  • A clickable Link.
  • An Image, if the Resource loads successfully.
  • A Button.

This Example demonstrates how multiple HTML Elements can work together to create a simple Web Page.

Common Beginner Mistakes

Mistake 1: Forgetting the Closing Tag

Incorrect:

html
<h1>Welcome
<p>Hello World</p>

Correct:

html
<h1>Welcome</h1>
<p>Hello World</p>

Mistake 2: Forgetting Quotes Around Attribute Values

Although HTML allows some unquoted Attribute Values, using quotes consistently is recommended.

Recommended:

html
<a href="https://www.example.com">Visit Website</a>

Mistake 3: Missing Image Alternative Text

Not Recommended:

html
<img src="photo.jpg">

Recommended for an informative Image:

html
<img src="photo.jpg" alt="A mountain landscape">

Mistake 4: Using Heading Elements Only to Change Font Size

Headings describe the Content Hierarchy. Use CSS when you want to control Font Size and Appearance.

Try It Yourself

Create a File named basic-examples.html.

Use the following Starter Code:

html
<!DOCTYPE html>
<html>
<head>
    <title>HTML Practice</title>
</head>
<body>

    <h1>My First Website</h1>

    <p>Write your introduction here.</p>

    <h2>My Favorite Website</h2>

    <a href="https://www.example.com">
        Visit Website
    </a>

</body>
</html>

Your Tasks:

  1. Change the main Heading.
  2. Add two new Paragraphs.
  3. Add another Heading using <h2>.
  4. Create a Link to a Website of your choice.
  5. Add an Image using <img>.
  6. Add a Button.
  7. Save the File and view the Output in your Browser.