1

I'm using this code to generate some endless running banner:

<style>
  #myimage {
    position: fixed;
    left: 0%;
    width: 100%;
    bottom: 0%;
    background:url("http://static.giga.de/wp-content/uploads/2014/08/tastatur-bildschirm-senkrechter-strich.jpg") repeat-x scroll 0% 0% / contain;
  } 
</style>

<div id="myimage">.</div>



<script>
    var offset = 0
    setInterval(function() {
        offset +=1
        document.getElementById("myimage").style.backgroundPosition = offset + 'px 0px';
    },50)
</script>

img

To my question: Now I'd like every single image to have a size of 100% of the screen.

I thought about just adding the attribute ...

background-size: 100%;

... but it doesn't seems working that way.

How can I set the width of every single image to 100% of the screens width without removing my style attributes?

  • No like I've added "background-size: 100%;" is not working. @TemaniAfif –  Feb 12 '18 at 22:08
  • I can not really get your thoughts. The question "[link](https://stackoverflow.com/questions/5362750/css-stretching-image-to-100-width)" you linked is not matching to my problem & I can not really get the "exactly the same with same wording" comment tbh. –  Feb 12 '18 at 22:10
  • I added a image. maybe that can clarify our misunderstanding :) @TemaniAfif –  Feb 12 '18 at 22:14

1 Answers1

0

Increase the height of the div and respect the proportion of the image:

var offset = 0
    setInterval(function() {
        offset +=1
        document.getElementById("myimage").style.backgroundPosition = offset + 'px 0px';
    },50)
#myimage {
    position: fixed;
    left: 0%;
    width: 100%;
    bottom: 0%;
    background:url("http://static.giga.de/wp-content/uploads/2014/08/tastatur-bildschirm-senkrechter-strich.jpg") repeat-x scroll 0% 0% / contain;
    
    /* you image is 1000x433 so we need 43.3% of width*/
    padding-bottom:43.3%;
    background-size:cover;
  }
<div id="myimage"></div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • 1
    Thanks nice solution. But why theres one down vote? Can not get it :) –  Feb 12 '18 at 22:22
  • @kurtwellitis don't pay attention to the downvote. Since there is no comment, i don't care about them :) as i guess the downvoter has no *good* reason ;) – Temani Afif Feb 12 '18 at 22:24