Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

337
Vistas
How to place text over a SVG shape with JavaScript, React, and HTML?

I'm using a combination of .createElementNS() and .appendChild() functions to add circle elements and their SVG parent elements to the DOM. However - when I create a sibling text element relative to the circle element, a text node is create in the DOM but isn't displayed. How do I get my text node to display over my circle element SVG?

Here's what my JavaScript creates followed by the SVG that's rendered in the DOM.

document.addEventListener('DOMContentLoaded', () => {
  let digitKeys = 10;
  for (let i = 0; i < digitKeys; i++) {

    const textNode = document.createTextNode(i); // last/deepest child

    const textElement = document.createElement('text'); // first/deepest parent
    textElement.setAttribute('color', 'white');
    textElement.setAttribute('z-index', 50);
    textElement.setAttribute('alignment-baseline', "middle");
    textElement.setAttribute('text-anchor', "middle");
    textElement.setAttribute('x', 8);
    textElement.setAttribute('y', 2);
    textElement.setAttribute('font-size', 50);
    textElement.appendChild(textNode); // append deepest child to first parent

    const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle') //svg elements require createElementNS() (NS = namespace) instead of createElement();
    circle.setAttribute('class', 'digitKeys');
    circle.setAttribute('cx', 50)
    circle.setAttribute('cy', 50)
    circle.setAttribute('r', 33)
    circle.setAttribute('fill', 'steel') // sibling of first parent
    circle.appendChild(textElement);
    console.log(circle)
<svg id="circlesSVG" width="100" height="100">

  <circle class="digitKeys" cx="50" cy="50" r="33" fill="steel">

    <text color="white" z-index="50" alignment-baseline="middle" text-anchor="middle" x="8" y="2" font-size="50">
      0
    </text>
  </circle>
</svg>

about 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda