Search code examples
htmlcssbackground-imagebackground-size

Two DIV backgrounds with different sizes


Essentially, the title says it all all.

div {
  border-radius: 4px;
  background-image: url(IMAGE1.PNG), url(IMAGE2.PNG);
  box-shadow: 0 0 10px rgba(0,0,0,0.75), inset 1px 1px 2px #fff, inset -1px -1px 1px #777;
}

Essentially, I want image1 to maintain default properties as the typical background, but I want image2 to be set to something equivalent to background-size: cover;


Solution

  • background-size accepts multiple values too.

    background-size: auto, cover;
    

    var pattern = Trianglify({
      height: 300,
      width: 300,
      cell_size: 40,
    });
    
    var div = document.getElementsByTagName('div')[0];
    
    div.style.backgroundImage = "url('https://i.sstatic.net/955qe.png'), url('" + pattern.png() + "')";
    div {
      border-radius: 4px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.75), inset 1px 1px 2px white, inset -1px -1px 1px #777;
      background-position: center;
      background-repeat: no-repeat;
      background-size: auto, cover;
      height: 140px;
      width: 300px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/trianglify/0.2.0/trianglify.min.js"></script>
    
    <div></div>