• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

102
Vistas
Load CSS Stylesheet into IFrame

I am creating an iframe to display a functional component (ReactJS) in its own silo. The iframe shows up with all the content, but CSS is not applied.

I have used this answer to write some JavaScript that should apply my main stylesheet to the iframe:

    import stylesheet from '../../main.css'    
    
    export default function FunctionalIFrameComponent({ children, title, ...props }) {
          const iframeRef = useRef()
          const mountNode = iframeRef?.current?.contentWindow?.document?.body
        
          useEffect(() => {
              if(iframeRef.current) {
                  console.log(stylesheet)
                  let cssLink = document.createElement("link") 
                  cssLink.href = "file://../../main.css"; 
                  cssLink .rel = "stylesheet"; 
                  cssLink .type = "text/css";
        
                  iframeRef.current.contentWindow.document.body.appendChild(cssLink)
              }
          }, [iframeRef])
        
          return (
            <iframe title={title} {...props} ref={iframeRef}>
              {mountNode && createPortal(children, mountNode)}
            </iframe>
          )
        }

However, no styles are applied.

When I log the contents of the stylesheet variable, only an empty object gets returned (i.e., {}).

How should I load my stylesheet into the iframe?

about 3 years ago · Juan Pablo Isaza
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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda