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

152
Views
Document.write is not working properly with media queries. What can I do - or is there an alternative?

This little function gives me a headache.

document.write in combination with media queries just won't work responsive.

I'm trying to change the css stylesheet with document.write as soon as the max-width changes.

It works, but not responsive - I have to reload the page.

It works perfectly fine with document.body.style.backgroundColor as example - but not with document.write.

Does anyone know why, or is there a better way?

let Swidth = window.matchMedia("(max-width: 700px)")
  function reSize(Swidth) {
    if (Swidth.matches) {
      /* document.body.style.backgroundColor = "yellow"; // This works */
      document.write('<link href="./src/css/variant1.css" type="text/css"  rel="stylesheet">');

    } else {
      
      /* document.body.style.backgroundColor = "blue"; // This works */
      document.write('<link href="./src/css/variant2.css" type="text/css"  rel="stylesheet">')

    }
  }
reSize(Swidth);
Swidth.addEventListener("change", reSize);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use the media attribute of the <link> element.

<link href="./src/css/variant1.css" type="text/css" 
      rel="stylesheet" media="screen and (max-width: 700px)">
<link href="./src/css/variant2.css" type="text/css"
      rel="stylesheet" media="not screen and (max-width: 700px)">
about 4 years ago · Juan Pablo Isaza Report

0

This solution works for me.

function addStylesheet(theHref) {
    if (document.createStyleSheet) {
        document.createStyleSheet(theHref);
    } else {
        var newSheet = document.createElement('link');
        newSheet.setAttribute('rel', 'stylesheet');
        newSheet.setAttribute('type', 'text/css');
        newSheet.setAttribute('href', theHref);
        document.getElementsByTagName('head')[0].appendChild(newSheet);
    }
}

let Swidth = window.matchMedia("(max-width: 700px)")

function reSize(Swidth) {
    if (Swidth.matches) {
        document.body.style.backgroundColor = "yellow";
        addStylesheet("./src/css/variant1.css")
    } else {
        document.body.style.backgroundColor = "blue";
        addStylesheet("./src/css/variant2.css")
    }
}
reSize(Swidth);
Swidth.addEventListener("change", reSize);
about 4 years ago · Juan Pablo Isaza Report
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!