HTML Basics

Foundational concepts
1. What is HTML and what does it stand for?

HTML stands for HyperText Markup Language. It is not a programming language but a markup language used to create the structure and content of web pages.

HTML uses a system of tags (like <p>, <h1>, <div>) to tell browsers how to display content. It's the foundation of every website โ€” browsers read HTML and render it visually.

Key points:

  • Markup language, not a programming language
  • Uses tags to structure content
  • Works with CSS for styling and JavaScript for interactivity
  • Every web page at its core is HTML
2. What is the purpose of DOCTYPE?

<!DOCTYPE html> is a declaration that appears at the very beginning of an HTML document. It tells the browser that the document is an HTML5 file.

Purpose:

  • Ensures browsers render the page in standards mode (not quirks mode)
  • Without DOCTYPE, older browsers may render inconsistently
  • Must be the first line of the HTML document
  • It is not a tag itself โ€” it's a document declaration

Example: <!DOCTYPE html> (for HTML5)

3. Explain the difference between an element and a tag.

Tag: The HTML syntax โ€” the angle brackets and keywords. Examples: <p>, </p>, <div>.

Element: The complete unit consisting of an opening tag, content, and a closing tag. Example: <p>Hello World</p> is one paragraph element.

In short:

  • Tags are the markup syntax (opening and closing)
  • Elements include the tags plus the content
  • Example: In <h1>Title</h1>, the tags are <h1> and </h1>, and the entire thing is the h1 element
4. What is the DOM (Document Object Model)?

The DOM is a tree-like representation of the HTML document that the browser creates after parsing the HTML. It represents all elements, attributes, and text as objects that can be accessed and manipulated.

Key points:

  • Created by the browser when parsing HTML
  • Represents the structure of the page as a tree of nodes
  • Each HTML element becomes a node in the tree
  • JavaScript can interact with the DOM to change the page dynamically
  • Not the same as HTML โ€” HTML is the source, DOM is the live representation

Example: document.getElementById('myId') accesses the DOM to find an element with that ID.

5. What is a semantic HTML element? Give examples.

Semantic HTML elements have meaning โ€” they clearly describe their purpose to both the browser and the developer.

Non-semantic: <div> and <span> have no inherent meaning โ€” they're just containers.

Semantic examples:

  • <header> โ€” Page header or section header
  • <nav> โ€” Navigation links
  • <main> โ€” Main content area
  • <article> โ€” Independent article or post
  • <section> โ€” Thematic grouping of content
  • <aside> โ€” Sidebar or related content
  • <footer> โ€” Footer of page or section

Benefits: Better SEO, accessibility, readability, and maintainability.

Elements & Attributes

Working with tags and properties
6. What is the difference between block and inline elements?

Block elements: Take up the full width available, stretching left and right. They always start on a new line and have top/bottom margins.

  • Examples: <div>, <p>, <h1>, <header>, <ul>

Inline elements: Only take up as much width as necessary. They flow within text and don't create new lines.

  • Examples: <span>, <a>, <strong>, <em>, <img>

Inline-block: Behaves like an inline element but can have width and height. Created with CSS: display: inline-block;

7. What are void elements in HTML?

Void elements are self-closing elements that don't have content or a closing tag. They represent an element entirely by a single tag.

Common void elements:

  • <img> โ€” Embeds an image
  • <br> โ€” Line break
  • <hr> โ€” Horizontal rule
  • <input> โ€” Input field
  • <meta> โ€” Metadata
  • <link> โ€” Link to external resources

Note: In HTML5, the trailing slash is optional: <img> or <img /> are both valid.

8. What is an attribute? Give examples of commonly used attributes.

An attribute provides additional information about an element. It is always included in the opening tag and typically has a name and value.

Syntax: <element attribute="value">

Common attributes:

  • id โ€” Unique identifier for an element
  • class โ€” Assigns one or more CSS classes
  • src โ€” Specifies the source of an image or script
  • href โ€” URL destination for links
  • alt โ€” Alternative text for images
  • title โ€” Tooltip text on hover
  • placeholder โ€” Placeholder text in input fields
  • disabled โ€” Disables a form element
9. What are data attributes and how are they used?

Data attributes (also called data-* attributes) allow you to store custom data privately within HTML elements.

Syntax: Any attribute starting with data- followed by a custom name.

Example: <div data-user-id="42" data-role="admin">

Usage:

  • Store metadata without cluttering your HTML
  • Pass data to JavaScript for dynamic behavior
  • Don't affect page rendering or styling (unless targeted by CSS)
  • Accessed in JavaScript: element.dataset.userId returns "42"

Benefits: Keeps HTML clean, separates concerns, and provides a standard way to embed custom data.

10. What is the difference between id and class attributes?

id attribute:

  • Must be unique within the entire page
  • Used to identify a specific, single element
  • Only one id per element
  • Higher CSS specificity (100 points)
  • Used for JavaScript selection: document.getElementById()

