custom elements
Define new element names such as lesson-card.
Learn Web Components with practical examples for rich HTML features, native browser behavior, accessibility, performance, SEO and live practice.
HTML advanced
Web Components are a set of browser standards for custom elements, shadow DOM and templates.
They can be useful when you need reusable interface pieces without depending on a large framework. A custom element can package behavior and markup behind a normal-looking tag.
They are powerful, but not always the first tool a beginner needs. Start with semantic HTML, then use Web Components when reuse and encapsulation solve a real problem.
Define new element names such as lesson-card.
Encapsulates markup and style inside a component.
Stores inert HTML that JavaScript can clone.
Callbacks run when elements connect or update.
Syntax and behavior
Custom element names must include a hyphen, such as course-card or lesson-timer.
<lesson-card title="HTML Advanced"></lesson-card> <script> customElements.define("lesson-card", class extends HTMLElement { connectedCallback() { this.textContent = this.getAttribute("title"); } }); </script>
<x-text>Just a paragraph</x-text> <script> // A p element would have been clearer. </script>
HTML quick reference
Use these patterns when you need the syntax quickly. Each example has its own anchor, so search engines and readers can land directly on the exact pattern instead of only at the top of the lesson.
A clean version of the markup from this lesson. Use it when you need the correct HTML shape quickly.
<lesson-card title="HTML Advanced"></lesson-card> <script> customElements.define("lesson-card", class extends HTMLElement { connectedCallback() { this.textContent = this.getAttribute("title"); } }); </script>
Meaningful markup that stays understandable before CSS and JavaScript are added.
The starting point from the practice lab. Change the HTML first, then use CSS only for presentation.
<main class="demo-card"> <h1>Custom element</h1> <lesson-pill text="Advanced HTML"></lesson-pill> </main> <script> customElements.define("lesson-pill", class extends HTMLElement { connectedCallback() { this.innerHTML = `<strong>${this.getAttribute("text")}</strong>`; } }); </script> <style> body { margin: 0; min-height: 100vh; display: grid; place-items: center; font-family: Inter, system-ui, sans-serif; background: #07111f; color: white; } .demo-card { width: min(820px, calc(100% - 32px)); padding: 34px; border-radius: 24px; background: #101a2d; box-shadow: 0 30px 80px rgba(0, 0, 0, 0.3); } h1 { margin: 0 0 16px; font-size: clamp(34px, 7vw, 58px); line-height: 1; } h2 { margin: 22px 0 10px; } p, li { color: #cfd8e6; line-height: 1.7; } button { border: 0; border-radius: 999px; padding: 12px 16px; background: #8cffc1; color: #07111f; font-weight: 900; } a { color: #8cffc1; font-weight: 900; } lesson-pill { display: inline-block; border-radius: 999px; padding: 12px 16px; background: #8cffc1; color: #07111f; } </style>
A complete practice snippet that shows how the HTML behaves in context.
A weak pattern from the lesson. Use it as a warning sign when reviewing real pages.
<x-text>Just a paragraph</x-text> <script> // A p element would have been clearer. </script>
A recognizable mistake you can search for and refactor.
Rules that matter
These elements can create rich interfaces, but they still need clear purpose, safe fallbacks and production discipline.
Custom elements require names such as app-card or user-menu.
A custom element does not automatically become a button, heading or landmark.
Encapsulation helps, but can complicate styling and accessibility.
Think about what appears before JavaScript runs.
Simple HTML does not need a custom element wrapper.
Attributes, events and slots should be clear to future developers.
Production thinking
This matters because Web Components can make reusable UI cleaner, but they do not replace the need to understand real HTML.
Custom components must still expose correct roles, labels, focus behavior and keyboard interaction.
Web Components can work well across projects, but require careful browser testing and design-system discipline.
Content generated only after JavaScript may be less reliable for crawlers. Critical content should be available in the initial HTML when possible.
Live code lab
Edit the HTML or CSS, then use Run to refresh the preview. The preview is isolated, so links and forms stay inside this practice area.
Mini assignment
Practice assignment
Try it yourself
Self-check
Do not only read this page. Answer these questions out loud or write the answers in your own notes. If one answer feels vague, revisit the examples before moving on.