What are web elements

With JavaScript frameworks like React and Angular, developers have long been able to define reusable web elements. However, each framework uses a different standard, which makes the cross-project use of the practical Code snippets prevented in many cases. The so-called Web Components, which are reusable HTML components that can be used independently of the framework. The web element type standardized in 2012 is now supported by every common browser.

What are Web Components?

Web Components are code blocks that encapsulate the internal structure of HTML elements including CSS and JavaScript and enable the respective code to be used anywhere in websites and web apps. The concept was developed by a working group of World Wide Web Consortium (W3C), which was founded in 1994 by the weaver inventor Tim Berners-Lee and has since been committed to standardizing all elementary web technologies. The Web Components Model, published as a standard in 2012, primarily provides the following four specifications for creating useful HTML components:

  • Custom elements: Set of JavaScript APIs for defining custom elements
  • Shadow DOM: Set of JavaScript APIs for adding DOM elements
  • ES modules: Modules for the integration and reuse of JavaScript documents
  • HTML templates: Mark-up templates that are not shown on the displayed page and can be used as the basis for user-defined elements

The Web Components standard is now supported by all common browsers. All JavaScript frameworks or libraries that work with HTML can be used to work with the encapsulated HTML code.

Why should you use Web Components?

Libraries and Frameworks such as Angular or jQuery have been one of the most important tools of every web programmer for years. But as practical and versatile as these basic code structures are, which save a lot of work in the development of projects, they are often inflexible when it comes to one cross-project use goes. It is not uncommon for developers to have to rewrite or rewrite code when, for example, a framework change is pending. For this reason the World Wide Web Consortium (W3C) introduced Web Components and thus a universal framework for the simple and comprehensive reuse of HTML, CSS and JavaScript code.

Since the universal web elements are separated by a simple, easy-to-learn syntax program beginners will also benefit from the W3C standard. In recent years, Google has been working on the "Polymer Project" Libraries and templates for programming web components to be developed and freely available.

An overview of the Web Components

The Web Components model is basically based on four specifications, which we will discuss in more detail below. We also give some concrete examples of the Web Components specifications.

Custom elements

Custom elements are HTML tags that encapsulate the content of HTML, including CSS instructions and scripts. You will be in the CustomElementRegistry declared. Their main characteristics are:

  • They end with a closing day.
  • Its name is a DOM string and always contains a hyphen.
  • Your name may only appear once within the CustomElementRegistry.

To create a custom element you need JavaScript and the method define. The following Web Components example shows an example of a custom element that can be used to integrate an individual button:

In order to be able to use this element in a web application, the following code is sufficient:

Shadow DOM

The most important property of Web Components is their ability to encapsulate HTML elements. The Shadow DOM API helps you by allowing you to attach hidden DOM trees to a document tree. Only the HTML tag of the shadow DOM is visible. This allows you to add HTML elements to the hidden DOM without having to change the main DOM every time. You can find more detailed information about this technique as well as specific application examples in our article about shadow doms and how they work.

ES modules

ES Modules are modules that export objects, functions or variables from a JavaScript file. This property makes it possible to divide variables within a file into groups and to reference them. Currently there are two ES module systems. While CommonJS Originally owned by Node.JS, the newer system is already included in JavaScript ES6.

To get a function from a JavaScript library to export, use the method export. In the example you are exporting a function that reproduces an input string twice.

Via import you can now call the exported function as often as you like.

HTML templates

A HTML template is a template for HTML files. The elements contained therein remain inactive and not rendered until they are explicitly called. Due to this property, they have no negative effect on the Loading time a website. They are therefore a sensible alternative to conventional JavaScript methods.

With the day <template> define an HTML template. In the following example you create a template called "My Element".

To use the template in a website, call it with the JavaScript methods getElementbyId and content and hang it on the DOM.

How do I use Web Components?

The presented examples for Web Components only work if you combine the individual parts of the Web Components model with one another. The following scheme shows the procedure in a simplified manner:

  1. Create a JavaScript class or function or export them from an existing JavaScript file using ES Modules.
  2. Declare your new custom element with the method CustomElementRegistry.define ().
  3. If necessary or desired, attach a hidden shadow DOM to add child elements to your custom element.
  4. Define an HTML template with the tags <template> and .
  5. Use the generated custom element within your website like a normal one HTML element.

The following tutorial offers you a simple introduction to programming Web Components:

With another click you load the video from YouTube. In this case, YouTube can set cookies over which we have no influence.

Compatibility of Web Components with the various browser versions

The compatibility of Web Components was not given in earlier browser versions. In the meantime, however, all common browsers support Custom Elements, Shadow DOM, ES Modules and HTML templates. The following table provides an overview of the compatibility of the various web components.

Compatible? Firefox Chrome Edge safari Opera
Custom elements Yes Yes Yes (from version 76) Yes Yes
Shadow DOM Yes Yes Yes (from version 75) Yes Yes
ES modules Yes Yes Yes Yes Yes
HTML templates Yes Yes Yes Yes Yes

Web Component Libraries, Templates, and Samples

Programming Web Components can be complicated, especially as a beginner. However, you can find numerous on the web Libraries with templates and standard functions as well as practical examples of Web Components to make your work easier.

  • Lit Element: simple basic class for creating web components
  • Polymer Project: As part of the Polymer Project, Google offers various tools for working with Web Components - including: a starter kit for programming apps with web components, an HTML template library for JavaScript and a wide variety of ready-to-use elements.
  • Hybrids: Provides a simple UI library for creating web components
  • Slim.js: library with advanced properties for web components based on class-based inheritance of JavaScript ES6 falls back
Similar articles

Bootstrap alternatives: an overview of the best front-end frameworks

Bootstrap is considered to be the most popular framework solution for the conception and creation of highly functional web interfaces. The collection of HTML, CSS and JavaScript components, which Twitter published under the free MIT license in 2011, undoubtedly enjoys a large community as well as a large number of extensions, interfaces to content management systems and ready-made themes. ...

Internet Programming Languages: The Best Languages ​​for the Web

Do you want to learn one or more web programming languages? The first challenge is to choose one. After all, there are hundreds of programming languages ​​- but not all of them are suitable for web development. To make the decision easier for you and to tackle your web project, we will briefly introduce you to the most popular Internet programming languages.

Less CSS: introduction and tutorial

CSS is one of the most important languages ​​on the World Wide Web. However, working with the stylesheet language is often unnecessarily cumbersome, which is why many developers prefer to use Less instead. The CSS preprocessor not only simplifies the writing of the style sheet code, but also extends CSS with various features such as variables, functions or mixins. Our ...

Reverse engineering of software

Reverse engineering of software can help to uncover and understand how it works and how it is structured. Through the "reverse engineering" of software down to the source code, it is not only possible to fathom false reports, but also to analyze competing programs. Reverse engineering can therefore be used in a variety of ways. But how exactly is reverse engineering in software ...

Polyfills: Code building blocks for modern web features

Websites and apps are subject to constant change. New features and functions often promise a higher quality, more secure or better performing web project, which is why their implementation is often purely a matter of form. However, many browsers lag far behind when it comes to supporting modern web technologies - a problem that you can counteract with so-called polyfills ...