Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      CodeSOD: A Unique Way to Primary Key

      July 22, 2025

      BrowserStack launches Figma plugin for detecting accessibility issues in design phase

      July 22, 2025

      Parasoft brings agentic AI to service virtualization in latest release

      July 22, 2025

      Node.js vs. Python for Backend: 7 Reasons C-Level Leaders Choose Node.js Talent

      July 21, 2025

      The best CRM software with email marketing in 2025: Expert tested and reviewed

      July 22, 2025

      This multi-port car charger can power 4 gadgets at once – and it’s surprisingly cheap

      July 22, 2025

      I’m a wearables editor and here are the 7 Pixel Watch 4 rumors I’m most curious about

      July 22, 2025

      8 ways I quickly leveled up my Linux skills – and you can too

      July 22, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025
      Recent

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025

      Zero Trust & Cybersecurity Mesh: Your Org’s Survival Guide

      July 22, 2025

      Execute Ping Commands and Get Back Structured Data in PHP

      July 22, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025
      Recent

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025

      “I don’t think I changed his mind” — NVIDIA CEO comments on H20 AI GPU sales resuming in China following a meeting with President Trump

      July 22, 2025

      Galaxy Z Fold 7 review: Six years later — Samsung finally cracks the foldable code

      July 22, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»A Brief Introduction to Web Components

    A Brief Introduction to Web Components

    May 9, 2025

    In a previous article, I gave a brief introduction to React. This tutorial introduces an alternative approach to building a component-based frontend. It covers the fundamentals of Web Components to build modular, reusable elements for your web applications.

    Web Components are a set of standardized browser APIs that allow you to create custom, reusable HTML elements with encapsulated functionality. They help developers create self-contained components that can be used across different frameworks or even without any framework at all.

    This tutorial assumes you have some basic programming experience and are comfortable reading and writing JavaScript. You should understand variables, functions, loops, objects, classes, and how JavaScript works in the browser. You don’t need to know anything about Web Components to get started.

    The four lessons presented here are taken from my free book of code playbacks:

    An Introduction to Web Development from Back to Front
    By Mark Mahoney

    This book is available for free on Playback Press. The book is a hands-on guide to modern web development, covering everything from core JavaScript features to building full-stack apps with various tools and technologies.

    Each lesson is presented as a code playback, which is an interactive code walkthrough that shows how a program changes over time along with my explanation about what’s happening. This format helps you focus on the reasoning behind the code changes.

    To view a playback, click on the comments in the left panel. Each comment updates the code in the editor and highlights the change. Read the explanation, study the code, and use the built-in AI tutor if you have questions. Here’s a short video that shows how to use a code playback:

    After this introduction, you might want to explore the official Web Components resources: MDN Web Components Guide.

    Table of Contents

    • Web Components Part 1: Your First Custom Element

    • Web Components Part 2: Data Communication

    • Web Components Part 3: Custom Events

    • Web Components Part 4: Building a Complete App

    • React vs Web Components Comparison

    Web Components Part 1: Your First Custom Element

    This first lesson introduces building user interfaces using Web Components, which let you bundle together HTML, CSS, and JavaScript into a single reusable element.

    These elements can be treated just like built-in HTML elements such as h1, div, and img. They’re particularly useful when you want to break a page into smaller, self-contained, and reusable parts like headers, tables, or forms, while keeping the code for each organized and isolated.

    In this playback, you’ll learn:

    • How to create a JavaScript class that represents a custom HTML element

    • How to access the attributes of the web component

    • How to create and use a web component in HTML

    The lesson focuses on creating a LegendHeader component that can be reused throughout a web application. You’ll see how custom elements can have attributes just like regular HTML elements (such as src in an img tag), and how these attributes can be changed from JavaScript code, triggering methods in response.

    View the playback here: Web Components Part 1- LegendHeader

    Web Components Part 2: Data Communication

    Building on the previous lesson, this playback demonstrates how to create multiple components that share data. I’ll enhance the LegendHeader component to display the count of legends being tracked, and add a new LegendTable component that displays all the CS legends in a database using an HTML table.

    A key concept introduced in this lesson is having a top-level element hold the web app’s data and letting it communicate changes to the components that rely on it. This approach makes component management more organized and maintainable.

    In this playback, you’ll learn:

    • How to create and work with multiple components

    • How to set up and use ‘observable attributes’ in a web component

    • How a top-level element can manage data and inform components when data changes

    View the playback here: Web Components Part 2- LegendTable

    Web Components Part 3: Custom Events

    This lesson expands the application by adding a NewLegendForm component that allows users to add new legends to the database. The playback introduces the concept of custom events that ‘bubble’ up through the DOM, enabling top-level elements to control requests for data.

    You’ll learn why it’s often better for components not to manage app-wide data themselves. Having a top-level element manage the entire web app’s data makes components simpler and more reusable, as they don’t need to know about each other or communicate directly.

    In this playback, you’ll learn:

    • How components can generate custom events to request data instead of managing it themselves

    • How a top-level element can listen for events and handle them

    • How a top-level element accesses data and passes it to components that need it

    View the playback here: Web Components Part 3- NewLegendForm

    Web Components Part 4: Building a Complete App

    In this final lesson, I bring everything together to create a complete application with authentication. I’ll add a new AuthBox component and implement a light authentication system so that only registered, logged-in users can add new legends to the database.

    The playback uses sessions on the server to control user access and reinforces the importance of centralized data management in component-based architecture.

    In this playback, you’ll learn:

    • How to implement user authentication with a web component

    • How to integrate all components into a complete, functional application

    • Best practices for data management in component-based web applications

    View the playback here: Web Components Part 4- AuthBox

    React vs Web Components Comparison

    Some of the main differences between React and web components can be summarized like this:

    Key Property React Web Components
    Component Definition Uses functions (typically) to define components Uses JavaScript classes that extend HTMLElement
    Tooling Requirements Requires tools for installation, transpilation, bundling (npm, webpack, babel, and so on) Native browser support with no build tools or installation required
    Template Syntax Uses JSX, an HTML-like syntax within JavaScript Uses standard HTML in strings or HTML templates
    DOM Updates Uses Virtual DOM to efficiently batch and minimize actual DOM manipulations Directly manipulates the DOM, typically less optimized for frequent updates
    Property Types Accepts various data types as props (strings, arrays, objects, functions) Only accepts strings as attributes in HTML
    Rendering Model Declarative: describe what the UI should look like, React handles updates More imperative: directly manipulate the DOM in response to changes
    Style Encapsulation No built-in style encapsulation (requires CSS-in-JS or CSS Modules) Built-in style encapsulation with Shadow DOM
    Browser Support Works in all browsers via polyfills Modern browsers only (may require polyfills for older browsers)
    Ecosystem Large ecosystem with many libraries and tools Smaller ecosystem, but growing

    Wrapping Up

    These four lessons cover the fundamentals of Web Components, but there’s much more to explore. Web Components provide a standards-based way to create reusable elements without the need for external libraries or frameworks, making them particularly valuable for building maintainable and portable code.

    If you found this format helpful, explore the rest of the book to see how full web apps are built from scratch using modern tools and approaches.

    Web Components represent just one approach to component-based web development. Keep building, keep reading, and try out the other playbacks when you’re ready to go further.

    If you have feedback about the playbacks I’d love to hear from you. You can reach me here mark@playbackpress.com.

    Source: freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleFree GenAI 65-Hour Bootcamp
    Next Article Webkit Word is a text editor built with GTK4/Libadwaita

    Related Posts

    Development

    GPT-5 is Coming: Revolutionizing Software Testing

    July 22, 2025
    Development

    Win the Accessibility Game: Combining AI with Human Judgment

    July 22, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    More old clock settings just moved to Windows 11’s modern Settings app

    Operating Systems

    CVE-2025-49786 – Apache HTTP Server Unvalidated User Input

    Common Vulnerabilities and Exposures (CVEs)

    Business leaders continue to push workers toward daily use of AI

    News & Updates

    CVE-2025-7538 – Campcodes Sales and Inventory System File Upload Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    CVE-2025-5111 – FreeFloat FTP Server TYPE Command Handler Remote Buffer Overflow Vulnerability

    May 23, 2025

    CVE ID : CVE-2025-5111

    Published : May 23, 2025, 2:15 p.m. | 1 hour, 24 minutes ago

    Description : A vulnerability, which was classified as critical, has been found in FreeFloat FTP Server 1.0. Affected by this issue is some unknown functionality of the component TYPE Command Handler. The manipulation leads to buffer overflow. The attack may be launched remotely. The exploit has been disclosed to the public and may be used.

    Severity: 7.3 | HIGH

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    Poll: Has YouTube taken its war on ad blockers too far?

    June 17, 2025

    LLMs Can Now Solve Challenging Math Problems with Minimal Data: Researchers from UC Berkeley and Ai2 Unveil a Fine-Tuning Recipe That Unlocks Mathematical Reasoning Across Difficulty Levels

    April 19, 2025

    Valve Unveils New SteamOS Compatibility for Your Device, Here’s What You Should Know

    May 17, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.