Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!