HTML, or HyperText Markup Language, is the core language for creating and structuring content on the web. HTML is the building block for web pages, allowing developers to organize text, images, videos, and other elements that users see and interact with in their browsers. If you are just starting as a developer, it is important to know how to learn HTML like a pro.
Why Learn HTML
Learning HTML is fundamental for anyone interested in web development because it provides the foundation for creating websites. Understanding HTML allows you to:
- Build Web Pages: With HTML, you can create static web pages, organize content, and make it accessible to users.
- Add Structure to Content: HTML tags provide structure, enabling search engines to read and understand the content on your site better.
- Facilitate Further Learning: HTML knowledge is essential for learning other web technologies, such as CSS (for styling) and JavaScript (for interactivity). It’s the first step to becoming proficient in web development and understanding the “language of the web.”
- Boost Career Opportunities: Knowing HTML is valuable for web developers and roles in content management, digital marketing, and UX design.
This guide offers a step-by-step approach to learning HTML, making it suitable for anyone who understands how the web works and how to build websites.
How to Learn HTML: Step By Step Guide
Step 1: Understand the Basics of HTML
What is HTML?
The first step in understanding how to learn HTML is to know what HTML is and its basic structure.
HTML stands for HyperText Markup Language. It is the standard language used for creating and designing web structures. It was initially developed in 1991 by Tim Berners-Lee and has since evolved into a powerful tool for web development, especially with the release of HTML5, the latest version.
HTML Structure
HTML documents follow a basic structure that outlines how a web page is displayed in a browser. Below are some key structural elements:
- <!DOCTYPE html>: Declares the document type and HTML version used. It ensures the browser interprets the content correctly. In this case, HMTL5.
- <html>: This tag wraps all HTML content on the page.
- <head>: Contains meta-information about the document, including the title, links to CSS files, and other essential metadata. This section doesn’t display content but provides information crucial for SEO, accessibility, and page loading.
- <body>: Holds all the visible content on the page, such as text, images, and videos. It is the main area for displaying content in the browser.
Code Example:
<!DOCTYPE html>
<html>
<head>
<title>Sample Page</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<p>This is an introductory paragraph.</p>
</body>
</html>
Common HTML Tags
HTML uses tags to mark up content, providing structure and meaning to the elements on the page. Some essential HTML tags include:
- <h1> – <h6>: Heading tags, ranging from <h1> (most important) to <h6> (least important), define headings and subheadings.
- <p>: Paragraph tag, used to add blocks of text.
- <a>: Anchor tag, used to create hyperlinks. It has an href attribute pointing to the linked page’s URL.
- <div>: Division tag, a container element used to group content, useful for layout and styling.
- <img>: Image tag is used to display images. It includes src (source) and alt (alternative text) attributes.
Code Example
<body>
<h1>Introduction to HTML</h1>
<p>HTML is the foundation of web development.</p>
<a href="https://www.example.com">Visit Example</a>
<div>
<img src="image.jpg" alt="Sample Image">
</div>
</body>
Step 2: Set Up Your Environment to Understand How to Learn HTML
Choosing a Code Editor
A code editor is used to write and edit HTML code. Some of the most popular editors, ideal for beginners and advanced users alike, include:
Visual Studio Code (VS Code)
VS Code is a free, open-source editor developed by Microsoft. It has extensive features like syntax highlighting, code auto-completion, and an integrated terminal.
Download and install VS Code from the official website. To start, open a new file, save it with an .html extension, and begin coding. You can customize the editor’s look and add extensions like “HTML Snippets” for convenience.
Sublime Text
This code editor is known for its speed and simplicity. Sublime Text is a versatile editor with a lightweight design. It offers a distraction-free writing environment, a powerful search tool, and customizable keyboard shortcuts.
Download and install Sublime Text from its official site. Open a new file, save it with a .html extension, and write HTML code.
Atom
This editor was developed by GitHub and is another free, open-source editor with great customization options. Atom provides multi-platform support, various HTML packages, and the ability to work collaboratively.
Download Atom from its website, open a new file, and save it with an .html extension. To enhance HTML coding, you can add packages like “Emmet” for quick HTML and CSS coding shortcuts.
Step 3. Write Your First HTML Page
Creating your first HTML page is an exciting step in learning HTML. Below is a step-by-step guide to writing a basic HTML page, including instructions on structuring content with headings and paragraphs, creating links, adding images, and building lists.
Creating a Basic HTML Page
- Open Your Code Editor: Open your preferred code editor (e.g., VS Code, Sublime Text, or Atom).
- Create a New File: Start a new file and save it with the .html extension (e.g., index.html).
- Write the Basic Structure: Type the following code to set up the basic HTML structure:
<!DOCTYPE html>
<html>
<head>
<title>My First HTML Page</title>
</head>
<body>
<!-- Your content will go here -->
</body>
</html>
This structure includes:
- <!DOCTYPE html>: Specifies the HTML5 version.
- <html>: Wraps all HTML content.
- <head>: Contains metadata, including the <title> of the page, which displays on the browser tab.
- <body>: Holds the visible content for the page.
Adding Headings and Paragraphs
HTML offers a range of heading tags, from <h1> to <h6>, for structuring titles and subheadings. The <p> tag is used for paragraphs of text.
Example of code
<body>
<h1>Welcome to My First HTML Page</h1>
<h2>About This Page</h2>
<p>This is my first attempt at creating a web page using HTML. It’s simple, but it’s a great starting point!</p>
</body>
In this example:
- <h1> and <h2> create a main title and a subtitle, respectively.
- <p> adds a paragraph of text.
Creating Links and Adding Images
Hyperlinks and images are essential components of any webpage.
- Creating Links: Create a clickable link using the tag <a>. The href attribute defines the URL the link points to.
Example 1:
<p>Check out my <a href="https://www.example.com">favorite website</a> for more information!</p>
- Adding Images: Use the <img> tag to display images. The src attribute specifies the image’s location and the alt attribute provides alternative text for screen readers.
Example 2:
<img src="image.jpg" alt="A beautiful scenery">
Building Lists
Lists help organize information in an ordered (numbered) or unordered (bullet-point) format.
- Ordered List (<ol>): Uses the <ol> tag for a numbered list.
Example
<ol>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
</ol>
- Unordered List (<ul>): Use the <ul> tag for a bullet-point list.
Example:
<ul>
<li>Item one</li>
<li>Item two</li>
<li>Item three</li>
</ul>
Each list item is wrapped in a <li> tag whether they are in ordered or unordered lists.
Putting It All Together
Here’s a complete example with headings, paragraphs, links, images, and lists:
<!DOCTYPE html>
<html>
<head>
<title>My First HTML Page</title>
</head>
<body>
<h1>Welcome to My First HTML Page</h1>
<h2>About This Page</h2>
<p>This page is created using HTML. Here, you’ll find some basics to get you started.</p>
<h2>Useful Resources</h2>
<p>Check out <a href="https://www.example.com">this website</a> for more HTML tutorials.</p>
<h2>My Favorite Image</h2>
<img src="scenery.jpg" alt="A beautiful scenery">
<h2>My Top 3 HTML Elements</h2>
<ol>
<li>Headings</li>
<li>Paragraphs</li>
<li>Links</li>
</ol>
<h2>Other Notable Elements</h2>
<ul>
<li>Images</li>
<li>Lists</li>
<li>Divisions</li>
</ul>
</body>
</html>
This setup provides a solid foundation for building and customizing your HTML pages with various elements for structure and style.
Step 4: Learn HTML Attributes and Styling
In this section, you’ll learn about HTML attributes, basic styling with inline CSS, and how to use classes and IDs for more organized styling.
Understanding HTML Attributes
HTML attributes provide additional information about elements, often in the form of values. Here are some commonly used attributes:
- href: Used in <a> tags to define the URL of a hyperlink.
Example:
<a href="https://www.example.com">Visit Example</a>
- src: Used in <img> and <iframe> tags to specify the source file’s URL.
Example:
<img src="image.jpg" alt="A sample image">
- alt: Used in <img> tags to provide alternative text for screen readers and as a fallback if the image doesn’t load.
Example:
<img src="image.jpg" alt="A beautiful scenery">
title: Adds a tooltip text when a user hovers over an element.
Example:
<p title="This is a paragraph">Hover over this text</p>
Attributes are written inside the opening tag and consist of a name and value pair, like attribute=”value”.
Basic Styling with Inline CSS
Inline CSS allows you to add styles directly to HTML tags using the style attribute. Although it’s not ideal for larger projects, it’s helpful for simple customizations or quick styling.
- Syntax: style=”property: value;”
Example:
<h1 style="color: blue; font-size: 24px;">Welcome to My Website</h1>
<p style="font-family: Arial; color: gray;">This is a paragraph with inline styling.</p>
In this example:
- color: blue; changes the text color to blue.
- font-size: 24px; sets the font size to 24 pixels.
- font-family: Arial; color: gray; changes the font to Arial and sets the color to gray.
Inline styling helps test quick styles, but for larger projects, consider using an external CSS file to keep your code clean and maintainable.
HTML Classes and IDs
Classes and IDs are attributes used to assign custom styling to specific or group elements. They make organising and applying styles across a webpage easier using CSS.
- Classes:
- Purpose: A class is a reusable style that can be applied to multiple elements.
- Syntax: class=”classname”
- CSS Example: .classname { property: value; }
Example code:
<p class="highlight">This paragraph has a custom style.</p>
<p class="highlight">This paragraph shares the same style as above.</p>
CSS code
.highlight {
color: green;
font-weight: bold;
}
- IDs:
- Purpose: An ID is a unique identifier meant for a single element.
- Syntax: id=”uniqueID”
- CSS Example: #uniqueID { property: value; }
Example code
<p id="special">This paragraph has a unique style.</p>
CSS:
#special {
color: red;
font-size: 18px;
}
Note: IDs should only be used once per page, while classes can be applied to multiple elements. Both classes and IDs are essential for organized styling, especially in larger projects where different page parts require specific designs.
Example Using Attributes, Inline Styling, Classes, and IDs
Here’s how attributes, inline styling, classes, and IDs come together in HTML:
<!DOCTYPE html>
<html>
<head>
<title>HTML Styling Example</title>
<style>
.highlight {
color: green;
font-style: italic;
}
#special {
color: red;
font-size: 20px;
}
</style>
</head>
<body>
<h1 style="color: blue;">Welcome to My Webpage</h1>
<p class="highlight">This paragraph has a class applied to it.</p>
<p class="highlight">This paragraph also uses the "highlight" class.</p>
<p id="special">This paragraph uses a unique ID for styling.</p>
<p>Visit <a href="https://www.example.com" title="Example Website">this website</a> for more details.</p>
<img src="image.jpg" alt="An example image" style="border: 2px solid black;">
</body>
</html>
This example combines inline styling, classes, and IDs to demonstrate multiple ways to effectively style and organize HTML content
Step 5 of How to Learn HTML: Learn HTML Forms and Input Elements
HTML forms are essential to web development because they allow users to input and submit data. This section covers the basics of HTML forms, input types, and how to use forms to collect data.
Introduction to HTML Forms
HTML forms are created using the <form> element, which acts as a container for input elements like text boxes, buttons, and labels.
Forms make it possible to gather user data and submit it to a server.
- <form>: Defines a form to collect user input.
- <input>: Creates various input fields.
- <button>: Adds a clickable button for submitting the form.
- <label>: Describes each input field to improve accessibility.
Example of a basic form structure:
<form action="/submit-form" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<button type="submit">Submit</button>
</form>
- action: Specifies the URL where the form data will be sent.
- method: Defines the HTTP method for submitting the form data (commonly GET or POST).
Common Input Types
HTML offers various input types to collect specific data, enhancing user experience and data accuracy.
- Text (type=”text”): Allows users to enter single-line text.
- Example 1:
<input type="text" name="username">
- Example 1:
- Email (type=”email”): Validates that the input is a properly formatted email address.
- Example 2:
<input type="email" name="email">
- Example 2:
- Number (type=”number”): Restricts input to numbers only.
- Example 3:
<input type="number" name="age">
- Example 3:
- Password (type=”password”): Hides input characters for privacy.
- Example 4:
<input type="password" name="password">
- Example 4:
- Radio (type=”radio”): Allows users to select one option from a group.
- Example 5:
<input type="radio" name="gender" value="male">
<input type="radio" name="gender" value="female">
6. Checkbox (type=”checkbox”): Enables multiple selections from a group of options.
-
- Example 6:
<input type="checkbox" name="hobby" value="sports">
<input type="checkbox" name="hobby" value="music">
7. Date (type=”date”): Provides a calendar interface for date selection.
-
- An Example:
<input type="date" name="birthdate">
- An Example:
Using Forms to Collect Data
HTML forms allow data submission to a server for processing. The data can be sent using either the GET or POST method:
- GET: Appends data to the URL, making it visible. Best for non-sensitive data.
- POST: Sends data securely without displaying it in the URL, suitable for sensitive information.
Example of submitting form data:
<form action="/submit" method="POST">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<button type="submit">Register</button>
</form>
In this example:
- Each <input> field has a name attribute that identifies it for data submission.
- The required attribute ensures users can’t submit the form without filling in these fields.
- type=”submit” on the button triggers the form submission.
Example: Complete Registration Form
Here’s a more detailed example of using multiple input types to create a basic registration form:
<!DOCTYPE html>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<h2>User Registration</h2>
<form action="/submit-registration" method="POST">
<label for="firstname">First Name:</label>
<input type="text" id="firstname" name="firstname" required><br><br>
<label for="lastname">Last Name:</label>
<input type="text" id="lastname" name="lastname" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<label for="gender">Gender:</label><br>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br><br>
<label for="birthdate">Birthdate:</label>
<input type="date" id="birthdate" name="birthdate"><br><br>
<button type="submit">Register</button>
</form>
</body>
</html>
This example demonstrates using a variety of form elements, including text fields, email fields, radio buttons, and a date picker, giving you the tools to gather a wide range of data from users.
Step 6: Learn Html Layout and Semantic Elements
Semantic HTML5 tags give meaning to web page elements, helping browsers and developers understand the structure and content of a webpage more quickly. Unlike generic <div> and <span> tags, semantic tags describe the role of each part of the page, enhancing both readability and accessibility.
Introduction to Key Semantic Elements
- <header>: Defines the header section of a page or section, typically containing the logo, navigation links, or introductory content.
<header>
<h1>Website Title</h1>
<nav>...navigation links...</nav>
</header>
- <footer>: Marks the footer section of a page, often including contact information, copyright details, or links to other relevant resources.
Example:
<footer>
<p>© 2024 MyWebsite</p>
</footer>
- <section>: Represents a distinct section of content, such as a chapter in an article or a different theme within the page.
<section>
<h2>About Us</h2>
<p>Details about the company...</p>
</section>
- <article>: Indicates self-contained content, like blog posts or news articles, which can stand alone outside the page’s context.
Example code
<article>
<h2>Latest News</h2>
<p>This is a news article...</p>
</article>
- <nav>: Contains navigation links for the site or page.
Example of code:
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#services">Services</a></li>
</ul>
</nav>
Organizing Page Layout with <div> and <span>
Though not semantic, <div> and <span> are flexible elements used for layout and grouping purposes:
- <div>: A block-level element used to create larger page sections, making it easy to style and organize.
Example code
<div class="content-section">
<h2>Our Services</h2>
<p>Details about services offered...</p>
</div>
- <span>: An inline element, mainly used to style a small section within other elements without disrupting the line flow.
Example code
<p>This is an <span class="highlight">important</span> word in the paragraph.</p>
Use <div> for larger containers and sections and <span> for styling within text.
Responsive Design Basics
Responsive design ensures a webpage looks and functions well across devices of various sizes, from desktop monitors to mobile screens. HTML and CSS play essential roles in creating responsive layouts.
- Media Queries: CSS media queries allow specific styling rules based on screen size or orientation.
Example code
/* Default style for desktops */
body {
font-size: 16px;
}
/* Style for screens less than 600px wide */
@media (max-width: 600px) {
body {
font-size: 14px;
}
}
- Flexible Grids: CSS Flexbox or CSS Grid layouts make it easy to design flexible and adaptable layouts that adjust based on screen size.
Flexbox Example:
.container {
display: flex;
flex-wrap: wrap;
}
.item {
flex: 1 1 300px; /* Flexibility to shrink, grow, and basis of 300px */
}
- Responsive Images: Using the max-width property or the picture element to adapt images for various screen sizes.
Example:
img {
max-width: 100%;
height: auto;
}
With semantic HTML, <div> and <span> organization, and responsive design techniques, you can create clean, accessible, and adaptable web layouts.
Step 6: Enroll for a Course
Sometimes, it’s good to just take a course and benefit from the advantages of what a curriculum offers. A curriculum offers beginners a structured approach to studying, making it easier to understand programming. In addition, at any given time, tutors will help explain concepts you might not understand on your own at any given time.
Feel free to enroll in an HTML course at Allthingsprogramming.com.