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

205
Views
¿Cómo cambiar una URL de fondo de css en javascript?

A continuación se muestra mi código css, quiero cambiar el fondo de la url a través de javascript, tal vez agregándolo a una variable porque la url cambiará dependiendo de algunos casos en mi programa. ¿Cómo hago esto? Gracias :)

 body { width: 100vw; height: 100vh; background: url('exampleurl.com/example'); background-color: #02203b; background-size: cover; background-position: center center; background-repeat: no-repeat; display: flex; justify-content: center; align-items: center; }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puedes usar backgroundImage como:

 const url = 'https://via.placeholder.com/500'; document.body.style.backgroundImage = `url(${url})`;
 body { width: 100vw; height: 100vh; background: url('exampleurl.com/example'); /* you can use that as default */ background-color: #02203b; background-size: cover; background-position: center center; background-repeat: no-repeat; display: flex; justify-content: center; align-items: center; }

about 4 years ago · Juan Pablo Isaza Report

0

Una buena manera de lograr esto sería usar la función JS classList.https://developer.mozilla.org/de/docs/Web/API/Element/classList

 function change() { const b = document.querySelector('body'); b.classList.toggle('dark'); }
 body { width: 100vw; height: 100vh; background: url('exampleurl.com/example'); background-color: #02203b; background-size: cover; background-position: center center; background-repeat: no-repeat; display: flex; justify-content: center; align-items: center; } .dark { background: black; background: url('https://via.placeholder.com/500/green'); background-size: cover; background-position: center center; background-repeat: no-repeat; }
 <button onclick="change()">change</button>

about 4 years ago · Juan Pablo Isaza Report

0

Para cambiarlo con jquery use:

 const url = 'https://via.placeholder.com/500'; $('body').css('background-image', 'url($url)');

Es decir, si desea cambiarlo según las condiciones, etc.

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!