class attribute:

  • Can be used on multiple elements
  • An element can have multiple classes (space-separated)
  • Lower CSS specificity (10 points)
  • Used for styling groups of similar elements
  • More flexible for reusable styles

Best practice: Use id for unique elements and JavaScript, use class for styling and grouping.

Forms & Input

Building interactive forms
11. What are the common input types in HTML5?

HTML5 introduced many new input types that provide better user experience and built-in validation. Here are the most common:

  • text โ€” Single-line text input (default)
  • email โ€” Email address (validates format)
  • password โ€” Hidden text input
  • number โ€” Numeric input with spinner
  • range โ€” Slider for selecting a value
  • date โ€” Date picker
  • time โ€” Time picker
  • checkbox โ€” Multiple selection
  • radio โ€” Single selection from group
  • file โ€” File upload
  • submit โ€” Submit form button
  • button โ€” Generic button

Example: <input type="email" placeholder="user@example.com">

12. What are form validation attributes?

HTML5 provides attributes to validate form input on the client side without JavaScript.

Common validation attributes:

  • required โ€” Field must not be empty
  • pattern โ€” Input must match a regex pattern
  • min and max โ€” For numbers, set minimum and maximum values
  • minlength and maxlength โ€” String length restrictions
  • step โ€” For number/date inputs, defines increment steps

Example: <input type="email" required>

Example: <input type="number" min="1" max="10" required>

Note: Client-side validation is for UX; always validate on the server too for security.

13. What is the <label> element and why is it important?

The <label> element associates text with form inputs. It improves usability and accessibility.

Why it matters:

  • Increases clickable area for checkboxes and radios
  • Screen readers use labels to announce form fields to users with disabilities
  • Improves mobile UX by making touch targets larger
  • Links form elements with descriptive text

Two ways to use labels:

Method 1 (implicit): Wrap the input inside the label

<label>Email: <input type="email"></label>

Method 2 (explicit): Use the for attribute to connect label to input id

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

14. What is the difference between <fieldset> and <legend>?

<fieldset> groups related form controls together and draws a border around them.

<legend> provides a caption or title for the fieldset.

Example:

<fieldset>
<legend>Personal Information</legend>
<label for="name">Name:</label>
<input type="text" id="name">
<label for="email">Email:</label>
<input type="email" id="email">
</fieldset>

Benefits: Better form organization, accessibility (legends are announced by screen readers), and visual grouping.

15. How do you handle form submission and what is the action attribute?

The action attribute specifies where form data is sent when submitted.

Example: <form action="/submit-form" method="POST">

The action attribute:

  • Specifies the URL that processes the form
  • If omitted, the form submits to the current page
  • Works with the method attribute (GET or POST)

GET vs POST:

  • GET โ€” Data appended to URL, visible in browser bar, limited size, use for non-sensitive data
  • POST โ€” Data sent in request body, hidden, no size limit, use for sensitive data like passwords

Complete example:

<form action="/login" method="POST">
<input type="text" name="username" required>
<input type="password" name="password" required>
<button type="submit">Login</button>
</form>

Media & Embeds

Images, video, and external content
16. What is the difference between <img> and <picture> elements?

<img> element: Simple image embed. Displays a single image.

<img src="photo.jpg" alt="A photo">

<picture> element: Provides multiple image sources based on conditions (screen size, format support, etc.). Better for responsive design and modern image formats.

Example:

<picture>
<source media="(max-width: 600px)" srcset="small.jpg">
<source media="(min-width: 601px)" srcset="large.jpg">
<img src="default.jpg" alt="Responsive image">
</picture>

When to use <picture>:

  • Responsive images (different sizes for different screens)
  • Art direction (different crops/compositions for different screens)
  • Modern image formats (WebP with JPEG fallback)
17. Why is the alt attribute important on images?

The alt attribute provides alternative text for images. It's crucial for accessibility and SEO.

Purposes:

  • Screen readers announce the alt text to visually impaired users
  • Displayed if the image fails to load
  • Used by search engines to understand image content
  • Improves SEO and search visibility

Good alt text examples:

  • <img src="cat.jpg" alt="Orange tabby cat sitting on a windowsill"> โœ“

Bad alt text examples:

  • <img src="cat.jpg" alt="image"> โœ— (Not descriptive)
  • <img src="cat.jpg" alt="cat.jpg"> โœ— (Just the filename)

For decorative images: Use empty alt: alt=""

18. How do you embed video in HTML5?

The <video> element embeds video directly in the page without external plugins.

Basic example:

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

Common attributes:

  • controls โ€” Shows play/pause buttons
  • autoplay โ€” Plays automatically (often blocked by browsers)
  • muted โ€” Starts with sound off (required for autoplay in most browsers)
  • loop โ€” Repeats when finished
  • poster โ€” Image shown before video plays

Multiple sources: Browser plays the first supported format, ensuring compatibility.

19. What are the security considerations when embedding iframes?

