Why isn't the CSS selector `:host > some_child_element` working as expected?

I have this unique custom element structure:

<my-custom-form>
  #shadow-root
   <style>
     :host label {
        color: red;
     }
   </style>
   <div id="wrapper">
     <slot name="form-label"></slot>
     ...
   </div>
</my-custom-form>

Utilizing it in the following way:

<my-custom-form>
    <label slot="form-label">Custom Label</label>
</my-custom-form>

Expectation was for the label to be styled with red text. However, it did not work. Is the <label> element actually a direct child of :host in the light DOM?

Answer №1

ShadowDOM reflects slotted content without incorporating it.

This means that styling is applied in lightDOM using global CSS.

The :host() selector only affects the surface appearance in lightDOM.

For a more in-depth explanation, visit: ::slotted CSS selector for nested children in shadowDOM slot

Answer №2

It seems like there may be a syntax issue you're dealing with, so try using this:

  • :host( [label] ){ }

For more clarification, check out this answer on SO:

  • Applying more in depth selection to the :host CSS pseudo class

You can also refer to the documentation for further information:

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

Is there a way to place labels or paragraphs directly on top of objects?

How can I center these labels on top of the items in my layout? Placing the <ion-label> has positioned the labels to the left of the items instead of centered. Is there a way to achieve top center alignment without affecting the round shape of the ch ...

Vertical stacking of Bootstrap cards is a common design practice

Good day everyone! I'm in the midst of a personal project that involves utilizing the bootstrap framework and PHP. My goal is to fetch data from my MySQL database and have it displayed in horizontal rows of 4 individual cards within a bootstrap card e ...

Shift all content to the right when viewing on mobile devices

I'm looking to create space for a menu on the left side, similar to the one on Mashable's mobile view. How can I move all the content to the right? Feel free to resize the window and compare with . Thank you. Best regards, Marius ...

Suggestions for preventing the highlighting of the space between buttons on a webpage

html: <button id='automoney' onclick='minusTen()'></button> <button id='automoney2' onclick='minusHundred()'></button> <button id='automoney3' onclick='minusFiveHundred()& ...

Opt for CSS (or JavaScript) over SMIL for better styling and animation control

I recently noticed an alert in my Chrome console that SVG's SMIL animations are being deprecated and will soon be removed. The message suggested using CSS or Web animations instead. Since I want to transition from SMIL to CSS animations, there are a ...

Creating a responsive website that utilizes YAML and is optimized for extremely narrow screen widths

I have noticed that my responsive design using YAML is working well for the most part. However, on screens with very small widths, the YAML grids become extremely tiny. In such cases, it would be more practical to have the right grid displayed below the le ...

Combining two different elements using CSS styling

In the process of designing, I am faced with the challenge of overlaying one section on top of another while maintaining a curved shape like shown in this design. I have divided the sections into first (blue) and second (gray), and the current layout look ...

Troubleshooting Challenges with JavaScript DOM Manipulation

I am facing an issue with a table where I need the first column to remain fixed when scrolling horizontally. The code snippet below works perfectly for the first column of td's, but fails to work for the tr's. Despite checking the code thoroughly ...

The logo emerges from the background color, with the text consistently appearing in black

In my attempt to display a logo and text together in an Outlook page, I encountered an issue. The content from the HTML page appears different when sent to MS Outlook. Despite not using flexbox in my HTML code, Outlook interprets it differently. I am uncer ...

Android Chrome users experiencing sidebar disappearance after first toggle

Over the past few days, I've dedicated my time to developing a new project. I've been focusing on creating the HTML, CSS, and Java layout to ensure everything works seamlessly. After completing most of the work, the design looks great on desktop ...

Is there a way to display my <i> tags inline without any issues?

I am facing an issue with my code and need assistance. I have two links that I want to display but my code doesn't seem to respond as expected. The first link is: https://i.sstatic.net/fryPH.png The second link is: https://i.sstatic.net/j849M.png A ...

I am currently attempting to design a blurred background with text overlay, where the text seems to clear up the blur effect of the background. However, I am facing

Goal: I want to achieve a visually appealing effect by overlaying text on a blurred background image, making it appear as though the text is transparent and revealing the clear background image behind it. Status: I have successfully created a layout with ...

Finding the correct path for the background-image URL in VueJS has been a challenge

Currently, I am learning VueJs and have encountered a roadblock in my journey. The issue I am facing is determining the correct path to specify for the URL of the background-image property in the CSS section of my component. <template> <div cla ...

Combining a standard JSS class with Material-UI's class overrides using the classnames library

I am exploring the method of assigning multiple classes to an element in React by utilizing the classnames package from JedWatson, along with Material-UI's "overriding with classes" technique. For reference, you can see an instance in MUI's docu ...

Ways to ensure a div is positioned beneath another div

I am facing an issue with two divs; one on the right and the other on the left. When I try to add a third div below the right div, it ends up appearing under the left div or at the left side of the left div. Here is my current setup: https://i.sstatic.n ...

Working with jQuery, CSS, and functions to modify the current tag's class

Let's keep it brief and straightforward: Here is my HTML <div id="headerVideoControls" class="overlayElement"> <div id="videoMuteUnmute" onclick="muteUnmuteVideo('headerVideo')">mute button</div> </div> Edited ...

Is it possible to integrate Polymer elements/WebComponents into TinyMCE?

I've been working on creating a unique TinyMCE editor for educational content, aiming to wrap specific blocks as interactive 'activities'. Each block of content will contain multiple activities with their own primary keys for identification. ...

Strategies for adjusting text size to fit within a resizable div container

I'm facing an issue with a resizable div that has text inside. When I resize the div, the last line of the text goes beyond the boundaries of the div. How can I ensure that all the text fits within the resizable div while maintaining the appropriate f ...

Tips for aligning a flex element child vertically

I am trying to achieve a layout where there are 3 divs vertically centered in one flex container with flex-direction:column. However, I need the 3rd div to be positioned at the bottom of the container. To better illustrate what I am looking for, you can v ...

Modifying the Placeholder Color in a Material UI Autocomplete: Tips and Tricks

In my team, we are working on two projects that both utilize an internal library with a header containing a search box. In one project, the placeholder text "Search" displays normally. https://i.stack.imgur.com/lhL5V.png However, in the second project wh ...