-2

Let's say a HTML page that has the following CSS:

body { background: url("http://via.placeholder.com/200x200"); width: 200px; height: 200px; }

Why doesn't this:

console.log(document.body.style.backgroundImage);  // idem for .style.background

show the URL?

Instead, it shows a blank string.

Notes:

  • it seems to works for a child div, but not for the body

  • here a SO snippet wouldn't help (because this question is specifically for body)

Basj
  • 41,386
  • 99
  • 383
  • 673
  • "*it seems to works for a child div, but not for the body*" [no, it doesn't seem to](https://jsbin.com/nuxiyirula/1/edit?html,css,js,console,output). The `.style` only gives you inline styles. It's how it always worked. If you want the values from a stylesheet see the duplicate. – VLAZ Apr 26 '23 at 11:12

1 Answers1

0

try this one

var bodyStyle = window.getComputedStyle(document.body);
console.log(bodyStyle.backgroundImage);
ersati
  • 85
  • 4