Comprehensive HTML Guide

Everything you need to know about HTML from basics to advanced concepts

HTML Basics

What is HTML?

HTML (HyperText Markup Language) is the standard markup language for creating web pages. It describes the structure of web pages using markup.

Note: HTML is not a programming language; it's a markup language that defines the structure of your content.

HTML Document Structure

Every HTML document should follow this basic structure:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document Title</title>
</head>
<body>
    <!-- Page content goes here -->
</body>
</html>
  • <!DOCTYPE html> - Declares the document type and HTML version
  • <html> - The root element of an HTML page
  • <head> - Contains meta information about the document
  • <body> - Contains the visible page content

HTML Elements and Tags

HTML elements are the building blocks of HTML pages, represented by tags.

Basic Syntax:

<tagname>Content goes here...</tagname>

Void Elements (Self-closing tags):

<img src="image.jpg" alt="Description">
<br>
<input type="text">

Note: While <img> and <br> are valid with or without a trailing slash (/), the slash is optional in HTML5.

Common HTML Elements

Element Description Example
<h1> to <h6> Heading elements (h1 is most important) <h1>Main Heading</h1>
<p> Paragraph <p>This is a paragraph.</p>
<a> Anchor (link) <a href="https://example.com">Link</a>
<img> Image <img src="image.jpg" alt="Description">
<ul>, <ol>, <li> Unordered list, Ordered list, List item <ul><li>Item 1</li></ul>
<div> Division/section <div>Content</div>
<span> Inline container <span>Inline content</span>

HTML Attributes

Attributes provide additional information about HTML elements.

Common Attributes:

  • id - Specifies a unique id for an element
  • class - Specifies one or more class names for an element
  • src - Specifies the source for embedded content
  • href - Specifies the URL for links
  • alt - Specifies alternative text for images
  • style - Specifies inline CSS
  • title - Specifies extra information about an element

Boolean Attributes:

Boolean attributes don't need a value. Their presence implies true, their absence implies false.

<input type="checkbox" checked>
<button disabled>Click me</button>

Semantic HTML

What is Semantic HTML?

Semantic HTML introduces meaning to the web page rather than just presentation. It makes web pages more informative and adaptable.

Benefits: Better accessibility, improved SEO, easier to maintain, and more consistent code.

Semantic Elements

Element Description
<header> Represents introductory content or navigational aids
<nav> Contains navigation links
<main> Contains the main content of the document
<article> Represents self-contained composition
<section> Represents a standalone section
<aside> Represents content only indirectly related to the main content
<footer> Represents a footer for its nearest sectioning content
<figure> and <figcaption> Represents self-contained content with an optional caption
<time> Represents a specific period in time
<mark> Represents text highlighted for reference purposes

Example of Semantic Structure:

<header>
    <h1>Website Title</h1>
    <nav>
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
        </ul>
    </nav>
</header>

<main>
    <article>
        <h2>Article Title</h2>
        <p>Article content...</p>
    </article>
    
    <aside>
        <h3>Related Links</h3>
        <ul>
            <li><a href="#">Link 1</a></li>
        </ul>
    </aside>
</main>

<footer>
    <p>Copyright © 2023</p>
</footer>

Semantic Text Elements

Element Description Example
<em> Emphasized text (typically italic) This is <em>important</em>
<strong> Important text (typically bold) <strong>Warning!</strong>
<blockquote> Block-level quotation <blockquote cite="source">Quote</blockquote>
<q> Inline quotation He said <q>Hello</q>
<cite> Title of a work <cite>The Great Gatsby</cite>
<abbr> Abbreviation or acronym <abbr title="World Wide Web">WWW</abbr>
<code> Computer code <code>console.log()</code>
<pre> Preformatted text <pre>Formatted text</pre>
<kbd> Keyboard input Press <kbd>Ctrl</kbd>+<kbd>C</kbd>
<var> Variable <var>x</var> = 5
<samp> Sample output <samp>Hello World</samp>
<time> Date/time <time datetime="2023-10-05">Oct 5</time>

HTML Forms

Basic Form Structure

Forms are used to collect user input and send it to a server for processing.

<form action="/submit" method="POST">
    <!-- Form controls go here -->
</form>
  • action - Specifies where to send the form data
  • method - Specifies the HTTP method (GET or POST)
  • enctype - Specifies how form data should be encoded (important for file uploads)

Form Controls

Text Input

<label for="name">Name:</label>
<input type="text" id="name" name="name" required>

Email Input

<label for="email">Email:</label>
<input type="email" id="email" name="email" required>

Password Input

<label for="password">Password:</label>
<input type="password" id="password" name="password" required>

Radio Buttons

<fieldset>
    <legend>Gender</legend>
    <label><input type="radio" name="gender" value="male"> Male</label>
    <label><input type="radio" name="gender" value="female"> Female</label>
</fieldset>

Checkboxes

<label><input type="checkbox" name="subscribe" checked> Subscribe to newsletter</label>

Select Dropdown

<label for="country">Country:</label>
<select id="country" name="country">
    <option value="us">United States</option>
    <option value="ca">Canada</option>
</select>

Textarea

