Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
HTML

HTML: Build Reusable Web Components

- 09.07.26 - ErcanOPAK

🧩 Web Components = Reusable Elements

Framework components are tied. Web Components are framework-agnostic. Reusable, encapsulated, standards-based.

📝 Web Component Basics

// Custom Element
class MyComponent extends HTMLElement {
    constructor() {
        super();
        this.attachShadow({ mode: 'open' });
    }
    
    connectedCallback() {
        this.render();
    }
    
    render() {
        this.shadowRoot.innerHTML = `
            <style>
                :host {
                    display: block;
                    padding: 20px;
                    background: #f0f0f0;
                    border-radius: 8px;
                }
            </style>
            <div>
                <h2><slot name="title">Default Title</slot></h2>
                <p><slot>Default content</slot></p>
            </div>
        `;
    }
}

// Register component
customElements.define('my-component', MyComponent);

// Usage in HTML
<my-component>
    <span slot="title">Hello World</span>
    <p>This is custom content.</p>
</my-component>

🎯 Advanced Components

// With properties and attributes
class UserCard extends HTMLElement {
    static get observedAttributes() {
        return ['name', 'email', 'avatar'];
    }
    
    constructor() {
        super();
        this.attachShadow({ mode: 'open' });
        this._name = '';
        this._email = '';
        this._avatar = '';
    }
    
    attributeChangedCallback(name, oldValue, newValue) {
        if (name === 'name') this._name = newValue;
        if (name === 'email') this._email = newValue;
        if (name === 'avatar') this._avatar = newValue;
        this.render();
    }
    
    render() {
        this.shadowRoot.innerHTML = `
            <style>
                .card {
                    border: 1px solid #ddd;
                    border-radius: 8px;
                    padding: 16px;
                    display: flex;
                    align-items: center;
                    gap: 16px;
                }
                img {
                    width: 64px;
                    height: 64px;
                    border-radius: 50%;
                }
            </style>
            <div class="card">
                <img src="${this._avatar}" alt="${this._name}">
                <div>
                    <h3>${this._name}</h3>
                    <p>${this._email}</p>
                </div>
            </div>
        `;
    }
}

customElements.define('user-card', UserCard);

💡 Web Component Tips

  • Use Shadow DOM for encapsulation
  • Define observed attributes
  • Use slots for content projection
  • Dispatch custom events
  • Use with any framework

“Web Components are framework-agnostic. Reusable, encapsulated. Essential for modular web.”

— Web Developer

Related posts:

HTML Inputs Behave Differently Across Browsers

HTML: Implement Accessibility (A11y) Best Practices

HTML5: Use Semantic Elements for Better SEO and Accessibility

Post Views: 2

Post navigation

CSS: Master Transforms for 2D and 3D Effects
JavaScript: Use Generators for Iterable Functions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

July 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Jun    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (960)
  • How to add default value for Entity Framework migrations for DateTime and Bool (898)
  • How to make theater mode the default for Youtube (857)
  • Get the First and Last Word from a String or Sentence in SQL (840)
  • How to select distinct rows in a datatable in C# (814)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (597)
  • Add Constraint to SQL Table to ensure email contains @ (582)
  • Average of all values in a column that are not zero in SQL (545)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (512)
  • Find numbers with more than two decimal places in SQL (460)

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes
  • Ajax: Use Axios for HTTP Requests
  • JavaScript: Understand Hoisting
  • HTML: Use Web Storage for Client-Side Data
  • CSS: Use Filter Effects for Visual Magic
  • Windows 11: Unlock God Mode for All Settings

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (960)
  • How to add default value for Entity Framework migrations for DateTime and Bool (898)
  • How to make theater mode the default for Youtube (857)
  • Get the First and Last Word from a String or Sentence in SQL (840)
  • How to select distinct rows in a datatable in C# (814)

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com