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

271
Visualizações
How can I center the text element inside <g> element?

This is the structure of my SVG

<g id="CDT-B6-2">
   <path id="CDT-B6-box"/>
   <text x="1210" y="993" id="CDT-B6-text">888</text>
</g>

Problems:

  1. The textContent from text element is dynamic, it may shorter than length of 3
  2. Inside my SVG I have many <g id="CDT-B6-2"> but different ids & position
  3. I used x="50%" y="50%" to all text elements but it didn't put the text alignment centered from <g> element
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use dominant-baseline: middle and text-anchor: middle:

text {
  dominant-baseline: middle;
  text-anchor: middle;
}

rect {
  stroke: black;
  stroke-width: 2;
  fill: none;
}
<svg width="200" height="100">
  <g transform="translate(10 20)">
    <rect x="0" y="0" width="80" height="40" />
    <text x="40" y="20">888</text>
  </g>
  <g transform="translate(110 40)">
    <rect x="0" y="0" width="80" height="40" />
    <text x="40" y="20">888888</text>
  </g>
</svg>

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