I’m trying to use a script variable as a background image source for a body element in HTML, but no solution on the internet has worked.
<script>
#Some conditions and variables that form the final variable
var backimage = 'images/' + image + '.jpg'
</script>
<style>
body {
background: url(#backimage varable here) no-repeat center center fixed;
background-size: cover;
...
I tried using code like this
Script:
window.onload = function() {
document.getElementById('paragraph').style.background = 'url('+iimage+')';
HTML:
background: <p id="paragraph"></p> no-repeat center center fixed;
But this does not give any results.
How i can use backimage variable correctly?
3
Answers
from your code, i will believe
image
is a variable set somewhere, I used a free image link as image for example, all you need to do is include your own link and asset thebackground-image
with JavaScriotfrom your code, i will believe
image
is a variable set somewhere, I used a free image link as image for example, all you need to do is include your own link and asset thebackground-image
with JavaScriotIt could be because you didn’t set a size for the
tag
Try to do it like this:
In the script instead of "background" write "backgroundImage"
And pay attention to set the image as it is important to you that they see it, if you want for example to see the whole image set in the script in addition:
If something is not clear enough, ask.