<label for="message">Message:</label>
<textarea id="message" name="message" rows="4"></textarea>

Buttons

<button type="submit">Submit</button>
<button type="reset">Reset</button>
<button type="button">Regular Button</button>

Form Attributes

Attribute Description
required Specifies that an input field must be filled out
placeholder Specifies a short hint that describes the expected value
pattern Specifies a regular expression that the input's value is checked against
min, max Specifies the minimum and maximum values for number inputs
step Specifies the legal number intervals for number inputs
autocomplete Specifies whether a form should have autocomplete on or off
readonly Specifies that an input field is read-only
disabled Specifies that an input field should be disabled
multiple Specifies that the user is allowed to enter more than one value

HTML5 Input Types

HTML5 introduced several new input types for better input control and validation:

Type Description Example
color Color picker <input type="color">
date Date picker <input type="date">
datetime-local Date and time without timezone <input type="datetime-local">
email Email address <input type="email">
file File upload <input type="file">
month Month and year <input type="month">
number Numeric input <input type="number" min="1" max="10">
range Slider control <input type="range" min="0" max="100">
search Search field <input type="search">
tel Telephone number <input type="tel">
time Time input <input type="time">
url URL input <input type="url">
week Week and year <input type="week">

Multimedia in HTML

Images

The <img> element is used to embed images in web pages.

<img src="image.jpg" alt="Description" width="500" height="300">
  • src - Specifies the path to the image
  • alt - Provides alternative text for screen readers and when images can't be displayed
  • width and height - Specify dimensions (in pixels)
  • loading - Can be set to "lazy" for lazy loading

Important: Always include the alt attribute for accessibility. Use an empty string (alt="") for decorative images.

Responsive Images

HTML provides ways to serve different images based on device characteristics.

srcset Attribute

<img src="small.jpg" 
     srcset="medium.jpg 1000w, large.jpg 2000w" 
     alt="Responsive image">

picture Element

<picture>
    <source media="(min-width: 800px)" srcset="large.jpg">
    <source media="(min-width: 500px)" srcset="medium.jpg">
    <img src="small.jpg" alt="Responsive image">
</picture>

Audio

The <audio> element is used to embed sound content.

<audio controls>
    <source src="audio.mp3" type="audio/mpeg">
    <source src="audio.ogg" type="audio/ogg">
    Your browser does not support the audio element.
</audio>

Attributes:

  • controls - Shows audio controls (play, pause, volume)
  • autoplay - Audio will play as soon as it's ready
  • loop - Audio will start over when finished
  • muted - Audio will be muted initially
  • preload - Specifies if and how the audio should be loaded when the page loads

Video

The <video> element is used to embed video content.

<video width="320" height="240" controls>
    <source src="movie.mp4" type="video/mp4">
    <source src="movie.ogg" type="video/ogg">
    Your browser does not support the video tag.
</video>

Attributes:

  • controls - Shows video controls
  • autoplay - Video will play as soon as it's ready
  • loop - Video will start over when finished
  • muted - Video will be muted initially
  • poster - Specifies an image to show while the video is downloading
  • preload - Specifies if and how the video should be loaded when the page loads

Iframes

The <iframe> element is used to embed another HTML page.

<iframe src="https://example.com" 
        title="Example Site" 
        width="800" 
        height="600">
</iframe>

Common Uses:

  • Embedding videos from YouTube, Vimeo, etc.
  • Embedding maps from Google Maps
  • Embedding social media content
  • Embedding third-party widgets

Security Considerations:

  • Use the sandbox attribute to restrict what the iframe can do
  • Consider using the allow attribute to specify permissions
  • Be cautious with third-party content due to security risks

HTML Tables

Basic Table Structure

Tables are used to display tabular data.

<table>
    <caption>Monthly Savings</caption>
    <thead>
        <tr>
            <th>Month</th>
            <th>Savings</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>January</td>
            <td>$100</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td>Total</td>
            <td>$100</td>
        </tr>
    </tfoot>
</table>

Table Elements:

  • <table> - Defines the table
  • <caption> - Defines a table caption
  • <thead> - Groups the header content
  • <tbody> - Groups the body content
  • <tfoot> - Groups the footer content
  • <tr> - Defines a table row
  • <th> - Defines a header cell
  • <td> - Defines a standard cell

Table Attributes

Attribute Description
colspan Specifies the number of columns a cell should span
rowspan Specifies the number of rows a cell should span
scope Specifies whether a header cell is for a column, row, or group

Example with colspan and rowspan:

<table>
    <tr>
        <th rowspan="2">Name</th>
        <th colspan="2">Contact</th>
    </tr>
    <tr>
        <th>Phone</th>
        <th>Email</th>
    </tr>
</table>

Accessible Tables

For complex tables, use these techniques to improve accessibility:

  • Use <th> elements for headers
  • Add scope attributes to headers (scope="col" or scope="row")
  • Use <caption> to describe the table's purpose
  • For complex relationships, use headers and id attributes

Note: Avoid using tables for layout purposes. Tables should only be used for tabular data.

Meta Tags and SEO

Common Meta Tags

Meta tags provide metadata about the HTML document.

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Free web tutorials">
<meta name="keywords" content="HTML, CSS, JavaScript">
<meta name="author" content="John Doe">
<meta http-equiv="refresh" content="30">

Important Meta Tags:

  • charset - Specifies the character encoding
  • viewport - Controls layout on mobile browsers
  • description - Used by search engines for page description
  • keywords - Specifies keywords for search engines
  • author - Specifies the author of the page
  • refresh - Refreshes the page after specified seconds
  • robots - Controls search engine crawling and indexing

Open Graph Protocol

Open Graph meta tags control how content appears when shared on social media.

<meta property="og:title" content="Page Title">
<meta property="og:type" content="website">
<meta property="og:url" content="https://example.com/page">
<meta property="og:image" content="https://example.com/image.jpg">
<meta property="og:description" content="Description of page">
<meta property="og:site_name" content="Site Name">
<meta property="og:locale" content="en_US">

Twitter Cards

Twitter-specific meta tags for enhanced display when shared on Twitter.

<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@username">
<meta name="twitter:creator" content="@author">
<meta name="twitter:title" content="Page Title">
<meta name="twitter:description" content="Page Description">
<meta name="twitter:image" content="https://example.com/image.jpg">

Favicons

Favicons are small icons displayed in browser tabs and bookmarks.

<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<link rel="manifest" href="site.webmanifest">

Modern favicon setup includes multiple sizes and formats:

<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">

Accessibility (a11y)

WCAG Principles

Web Content Accessibility Guidelines (WCAG) are based on four principles:

  1. Perceivable - Information must be presentable in ways users can perceive
  2. Operable - Interface components must be operable
  3. Understandable - Information and operation must be understandable
  4. Robust - Content must be robust enough to be interpreted reliably

Semantic HTML for Accessibility

Using semantic HTML elements improves accessibility:

  • Use proper heading structure (h1-h6)
  • Use <nav> for navigation
  • Use <main> for main content
  • Use <article> for self-contained content
  • Use <section> for thematic grouping
  • Use <aside> for tangential content
  • Use <footer> for footer information

ARIA (Accessible Rich Internet Applications)

ARIA attributes enhance accessibility when semantic HTML isn't sufficient.

Common ARIA Attributes:

  • role - Defines the type of element (e.g., role="navigation")
  • aria-label - Provides a label for an element
  • aria-labelledby - References another element that labels this one
  • aria-hidden - Hides an element from screen readers
  • aria-live - Indicates dynamic content that should be announced
  • aria-current - Indicates the current item in a set

Example:

<button aria-label="Close">X</button>
<nav role="navigation" aria-label="Main">...</nav>
<div role="alert" aria-live="assertive">Error message</div>

Accessibility Best Practices

  • Always provide text alternatives for non-text content (alt attributes)
  • Ensure sufficient color contrast (minimum 4.5:1 for normal text)
  • Don't rely solely on color to convey information
  • Make all functionality available from a keyboard
  • Give users enough time to read and use content
  • Help users navigate and find content
  • Make text readable and understandable
  • Make content appear and operate in predictable ways
  • Help users avoid and correct mistakes
  • Ensure compatibility with current and future user tools

Testing Accessibility

Automated Tools:

  • WAVE (Web Accessibility Evaluation Tool)
  • axe DevTools
  • Lighthouse (built into Chrome DevTools)
  • HTML_CodeSniffer

Manual Testing:

  • Keyboard navigation testing
  • Screen reader testing (NVDA, VoiceOver, JAWS)
  • Zoom testing (200% zoom)
  • Color contrast checking

HTML Best Practices

General Best Practices

  • Always declare the document type (<!DOCTYPE html>)
  • Use semantic HTML elements
  • Close all HTML elements properly
  • Use lowercase for element and attribute names
  • Quote attribute values
  • Include alt attributes for images
  • Define the language of the document (lang attribute)
  • Use UTF-8 character encoding
  • Keep CSS and JavaScript in separate files
  • Validate your HTML using the W3C validator

Performance Considerations

  • Minify HTML, CSS, and JavaScript
  • Optimize images (correct format, compression, dimensions)
  • Use lazy loading for images and iframes (loading="lazy")
  • Preload critical resources
  • Use efficient CSS selectors
  • Defer non-critical JavaScript
  • Implement caching strategies
  • Use CDNs for common libraries

Security Best Practices

  • Use HTTPS
  • Implement Content Security Policy (CSP)
  • Use Subresource Integrity (SRI) for CDN resources
  • Sanitize user input to prevent XSS
  • Use rel="noopener noreferrer" with target="_blank"
  • Set secure cookie attributes (Secure, HttpOnly, SameSite)
  • Implement CSRF protection
  • Use security headers (X-Frame-Options, X-XSS-Protection, etc.)

Mobile Considerations

  • Use the viewport meta tag
  • Design for touch interactions (adequate touch targets)
  • Optimize for vertical space
  • Consider mobile performance (limited bandwidth, processing power)
  • Test on real devices
  • Implement responsive design
  • Consider mobile-first approach
  • Optimize for portrait and landscape modes

HTML Resources