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

87
Visualizações
How to override inherited svg style

I am making a visualization program with d3 javascript.

There is a big group of lines. all the lines should start with opacity of 0.1. In this big group, there are some small groups. When an event is triggered, one of small groups visible and all other small groups invisible. Which small group is visible is dependent on the trigger event. I want to make one of small groups visible and all other small groups invisible by setting the opacity attribute.

So is there any keyword to make the group override the attribute or the style from its parent? Or is there any other similar attribute or style which can be overridden? Otherwise I may have to use for loop to traverse and modify every small groups, which is not very efficient.

<svg width="1000" height="500">
<g class="big_group" style="opacity: 0;">
    <g class='small_group inherit_opacity'>
        <line x1="100" y1="100" x2="200" y2="200" style="stroke: black;"></line>
    </g>
    <g class='small_group inherit_opacity'>
        <line x1="200" y1="100" x2="200" y2="200" style="stroke: black;"></line>
    </g>
    
    <g class='small_group' id='try_to_override_opacity_but_fail' style="opacity: 1;">
        <line x1="300" y1="200" x2="200" y2="200" style="stroke: black;"></line>
    </g>
</g>
</svg>

I intuitively thought there might be some kind of "override" keyword in "try_to_override_opacity_but_fail" to override the opacity in parent group.

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

0

Only if you define the initial opacity with CSS, not with a style attribute:

<svg viewBox="0 0 1000 500">
  <style>
    g{
      opacity:0;
      stroke:black;
      stroke-width:10;
    }
  </style>
  <g>
    <line x1="100" y1="100" x2="200" y2="200"></line>
  </g>
  <g>
    <line x1="200" y1="100" x2="200" y2="200"></line>
  </g>
  <g style="opacity:1;">
    <line x1="300" y1="200" x2="200" y2="200"></line>
  </g>
</svg>

about 4 years ago · Juan Pablo Isaza Relatório

0

the .svg files works fine with css so you can style them like:

svg {
    width: 200px;
    & > g {
        path:nth-last-of-type(2) {
            opacity: 0;
        }
    }
}

if you need to made changes dynamically, should read about style the elements by javascript ;)

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