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

131
Visualizações
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 4 years ago · Juan Pablo Isaza
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