JSFiddle - React, Tailwind, and code Playground
CSS
div {
background-image: url('https://iheartcats.com/wp-content/uploads/2016/06/cat-hacker.jpg');
}
JavaScript
const div = document.querySelector('div')
// https://stackoverflow.com/a/21655410
style = div.currentStyle || window.getComputedStyle(div, false)
url = style.backgroundImage.replace(/url\((['"])?(.*?)\1\)/gi, '$2').split(',')[0];
console.log(url)
const image = new Image()
image.onload = () => {
console.log(image.height, image.width)
div.style.height = image.height + 'px'
div.style.width = image.width + 'px'
}
image.src = url