cssbackground-image

Positioning background image, adding padding


I'd like to add a background to a div, position right center, but!, have some padding to the image. The div has padding for the text, so I want to indent the background a little. probably makes most sense w/ example:

http://jsbin.com/umuvud/edit#javascript,html,live

Thanks!


Solution

  • Updated Answer:

    It's been commented multiple times that this is not the correct answer to this question, and I agree. Back when this answer was written, IE 9 was still new (about 8 months old) and many developers including myself needed a solution for <= IE 9. IE 9 is when IE started supporting background-origin. However, it's been over six and a half years, so here's the updated solution which I highly recommend over using an actual border. In case < IE 9 support is needed. My original answer can be found below the demo snippet. It uses an opaque border to simulate padding for background images.

    #hello {
      padding-right: 10px;
      background-color:green;
      background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPBAMAAADJ+Ih5AAAAElBMVEVVzFX///9q0mqU35R/2H+q5ar/AMDXAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAMklEQVQImWNgIAIwKUAZyk4g0pSBwUWBhcGBiZmBQcWFwZiBAcgAYghDxQQoBRYhAQAAzeEC4AKpepUAAAAASUVORK5CYII=") no-repeat scroll right center #e8e8e8;
      background-origin: content-box;
    }
    <p id="hello">I want the background icon to have padding to it too!I want the background icon twant the background icon to have padding to it too!I want the background icon to have padding to it too!I want the background icon to have padding to it too!</p>

    Original Answer:

    you can fake it with a 10px border of the same color as the background:

    http://jsbin.com/eparad/edit#javascript,html,live

    #hello {
       border: 10px solid #e8e8e8;
       background-color: green;
       background: url("http://www.costascuisine.com/images/buttons/collapseIcon.gif")
                   no-repeat scroll right center #e8e8e8;
    }