The :host selector is not functioning properly for a custom element with shadow DOM

I am currently in the process of developing a custom component with shadow dom attached to it. The custom component is successfully created and added to the dom with shadow dom attached, but for some reason, the :host style is not being applied.

HTML

<html>
  <head></head>
  <body>
    <notify-user>
      <h3 slot='title'>New message in #customer-support</h3>
      <p>This showcases the power of shadow dom</p>
      <button>Reply</button>
    </notify-user>
    <script src='main.js' type="text/javascript"></script>
  </body>
</html>

JS

class Notification extends HTMLElement{
    constructor(){
        super();
    }
    connectedCallback(){
        console.log('notification custom element attached to DOM');
        console.log('this',this);
        let nRoot = this.attachShadow({mode: 'open'});
        nRoot.innerHTML = `
                    <style>
                        :host {
                            background-color: #3498db;
                            border-radius: 5px;
                            line-height: 1.4;
                            width: 25rem;
                        }
                        button {
                            min-width: 5rem;
                            padding: 0.5rem 2rem;
                            margin: 0.2rem;
                            background-color: transparent;
                            border-radius: 2px;
                            border: none;
                            color: white;
                        }
                        p {
                            color: white;
                        }
                    </style>    
                    <div class="wrapper">
                        <button><span>X<span></button>
                        <slot name='title'></slot>
                        <slot></slot>
                        <p>1 minute ago</p>
                    </div>`;

    }
}

customElements.define('notify-user', Notification);

Even after setting up everything as per the code, the browser fails to apply the specified style for :host. Any assistance would be greatly appreciated.

Would anyone be able to help me troubleshoot this issue?

Answer №1

The :host selector is utilized in this context. However, the problem arises from the fact that your host element lacks any visible content to be shown.

To solve this issue easily, you could adjust the CSS display property to 'block' in order to ensure that the element is visible:

:host {
    background-color: #3498db;
    border-radius: 5px;
    line-height: 1.4;
    width: 25rem;
    display: block;
}

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

JavaScript For/in loop malfunctioning - Issue with undefined object property bug

Currently, I am tackling a basic For/in loop exercise as part of a course curriculum I am enrolled in. The exercise involves working with a simple object containing 3 properties and creating a function that takes two parameters - the object name and the it ...

What is the most effective method for assessing a service using angular?

As a beginner in angular testing, I am eager to start creating tests for my service. However, I have encountered an issue where my service (foo) injects another service (bar) in its constructor. Should I create a mock for bar? Do I need to mock every func ...

How can a chat script be created efficiently without needing Server Root Access?

I currently have a hosting account (cPanel or DirectAdmin) where I don't have root access and am unable to use exec() or shell_exec() functions due to restrictions set by the server administrator. While I understand that socket programming is conside ...

Error: The function passed to props is not mappable

BottomMissionText.tsx import React from "react"; import { Center, Text } from "@chakra-ui/react"; import IMissionText from "../../interfaces/AboutPage/IMissionText"; //Custom component for displaying mission text export defau ...

Receiving updates from the website

It is essential to retrieve the initial news item listed on the website at certain intervals. I am working with Python and Selenium for this task. I have experimented with various methods, such as parsing HTML and attempting to interact with JavaScript, ...

What is the purpose of $ and # in the following code snippet: $('#<%= txtFirstName.ClientID%>')

$('#<%= txtFirstName.ClientID%>').show(); Attempting to pass the ClientId as a parameter from server tags to an external JavaScript file. <input type="text" ID="txtFirstName" runat="server" maxlength="50" class="Def ...

Adjusting the dimensions of a shape proportionally as the window size is altered through the onWindowResize() function

After completing some coding courses, I am now venturing into the world of WebGL using Three.js to enhance my programming skills. I have been experimenting with a widely used function in examples on threes.org: function onWindowResize() { ...

The React.js Mui Collapse component exclusively triggers animation during the transition while closing, without any animation when expanding

Whenever I implement the Collapse component from Mui library in my projects, I face an issue where the transition animation only works when closing the component (when in={false}). However, when opening the component, there is a delay before the animation ...

Break free from the confines of a single quote in jQuery within

I'm currently facing a challenge with my ajax function that calls a PHP class to retrieve code stored in the variable $var. The issue arises from having single quotes within the $var string, causing JavaScript to throw an error. Is there a more effic ...

Having trouble with your Ajax post request?

I am currently working on creating a form that allows users to input information and submit it without the page refreshing. The processing of the form data will occur after the user clicks the submit button. To achieve this, I am utilizing jQuery and Ajax ...

Ways to secure and conceal JavaScript functions or files from being accessed by users

Is there a way to protect my JavaScript methods from being easily copied by users? I am looking for a solution to hide my methods in firebug as well. Any suggestions on how to achieve this? Note: I am looking for methods to hide my code from users without ...

Waiting for Promise Js to be fulfilled

I've been exploring the use of Bluebird for handling promises in Node.Js. I have encountered a situation where I need a function to return only when a promise is fulfilled. The desired behavior can be illustrated by the following code snippet: functi ...

Conceal medication within the jQuery Twitter Bootstrap wizard

I am currently using the Twitter Bootstrap Wizard found at . I am attempting to hide the navbar so users cannot see how many steps they need to complete. Is there a way to hide the navbar? I have tried doing this: .navbar{ display: none } While this c ...

The redirection to the HTML page happens too soon, causing the ASYNC functions to fail in saving the user's image and data to the server

Hey everyone! I'm facing an issue with async/await functions. Here's the code snippet from my backend where I'm trying to save details of a newly registered user. I'm puzzled as to why the Redirect() function is executing before the f ...

Creating a dynamic menu with JQuery

I'm working on a form that contains two hidden parameters that need to be passed to the LoadView function. <form name="frm1" action="http://localhost/tddd27/index.php/AddProduct/LoadView"> <input type="text" name="ID" value="<?php echo $ ...

Guide on transferring morph targets from Blender to three.js?

Is there a way to export morph targets from a Blender file to three.js? I'm aiming to create a json file containing an array called "morphTargets." Any advice on how to achieve this? ...

smooth upward scrolling and superimposed elements

After exploring the question posed in this thread, I am puzzled as to why setting the slider's scroll direction to vertical does not result in smooth up and down scrolling. Could this issue be related to slick itself, or perhaps the accompanying CSS c ...

Would you like to place some text within a content box?

I am facing a challenge where I want to insert content into a contentbar that has an opacity of 0.6. However, I do not want the content (such as text or videos) to have the same opacity as the contentbar. I attempted to place them in separate div tags with ...

Finding the position of a specific element in an array by searching through multiple object keys

Assuming I have a single key (for example, if I have the object.name = 'Sam') and I want to find the index using: var index = array.map(function(el) {return el.name}).indexOf('Sam'); I can retrieve the index of the array element with ...

Tips on adjusting the position of an icon within a dropdown list in HTML?

I have a search field on my webpage with an icon inside it. I managed to find some sample code for the icon. https://i.sstatic.net/WUOot.png However, I am struggling to position the icon to the right of the search box. I've tried adjusting the styli ...