What are the differences between custom elements and web components in terms of managing CSS encaps

Is the style encapsulation provided by custom elements that do not utilize a shadow DOM equivalent to web components (autonomous custom elements) utilizing a shadow DOM?

The information on using custom elements, including examples on GitHub, from the MDN page is somewhat unclear regarding this.

Answer №1

Indeed, Elements created with Custom Elements but lacking shadowDOM function as regular DOM elements, such as a simple <div> or <p>.

It is only through the use of shadowRoots that CSS can be scoped (or even id attributes)
Enabling external styling through:

  • CSS properties
  • ::part
  • theme

Distinct technologies within Web Components

  • Templates
  • Custom Elements
  • shadowDOM

Each of these can stand alone without depending on the others

You are able to apply a shadowRoot to any regular DOM element:

document.querySelector("DIV").attachShadow({mode:"open"})

This action does not transform them into a Custom Element, but it does create a scoped area for CSS.

Incorporate shadowDOM only when necessary. (unfortunately, many libraries enforce shadowDOM usage)

Moreover, you have complete freedom to invent any tags you desire.

<pie-chart>
  <slice size="90" stroke="green">HTML</slice>
  <slice size="1"  stroke="red">JavaScript</slice>
  <slice size="9"  stroke="blue">CSS</slice>
</pie-chart>

The tag <pie-chart> represents a Web Component/Custom Element, whereas <slice> remains unclassified (and cannot be classified).

For more information, visit my blogs on DEV.to:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looking for help with Vue.js/Vuetify - having issues with input field and removing items from list

Currently, I am busy diving deep into Vuejs and encountering a couple of challenges. The first issue I am facing is while creating a basic todo app with CRUD functionality. When passing data through an input field, it refuses to adhere to full-width setti ...

personalizing material-ui component styles – set select component color to be pure white

I am looking to implement a dropdown menu using material-ui components (refer to https://material-ui.com/components/selects/). To do so, I have extracted the specific component from the example: Component return <div> <FormControl variant="outli ...

Are you interested in designing a bootstrap album for your homepage?

I'm currently working on designing an album-style layout similar to the examples provided below: if you look at the recent works section where you have an image on one side and text on the other, occupying the entire width of the page: or the example ...

Enhance your website's accessibility by using JavaScript to allow the down and up arrow keys to function

Thank you for taking the time to read this, any feedback is greatly appreciated. The current script on my website is only partially functional (click "i" in the bottom right corner). Currently, the script will focus on the first link AFTER pressing the T ...

The white-space property disrupts the normal width of elements

My initial goal was to stack two elements together side-by-side, both taking the full available height. One element has a fixed width of 200px with an image (70px wide) inside, while the other element should only fit one line of text, clipping any overflow ...

Tips for ensuring that all children within a flex-container have equal heights

I seem to be facing an issue with this problem. My goal is to ensure that all the child divs within a flex container have the same height as the tallest child div, which in this case is 100px. Additionally, I want them to be aligned at the center. I&apos ...

Issues with Navigation Style in Internet Explorer 8

Having trouble with the <nav> styling not displaying correctly in IE8 on internal pages. Strangely, it's almost perfect on the homepage but drops to the right instead of down. Website: searchtransparency(dot)net/trc/ ...

There seems to be a white space problem located beneath the header photo and the initial

I'm a beginner at coding and working on this project for fun. I'm almost done, but I can't get rid of a white space that's dividing my header from the first section. See the screenshot below: https://i.stack.imgur.com/a1flt.png Even af ...

A guide on updating CSS content dynamically within ExtJS

In my resource folder, I have a CSS file that contains some values which I need to change or add based on certain conditions. However, I'm struggling to figure out how to achieve this. Here is an example of the code: triggers: { clear: { ...

Tips for manipulating SVG in a 3D realm using CSS

I'm struggling with creating a spinning animation using a `svg` that contains three `polygons`. I want to rotate each of them horizontally in 3D space, but the animation appears flat. Below is the code snippet showcasing the three polygons with classe ...

Steps for deactivating tab stops on the primary webpage in the presence of a snackbar

On my web page, there are multiple drop-down menus, input fields, and buttons that can be interacted with using both the tab key and mouse. After entering certain data, I trigger a snackbar (source: https://www.w3schools.com/howto/howto_js_snackbar.asp) t ...

Shifting the Bootstrap sidebar

Here is a layout defined using Bootstrap: <div class="container-fluid"> <div onclick="smallSize()">Click</div> <div onclick="largeSize()">Click</div> <div class="row"> ...

<a href> hyperlink with the web address as the anchor text

Can a hyperlink be generated using a variable instead of typing in static text like CLICK HERE, so that it displays the full URL of the page specified in the a href tag, regardless of the URL? < a href="http://sourceforge.net/projects/tcpick/,">CL ...

What is the functionality of this pseudo element?

I am interested in understanding the purpose of the pseudo element and how it affects the layout without altering the existing markup. Check out the code demo div{ border: 1px solid #000; height: 300px; } div:before{ content: ""; ...

The global CSS styles in Angular are not being applied to other components as expected

Currently utilizing Angular v10, I have a set of CSS styles that are meant to be used across the entire application. To achieve this, I added them to our global styles.css file. However, I'm encountering an issue where the CSS is not being applied to ...

Unable to change the alignment to the left in the CSS styling

My table has th elements with a text-align: left property. I tried to override this with text-align: center in my CSS, but it's not working. Can anyone suggest a reason why? Here is my table: <table class="days_table" align="center"> <thea ...

Display my search box when the button is activated

I have a button labeled "Search" that I want to click in order for my search field (which is currently hidden with display:none) to become visible. However, my current setup is not working as intended. I have created a jsfiddle to illustrate my issue. Al ...

Switch the class back and forth between fixed-bottom and position-sticky

I am working on making the footer have a class of position-sticky which changes to fixed-bottom when it reaches the end of the page. So far, this is what I have implemented: $(window).on("scroll touchmove", function() { var scrollHeight = $(doc ...

What is the best way to prioritize items on a list in JavaScript?

Looking to organize your to-do list items by priority? In this task list, users can enter an item, select a priority level, and add it to the list. Here is an example HTML form: <input id="task" type="text"/> <select id="priority"> <o ...

What is causing the incorrect resolution of the absolute path?

Struggling to incorporate Google's more modern recaptcha example? Not well-versed in Linux paths. Organized Structure https://i.sstatic.net/MBU3Q.png PHP <?php // autoload.php @generated by Composer require_once '/vendor/composer/autolo ...