Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
How to apply styles on shadow DOM

I'm trying to apply some styles on the shadow DOM. I have this example:

const existingElement = document.getElementById("foo");
const shadow = existingElement.attachShadow({ mode: "open" });
const message = document.createElement("p");

message.setAttribute("class", "text");
message.textContent = "Hello World!";
shadow.appendChild(message);
#foo::shadow .text{
  color: blue; //not working
}
<div id="foo"></div>

In the snippet I'm generating a <p class="text">Hello World!</p> in the shadow root inside that <div id="foo"></div>

I need to apply styles to that class text but as it is inside the shadow DOM I can't apply any styles directly. I have tried with ::shadow, ::ng-deep, ::content but no results yet. Any idea?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I have been trying to find some information about this. And what I can find is that a shadow element is completly seperate from the other parts of the environment. Though as the link below stats

It’s worth noting that inheritable styles like color, font and line-height are still inherited in a shadow DOM. To prevent that, use all: initial or, preferably, all: revert once it has better browser support.

Styling in the Shadow DOM With CSS Shadow Parts

I played around with some of your code and made something like this. Which makes the external css have effect over the shadow dom.

const existingElement = document.getElementById("foo");
const shadow = existingElement.attachShadow({ mode: "open" });
const message = document.createElement("p");

message.setAttribute("part", "text");
message.textContent = "Hello World!";
shadow.appendChild(message);
#foo::part(text) {
  color: blue; 
}
<div id="foo"></div>

Instead of having the setAttribute set a class, you set a part. This seems to get the desired effect that you want. It also needs to be said that you are able to set a attribute in JavaScript to apply style to it directly.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda