The integration of CSS variables with nested shadow roots allows for seamless inheritance within

Utilizing VueJS, I have created two custom web elements called service-card and slot-card. These elements allow for the customization of styles using CSS variables such as --pm-scale: 0.75.

slot-card can function independently but is also embedded within the inner HTML of service-card:

<slot-card ...>
    <service-card ...>
        ...
        <slot-card ...>
    

Both elements contain the following CSS which is "injected into the shadow root" per the VueJS documentation:

:host {
        --pm-scale: 1;
    }
    

(According to the documentation, "An SFC loaded in custom element mode inlines its tags as strings of CSS and exposes them under the component's styles option. This will be picked up by defineCustomElement and injected into the element's shadow root when instantiated.")

The issue arises on the page below where the value of pm-scale (.75) correctly overrides the variable for the top-level service-card, but the inner slot-card reverts back to the default value of 1:

<style>
    .pm-widget {
                --pm-scale: .75;
    </style>
    <body>
    <service-card class="pm-widget" ...>
        ...
        <slot-card class="pm-widget" ...>
    </body>
    

In Chrome devtools, both the :host and pm-widget styles are applied to both elements. For service-card, pm-widget values override :host values, however for slot-card, it seems that the :host selector on the inner slot-card takes precedence over the pm-widget class. The top-level slot-card element is correctly styled with the pm-widget variable values.

What would be the most effective solution to rectify this situation? Any suggestions would be greatly appreciated!

I have attempted to override CSS variables utilized in the nested web component's styles. I anticipate that values defined in the final document should supersede those defined in the inlined :host selector of the nested component.

Answer №1

After some experimentation, I found that utilizing an internal variable can be effective in certain scenarios, even if the exact reason is not completely clear to me:

:host {
    --custom-scale-internal: var(--custom-scale, 1);
}

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

Place an image at the center with a height set to 100% within a div that has a fixed height and

Apologies for asking about this topic again, but I have been unable to find a solution where the image fills 100% of the height. If you'd like to see the issue in action, here's a link to the jsfiddle: http://jsfiddle.net/BBQvd/3/ I'm just ...

Issue with Vue: After removing an item from a v-for list, there is a remnant

Can you help me solve this issue with my Vue component setup? I have a list of components, each consisting of a preview canvas, a name, and a delete button. These components are stored in Vuex. For example, the list could look like this: | [W] Item 1 ...

Is there a way to dynamically change the height in jQuery/JavaScript?

I am encountering an issue with my embed code where the height changes randomly after a few seconds, depending on certain parameters. Here is an example of the code I am using: $( <embed></embed>) .attr("src", "http://www.bbs.com") ...

Arranging H1 Styling with CSS for Optimum Alignment

My issue revolves around a DIV tag adorned with a background image: <a href="#"> <div class="pagebar jquery"> <h1>JQuery Demonstrations</h1> </div> </a> The CSS applied to this element is as follows: .pagebar ...

Retrieve a PDF file from an HTTP response using Axios

I'm currently working on a project that involves integrating Vue with a Laravel API as the back-end. One of the requirements is to be able to download PDF files from the server when a certain link is clicked. I am using axios for making GET requests, ...

Creating Input Fields on the Fly in VueJS

My goal is to dynamically manipulate input fields in real-time. I found a helpful solution at , which successfully adds and removes fields as needed. However, when attempting to save the input values to the database, the functionality breaks. Here is the ...

What steps should be taken in VUEjs if the API response is null?

There's a method in my code that retrieves a token from an API: let { Token } = await API.getToken({ postId: postId }) if(){} Whenever the token is null, I receive a warning in the console saying "Cannot read property 'Token' ...

Why do Computed Setters fail to refresh the View?

In the current view, there are two buttons available for selection: one for the list layout and the other for the group layout. The chosen state needs to be stored either in local storage or a cookie. // Pug Syntax #app span.button( :class="{'a ...

Having Trouble with Your CSS Styles?

After working with HTML and CSS for over 5 years, I find myself stumped by this particular issue. At the provided URL, there is a single div in that container with an ID of Clarity. The CSS rules I have implemented are as follows: #clarity { text-align: ...

Issues Encountered in Npm Installation

When attempting to run npm install, the following errors are displayed: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemai ...

Styling X and Y axis font color in charts using JavaFX 2.x CSS

Is there a way to change the font color of both X and Y axes? In my search through the css reference for fonts, I only found properties like font-size, font-style, and font-weight. font-size, font-style and font-weight I attempted to use -fx-font-color ...

Currently, I am compiling a list of tasks that need to be completed, but I am encountering a dilemma that is proving difficult to resolve

Recently delved into the world of Javascript and encountered a roadblock that I can't seem to overcome. Here's the snippet of code causing me trouble: add = document.getElementById("add"); add.addEventListener("click", () => { console.log("Ple ...

How to Create a Div with a Stylish Arrow at the Bottom Using HTML

Is there a way to implement this specific object as a DIV using HTML and CSS? I've set up a jsfiddle for testing purposes: http://jsfiddle.net/fz4f4xjv/2/ Here's the code I currently have, but I'm unsure how to add the arrow at the bottom: ...

How can you implement a CSS tooltip specifically for a single element?

Is there a way to add a margin specifically to one tooltip element in bootstrap 4? $('[data-toggle="tooltip"]').tooltip() .tooltip.bs-tooltip-right{ margin-left: 30px; } button{ margin-top: 20px !important; } <link rel="stylesheet" ...

Tips for anchoring a row to the bottom of a Bootstrap webpage

After working with bootstrap, I am looking to position the last row of the page at the bottom in a fixed and responsive size manner. ...

A guide on iterating through carousel elements using Vanilla JavaScript

I am currently extracting data from The Movie Database and I aim to showcase the retrieved information on my webpage using a carousel card with 3 slides. Here is a summary of my progress so far, html, <div class="details"><!--Movie deta ...

Issue with Sass @use failing to load partial

I'm currently facing an issue while trying to import a sass partial called _variables.scss into my main stylesheet named global.scss. Every time I compile the sass code, I encounter the following error message: Error: Undefined variable. Here is the ...

Update the positioning of the element to center instead of the default top left origin using jQuery

I am facing an issue with positioning a marker inside a triangle, which is represented by a simple div, as shown in the image below: https://i.stack.imgur.com/0Q7Lm.png The marker needs to be placed exactly at the centroid of the triangle. However, it see ...

Modify CSS based on the size of the containing iframe dynamically

My website is embedded on partner sites through an iframe, but the length of my new website exceeds the space allocated in the iframes set by my partners (who have disabled scrolling). Since I am unable to adjust the settings on my partner sites, I need t ...

Adjusting the size of a division element to match the dimensions

Currently, I am still in the process of finalizing the remainder of the page, but I have encountered a minor issue. The problem lies in my image div not matching the height of the container. Although the image itself is the correct size, the image div appe ...