HTML Essentials
This tutorial covers the fundamental building blocks of HTML, essential for creating web pages.
What is HTML?
HTML (HyperText Markup Language) is the standard markup language for documents designed to be displayed in a web browser. It defines the meaning and structure of web content.
Think of HTML as the skeleton of a webpage. It provides the structure and content, while CSS styles it, and JavaScript adds interactivity.
Basic Structure of an HTML Document
Every HTML document has a basic structure:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First Webpage</title>
<!-- Links to stylesheets, scripts, etc. go here -->
</head>
<body>
<h1>Hello, World!</h1>
<p>This is a paragraph.</p>
</body>
</html>
Key Elements Explained:
<!DOCTYPE html>
: Declares the document type and version of HTML (HTML5 in this case).<html>
: The root element of every HTML page. Thelang="en"
attribute specifies the language.<head>
: Contains meta-information about the HTML document, such as its title, character set, and links to external resources.<meta charset="UTF-8">
: Specifies the character encoding for the document, which is crucial for displaying text correctly.<meta name="viewport" content="width=device-width, initial-scale=1.0">
: Configures the viewport for responsive design, ensuring proper scaling on different devices.<title>
: Sets the title that appears in the browser's title bar or tab.<body>
: Contains the visible content of the HTML document.
Common HTML Tags
Headings
Headings are used to define titles and subtitles. There are six levels, from <h1>
(most important) to <h6>
(least important).
Example:
<h1>Main Title</h1>
<h2>Section Title</h2>
<h3>Subsection Title</h3>
Paragraphs
The <p>
tag defines a paragraph of text.
Example:
<p>This is a sample paragraph. It contains some text that will be displayed as a block on the webpage.</p>
<p>This is another paragraph, separated from the first one.</p>
Links
The <a>
tag (anchor) creates hyperlinks to other web pages or resources. The href
attribute specifies the URL.
Example:
<a href="https://www.microsoft.com">Visit Microsoft's Website</a>
<a href="#section-id">Jump to Section</a> <!-- Internal link -->
Images
The <img>
tag embeds an image into the HTML document. The src
attribute specifies the image URL, and the alt
attribute provides alternative text for accessibility.
Example:
<img src="images/logo.png" alt="Company Logo" width="150" height="50">
Note: Attributes like width
and height
are often handled better with CSS for responsive design.
Lists
HTML supports ordered (<ol>
) and unordered (<ul>
) lists. List items are defined with the <li>
tag.
Unordered List:
<ul>
<li>Item One</li>
<li>Item Two</li>
<li>Item Three</li>
</ul>
Ordered List:
<ol>
<li>First Step</li>
<li>Second Step</li>
<li>Third Step</li>
</ol>
Semantic HTML
Semantic HTML uses elements that clearly describe their meaning to both the browser and the developer. This improves accessibility and SEO.
<header>
, <nav>
, <main>
, <article>
, <section>
, <aside>
, <footer>
.
Attributes
HTML elements can have attributes that provide additional information about the element. Attributes are always specified in the start tag and usually come in name/value pairs like name="value"
.
Example:
<a href="https://www.example.com" target="_blank">Link</a>
<!--
href: Specifies the URL of the page the link goes to.
target="_blank": Opens the linked document in a new tab window.
-->
Nesting Elements
HTML elements can be nested within each other. For example, a <p>
element can be nested inside a <div>
element.
Example:
<div class="content-block">
<h2>About Us</h2>
<p>We are a company dedicated to innovation and technology.</p>
</div>
Next Steps
Now that you have a grasp of HTML essentials, you can move on to styling your web pages with CSS or adding dynamic behavior with JavaScript.