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

197
Vistas
How can I remove an anonymous inline <style> block being injected with a widget?

I'm building a site that uses a JavaScript widget to embed a simple form into the page:

<script src="//example.com/formWidget.js?id=1234"></script>

This then injects the form HTML directly into the page for us to style to suit the site. However, the person who has made the form has included a lot of inline styling that's overriding the site's CSS. I don't want to make every CSS rule !important because that's usually a bad idea; and it's important to note that the inline styling is NOT done on a per-element basis (e.g. <span style="border: 1px solid red;">, which is solved in questions like this); but as a <style> block being injected into the page alongside the HTML:

<style>
.widget-form button {
  border: 1px solid red;
  float: left;
}

.widget-form input {
  background: green;
}
</style>

The style block doesn't have an ID that I can selectively target, so how can I remove this injected inline style block? I'm fine using JS/jQuery to solve it, since that's how the widget is being added.

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

0

As long as the <style> block contains something uniquely identifiable, then it's relatively straightforward to remove. Using jQuery to loop through all the <style> blocks in the page, you can then check their content for the offending selector and remove the entire block:

$('style').each(function(index, elem) {
    let styleBlock = $(this);
    let content = styleBlock.text();

    if (content.indexOf('.widget-form') > -1) {
        //  Found an inline style block - let's kill it!

        styleBlock.remove();
    }
});

You can also do this in a very similar way with plain JS:

var styles = document.querySelectorAll('style');

Array.prototype.forEach.call(styles, function(el, i) {
    let content = el.textContent;

    if (content.indexOf('.widget-form') > -1) {
        //  Found an inline style block - let's kill it!
        
        el.remove();
        // Or el.parentNode.removeChild(el); for maximum backwards compatibility
    }
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

just put it on your css file

.widget-form button {
  border: unset;
  float: unset;
}

.widget-form input {
  background: unset;
}

add !important if necessary

about 4 years ago · Juan Pablo Isaza Denunciar

0

then try this

$( "<html>*/</html>" ).appendTo( "style:last-of-type" );
$( "<html>/*</html>" ).prependTo( "style:last-of-type" );

the selector changes according to your index file

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