HTML

HTML

HTML Basic Examples

HTML (HyperText Markup Language) is the core language used to build and structure web pages. It provides the foundation for creating everything from simple content to complex layouts.

Below are HTML Basic Examples that showcase essential HTML elements, attributes, and tags, along with real-world use cases and clear explanations to help you understand how these pieces fit together in creating functional, well-structured web pages. Whether you're just starting out or looking to refresh your skills, these examples will give you a solid foundation in HTML.

HTML Tags (Example)

Tags are the symbols used to define the start and end of an HTML element. They come in pairs for most elements (like <h1> and </h1>), but some elements are self-closing, such as <br> for a line break.

Example:

<h1>Welcome to HTML Basics</h1>
<br>

HTML Elements (Example)

HTML elements are the basic components of a webpage, made up of a start tag, content, and an end tag. They define how content is displayed and organized.

Example:

<p>This is a paragraph element.</p>

In this example, the <p> tag marks the start of the paragraph, "This is a paragraph element." is the content, and </p> closes the element.

HTML Attributes (Example)

Attributes add extra functionality or information to HTML elements. They are always included in the opening tag and typically come in name/value pairs, like src="value" or alt="description".

Example:

<img src="sitepoint-logo.jpg" alt="SitePoint Logo">

Here, src defines the image's source file, and alt provides alternative text, which improves accessibility.


Combining HTML Elements, Attributes, and Tags (Example)

<a href="https://www.sitepoint.com" target="_blank">Visit SitePoint
  <img src="sitepoint-logo.png" alt="Visit SitePoint">
</a>

In this example, the <a> tag uses href to link to SitePoint's website and target="_blank" to open the link in a new tab. The <img> tag inside the link has attributes src and alt.

HTML Documents Structure

An HTML document is built around a few essential components: the document type declaration (<!DOCTYPE>), the <html> element as the root, the <head> for important metadata, and the <body> where all the visible content lives.

Basic HTML Tags for Document Structure

Tag Description
<!DOCTYPE> Declares the document type and version of HTML
<html> The root element that wraps all other elements
<head> Holds metadata, like the title and links to CSS
<body> Contains the visible content displayed on the page

These tags work together to structure the entire document and define how the page should behave and display content.

<!DOCTYPE> Declaration

The <!DOCTYPE> declaration defines the document type and version of HTML being used. It must appear only once at the top of the page, before any other HTML tags, to ensure the browser interprets the content correctly. This declaration is not case-sensitive.

For HTML5, the <!DOCTYPE> declaration looks like this:

<!DOCTYPE html>

This simple declaration ensures modern browsers handle your content according to the latest standards.

HTML Hierarchy

HTML relies on a nested structure where elements are placed inside one another, creating a clear content hierarchy.

<div>
  <h1>Title</h1>
  <p>This is a paragraph inside a div.</p>
</div>

This nesting not only organizes your content but also helps with layout and styling down the road.

HTML Headings

Headings are essential for creating a clear structure on your page, with six levels ranging from <h1> to <h6>. Use <h1> for the main title and move down in levels for subheadings.

<h1>Main Heading</h1>
<h2>Subheading</h2>

Headings help both users and search engines understand the hierarchy and importance of the content on your page.

HTML Paragraphs and Breaks

The <p> tag defines paragraphs, separating blocks of text, while <br> adds a simple line break without starting a new paragraph.

<p>This is a paragraph.</p>
<p>Line 1<br>Line 2</p>

Paragraphs are your go-to for organizing content, and <br> is handy for breaking text when you need a quick shift.

Use the <a> HTML tag to create links to other pages or resources. The href attribute defines where the link will take users.

<a href="https://www.sitepoint.com/">Visit SitePoint</a>

Links are key to building navigation and connecting users to more content across the web.

HTML Images

To embed images, use the <img> tag with attributes like src to define the image source and alt to describe the image, ensuring accessibility.

<img src="sitepoint.jpg" alt="SitePoint.com">

Including descriptive alt text is crucial for accessibility and helps search engines understand your images.

How to View HTML Source

Curious about how a webpage is structured? You can easily view the HTML source code in just a few steps. Here's how:

  1. Right-click on the webpage. Anywhere on the page (except images or links), right-click with your mouse.
  2. Select "View Page Source". This option will open a new tab showing the full HTML code of the page, including any CSS or JavaScript files linked to it.
    • On Chrome, Firefox, and Edge, this option is labeled as "View Page Source."
    • On Safari, you may need to enable "Show Develop menu" in the Safari Preferences under the Advanced tab.
  3. For more detailed inspection. Right-click and select "Inspect" or "Inspect Element". This opens the browser's Developer Tools, allowing you to:
    • Examine the HTML of specific elements.
    • See live updates as you hover over elements.
    • Check how HTML interacts with CSS and JavaScript in real-time.

Viewing the source is a great way to learn HTML structure, troubleshoot errors, or even see how other developers build their pages.

FAQs on HTML Basic Examples

How do I start using HTML as a beginner?

The best way to start learning HTML is by getting hands-on with the basic building blocks. Begin by practicing with essential tags like <p> for paragraphs, <h1> for headings, <a> for links, and <img> for images. These tags cover the most common elements you'll see on any webpage. Once you're comfortable with these, you can expand to more complex elements and attributes. Don’t be afraid to experiment—HTML is very forgiving, and the more you try, the faster you'll learn!

What is the basic structure of an HTML tag?

HTML tags are straightforward. Each tag is enclosed in angle brackets: <tag>content</tag>. The opening tag starts the element, the content goes between the tags, and the closing tag (which includes a forward slash, like </tag>) marks the end. Some tags are self-closing, like <img>, but most follow this structure. Mastering this concept is key to understanding how HTML organizes content on a webpage.

Is HTML hard to learn?

Not at all! HTML is one of the most beginner-friendly languages out there. It’s all about structure, and once you grasp the basic tags and how they work together, the rest falls into place. You won’t need to memorize a ton of rules or syntax, making it ideal for those just getting started in web development.

What's the difference between HTML and CSS?

Think of HTML as the foundation of your webpage—it provides the structure and content. CSS, on the other hand, is the design layer that brings your HTML to life. While HTML defines elements like headings, paragraphs, and images, CSS controls how they look—fonts, colors, spacing, and layout. Together, they make a complete webpage, with HTML handling the content and CSS managing the presentation.