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

180
Vistas
How to add background color to text inside textbox?

I do not know how to ask this question correctly and I do not know how to find solutions in google because it leads me to how to add entire background color to a textbox which I am not looking for. I am trying to achieve such results like image below:

enter image description here

I want to add background to each text(of course characters) inside a input tag except spaces. User will start typing stuff inside input tag and will apply background color but spaced text won't color.

I do not have any code because I do not know how to do it using css or JS if neccesary. I just have a

<input type="text">

I think it might use only JS but I do not know how to(of course I know JS). How do I achieve that?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use div with contentEditable and use little js to append text as span

<div contentEditable></div>

CSS

span {
  background-color: red;
  margin-right: 2px;
}
div {
  border: 1px solid black;
}
var el = document.querySelector("div");

el.addEventListener("blur", () => {
  var content = el.textContent;
  var contents = content.split(" ");
  el.innerHTML = "";

  contents.forEach((item) => {
    el.innerHTML += `<span>${item}</span>`;
  });
});

Something like this

about 4 years ago · Juan Pablo Isaza Denunciar

0

Another method is to overlap the input box with a "mirroring" element. The overlap must be precise, both in layout and text characteristics. Here is a minimal example:

var input = document.getElementById("input"),
  mirror = document.getElementById("input-mirror");

input.addEventListener("keyup", function() {
  var words = input.value.split(" ").map(function(a) {
    var span = document.createElement("span");
    span.textContent = a;
    return span;
  });
  mirror.innerHTML = "";
  for (let i = 0; i < words.length; i++) {
    mirror.append(words[i], " ");
  }
});
.container {
  display: grid;
}

#input,
#input-mirror {
  font: inherit;
  box-sizing: border-box;
  margin: 0;
  border: 1px solid;
  padding: 0;
  grid-row: 1;
  grid-column: 1;
}

#input {
  background: transparent;
}

#input-mirror > span {
  background: #0f0;
}
<div class="container">
  <span id="input-mirror"></span>
  <input id="input" type="text" />
</div>

about 4 years ago · Juan Pablo Isaza Denunciar
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