The <iframe> element embeds another webpage inside your page. It can be a security risk if not handled carefully.

Security concerns:

  • Iframes can be hijacked to display malicious content
  • Malicious scripts inside iframes can access parent page data
  • XSS attacks can inject content into iframes

Security best practices:

  • Always use sandbox attribute to restrict permissions:
    <iframe src="external.html" sandbox></iframe>
  • Use sandbox="allow-scripts allow-forms" to allow specific features
  • Only embed from trusted sources
  • Use HTTPS for all iframe sources
  • Validate and sanitize all user input before embedding

Example with restrictions:
<iframe src="trusted.com" sandbox="allow-scripts allow-same-origin"></iframe>

Advanced Topics

Modern HTML and best practices
20. What is the difference between localStorage, sessionStorage, and cookies?

All three store data on the client side, but they differ in scope, duration, and storage capacity.

Cookies:

  • Sent to server with every HTTP request
  • Can have an expiration date
  • Limited size (4KB)
  • Created in JavaScript or on the server

localStorage:

  • Not sent to server automatically
  • Persists until manually cleared
  • Much larger capacity (~5-10MB)
  • Shared across all tabs/windows of the same origin

sessionStorage:

  • Similar to localStorage but cleared when tab/window closes
  • Not shared between tabs
  • Limited to current session

Use cases: Cookies for authentication, localStorage for preferences, sessionStorage for temporary data.

21. What are Web Components?

Web Components are a set of APIs that allow you to create custom, reusable HTML elements with encapsulated styles and behavior.

Three main parts:

  • Custom Elements: Define your own HTML tags
  • Shadow DOM: Encapsulates styles and markup
  • HTML Templates: Define template content

Example of a custom element:

<my-button>Click Me</my-button>

JavaScript to define it:

Use the Custom Elements API to register a new element with custom behavior and styling.

Benefits:

  • Reusable across projects
  • Encapsulation prevents CSS conflicts
  • True component-based development
  • Works with any framework or vanilla JavaScript
22. What is Shadow DOM and why is it useful?

Shadow DOM is a scoped subsystem of the DOM that allows you to attach a hidden DOM tree to an element.

Purpose:

  • Encapsulates styles โ€” CSS inside Shadow DOM doesn't leak out
  • Encapsulates structure โ€” Hidden from the light DOM
  • Prevents naming conflicts
  • Enables true component reusability

Example: Browser's native elements like <video> use Shadow DOM internally to hide their complex internal structure.

Creating Shadow DOM (simplified):

Use the attachShadow() method to create an isolated DOM tree within an element.

Benefits: True encapsulation, prevents global style pollution, enables reusable components.

23. What is accessibility in HTML and why is it important?

Web accessibility means ensuring that websites are usable by everyone, including people with disabilities. HTML plays a crucial role in making sites accessible.

Accessibility features in HTML:

  • Semantic HTML (<header>, <nav>, <main>) โ€” Screen readers understand structure
  • Alt text on images โ€” Visually impaired users know what's in images
  • <label> elements โ€” Form fields are labeled for screen readers
  • ARIA attributes โ€” Provide additional accessibility information
  • Proper heading hierarchy โ€” Helps users navigate
  • Keyboard navigation support โ€” Users who can't use a mouse can navigate

Why it matters: Legal requirement (ADA, WCAG), ethical responsibility, broader audience, better SEO, better code structure.

24. What are ARIA attributes and how are they used?

ARIA (Accessible Rich Internet Applications) attributes provide additional accessibility information to assistive technologies when semantic HTML isn't enough.

Common ARIA attributes:

  • role โ€” Defines the element's role (role="navigation")
  • aria-label โ€” Provides a label when no text is visible (aria-label="Close menu")
  • aria-labelledby โ€” Links element to its label
  • aria-hidden โ€” Hides element from screen readers (aria-hidden="true")
  • aria-live โ€” Announces dynamic content updates

Example: <button aria-label="Close dialog">ร—</button>

Important note: Use semantic HTML first; ARIA is a supplement when HTML doesn't have a semantic element.

25. How does HTML relate to SEO and what are best practices?

HTML structure strongly influences SEO. Search engines parse HTML to understand content and ranking.

HTML SEO best practices:

  • Proper heading hierarchy: One <h1>, then <h2>, <h3> in order
  • Semantic HTML: Use <header>, <nav>, <main>, <article>, <footer>
  • Meta description: <meta name="description"> shown in search results
  • Title tag: <title> โ€” First thing users see in search results
  • Alt text on images: Helps search engines understand images
  • Proper URL structure: Use clean, descriptive URLs
  • Mobile-friendly: <meta name="viewport"> for responsive design
  • Schema markup: Structured data helps search engines

Example meta tags:
<title>Best HTML Tutorial | Ramayai AI Academy</title>
<meta name="description" content="Learn HTML from scratch...">

Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

Master HTML & CSS.
Build real projects. Get hired.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’