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!
The padding-box value means that the only the padding box part of the HTML element has the background image rendered. The content-box value means that the only the content box part of the HTML element has the background image rendered.
In the File Properties dialog, select the Appearance tab. In the Padding section, enter numbers in the Left, Right, Top, and/or Bottom fields to set the width of the padding (in pixels). Click OK. The padding is added to the image, displayed in the default background color of the image.
Rather than leave the padding transparent, you can add color to the padding since the background color doesn't fully extend past the text element.
CSS allows you to add multiple background images for an element, through the background-image property. The different background images are separated by commas, and the images are stacked on top of each other, where the first image is closest to the viewer.
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("https://placehold.it/15/5C5/FFF") 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>
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; }
this is actually pretty easily done. You're almost there, doing what you've done with background-position: right center;
. What is actually needed in this case is something very much like that. Let's convert these to percentages. We know that center
=50%
, so that's easy enough. Now, in order to get the padding you wanted, you need to position the background like so: background-position: 99% 50%
.
The second, and more effective way of going about this, is to use the same background-position
idea, and just use background-position: 400px (width of parent) 50%;
. Of course, this method requires a static width, but will give you the same thing every time.
Method 1 (99% 50%)
Method 2 (400px 50%)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With