Education
E-Learning
HTML Advanced Topics: A Beginner Guide
by sabari on | 2024-12-17 12:05:29 Last Updated by sabari on | 2024-12-17 13:26:51
Share: Facebook |
Twitter |
Whatsapp |
Linkedin Visits: 36
Section 1: HTML Data Attributes (Custom Data Storage)
What It is Used For:
- Store custom data directly on HTML elements.
- Enable JavaScript to access and manipulate this data
dynamically.
- Useful for creating dynamic web applications.
Key Concepts:
- Data Attributes Syntax:
<div data-user-id="123" data-role="admin">John Doe</div>
- Accessing Data Attributes in JavaScript:
const user = document.querySelector("[data-user-id]");
console.log(user.dataset.userId); // Output: 123
Practice:
- Exercise:
Create a list of products with data-price and data-category attributes. Write JavaScript to filter items by
category.
Mini-Project:
Interactive Shopping
List: Create a shopping list
where items have data-price attributes. Add a
feature to calculate the total cost dynamically.
Section 2: HTML <template> Element
What It is Used For:
- Define reusable HTML fragments that are not rendered
until used.
- Helps with dynamic content creation in JavaScript.
Key Concepts:
- Template Syntax:
<template id="card-template">
<div class="card">
<h2></h2>
<p></p>
</div>
</template>
- Cloning Templates in JavaScript:
const template = document.getElementById("card-template").content.cloneNode(true);
template.querySelector("h2").textContent = "Title";
template.querySelector("p").textContent = "Description";
document.body.appendChild(template);
Practice:
- Exercise:
Create a template for a user card and use JavaScript to populate it with
user data.
Mini-Project:
Dynamic Team Roster: Build a dynamic team page where user profiles
are generated using templates.
Section 3: HTML <noscript> Element
What It is Used For:
- Provide fallback content for users with JavaScript
disabled.
Key Concepts:
- Basic Example:
<noscript>
<p>JavaScript is required to view this page.</p>
</noscript>
- Usage in Forms or Analytics:
<noscript>
<img src="fallback-tracking.gif" alt="Analytics">
</noscript>
Practice:
- Exercise:
Create a webpage with a <noscript> message explaining why JavaScript is needed.
Mini-Project:
Fallback Newsletter
Signup: Create a form that
works even if JavaScript is disabled.
Section 4: HTML <picture> Element (Responsive Images)
What It is Used For:
- Serve different images based on screen size or
resolution.
Key Concepts:
- Basic Syntax:
<picture>
<source srcset="image-large.jpg" media="(min-width: 768px)">
<source srcset="image-small.jpg" media="(max-width: 767px)">
<img src="image-default.jpg" alt="Example">
</picture>
Practice:
- Exercise:
Use the <picture> element to serve a high-resolution image for desktops
and a lower-resolution image for mobile devices.
Mini-Project:
Responsive Gallery: Create a responsive image gallery using the <picture> element.
Section 5: HTML <source> Element (Media)
What It is Used For:
- Specify multiple media sources for <audio> or <video> elements.
Key Concepts:
- Example with <video>:
<video controls>
<source src="video.mp4" type="video/mp4">
<source src="video.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
- Example with <audio>:
<audio controls>
<source src="audio.mp3" type="audio/mpeg">
<source src="audio.ogg" type="audio/ogg">
</audio>
Practice:
- Exercise:
Create a video player with multiple source formats for compatibility.
Mini-Project:
Media Showcase: Build a page that
showcases audio and video content with fallback options.Section 6: HTML <track> Element (Subtitles)
What It is Used For:
- Add subtitles or captions to <video>.
Key Concepts:
- Basic Syntax:
<video controls>
<source src="movie.mp4" type="video/mp4">
<track src="subtitles-en.vtt" kind="subtitles" srclang="en" label="English">
</video>
- Creating .vtt Subtitle Files:
WEBVTT
00:00:01.000 --> 00:00:05.000
Hello, welcome to the video!
00:00:05.001 --> 00:00:10.000
Lets learn HTML subtitles.
Practice:
- Exercise:
Add subtitles in two languages to a video.
Mini-Project:
Accessible Video Player: Create a video player with subtitles and an
option to switch languages.
Section 7: HTML Web Components (Custom
Elements, Shadow DOM)
What It is Used For:
- Create reusable custom HTML
elements with encapsulated styles and functionality.
- Avoid conflicts with global
styles or scripts by using the Shadow DOM.
Key Concepts:
1. Custom
Elements:
Define new HTML elements.
class MyElement extends HTMLElement {
connectedCallback() {
this.innerHTML = `<p>Hello, Im a custom element!</p>`;
}
}
customElements.define("my-element", MyElement);
Usage in HTML:
<my-element></my-element>
2. Shadow
DOM:
Encapsulate styles and DOM structure within a
custom element.
class ShadowElement extends HTMLElement {
constructor() {
super();
const shadow = this.attachShadow({ mode: "open" });
shadow.innerHTML = `
<style>
p { color: blue; }
</style>
<p>Shadow DOM content!</p>
`;
}
}
customElements.define("shadow-element", ShadowElement);
Usage in HTML:
<shadow-element></shadow-element>
Practice:
- Exercise: Create a
user-card
custom element with a
shadow DOM that displays a name and profile picture.
Mini-Project:
Reusable Alert Box: Build a web component for an alert box with custom styles
and dismiss functionality.
Section 8: HTML Inline SVG
What It is Used For:
- Embed vector graphics
directly in HTML.
- SVGs are scalable,
lightweight, and great for icons or illustrations.
Key Concepts:
1. Basic
Syntax:
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>
2. Styling
SVGs with CSS:
<style>
.icon { fill: green; stroke: black; stroke-width: 2; }
</style>
<svg class="icon" width="50" height="50">
<rect width="50" height="50"></rect>
</svg>
3. Using
<use>
to
Reuse SVG Symbols:
<svg>
<symbol id="icon-star" viewBox="0 0 24 24">
<path d="..."></path>
</symbol>
</svg>
<svg><use href="#icon-star"></use></svg>
Practice:
- Exercise: Create an SVG illustration
of a house with shapes like rectangles, circles, and polygons.
Mini-Project:
SVG Icon Library: Design a set of reusable SVG icons and embed them in a
webpage.