Search code examples
htmlcssbackground-image

Multiple backgrounds on top of each other (Normal, Stretched, Normal)


Since I faced an issue with the background image beeing too short for the content at different resolutions, I tried to split the background into 3 parts and stretching the middle one automatically to fill the space between the top and bottom image accordingly. Unfortunately I didn't manage to realize this in CSS. How to do this exactly?

content_background_top.png: 1024 x 68px
content_background_middle.png: 1024 x 6px (Stretched)
content_background_bottom.png: 1024 x 71px

Something like this:

#content{
    width: 1024px;
    height: 950px;
    text-align: center;
    padding: 35px 0 35px 0;
    background: url(img/content_background_top.png), url(img/content_background_middle.png), url(img/content_background_bottom.png);
    background-repeat: no-repeat;
    background-size: 1024px 68px, 1024px auto, 1024px 71px;
}

Solution

  • You'll need to specify the background-positions and background sizes. Also note that you'll need to list your larger "middle" background last so that it doesn't cover the others.

    #content {
      width: 1024px;
      height: 950px;
      text-align: center;
      padding-top: 35px;
      background: url(https://i.sstatic.net/vNQ2g.png?s=128&g=1), url(https://i.sstatic.net/vNQ2g.png?s=128&g=1), url(https://i.sstatic.net/vNQ2g.png?s=128&g=1);
      background-repeat: no-repeat;
      background-position: 0% 0%, 0% 100%, 0% 50%;
      background-size: 100px, 100px, cover;
    }
    <div id="content"></div>