85

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!

Ian Davis
  • 19,091
  • 30
  • 85
  • 133

7 Answers7

122

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("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>

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;
}
Community
  • 1
  • 1
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
  • 16
    Another way, use the calc method in CSS. background-position-x: calc(100% - 4px); – user959729 Jul 18 '13 at 20:48
  • 8
    Or to use the background-origin property https://developer.mozilla.org/en-US/docs/Web/CSS/background-origin which is intended to solve issues like this one. IE9 and up support: http://jsbin.com/umuvud/50/edit – Dan Eastwell Aug 01 '13 at 13:21
  • 3
    Awesome! You can even use a transparent border, and if you use ems as border-width, this works great with responsivity. Not very hackish at all, just very smart, in my opinion! – Erika Feb 12 '15 at 19:05
  • That is genius. Helped me make an image on a position: fixed header element act in a more or less responsive way too. – jhrr Aug 11 '16 at 19:06
  • background-clip: content-box; The background will be painted within the content box. – Eduard Kolosovskyi Nov 22 '16 at 13:34
  • thanks, we can replace `right center`, to number like `12px 30px` too – yussan Feb 11 '17 at 04:35
  • What if we want repeated images and still pad them? – yerlilbilgin Sep 18 '17 at 22:53
  • Thanks @DanEastwell - your comment I believe is the correct answer. `background-origin: content-box` gave me what I was expecting. – Lime Jul 05 '18 at 21:05
  • sending hearts, wonderful solution – Itai Spector Mar 16 '20 at 12:13
57

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%)

John Fish
  • 1,060
  • 1
  • 7
  • 13
15

There is actually a native solution to this, using the four-values to background-position

.CssClass {background-position: right 10px top 20px;}

This means 10px from right and 20px from top. you can also use three values the fourth value will be count as 0.

Hejar
  • 175
  • 1
  • 2
  • 2
    Don't know why they marked a hack as the correct answer! this is the correct way. – siwalikm Jul 22 '17 at 16:21
  • 1
    This doesn't seem to work. – wag0325 Jul 23 '17 at 14:33
  • 2
    if you're also trying to use background-size: cover, the accepted answer is better, because it controls the size of the image correctly. For this solution you'll have to also define the size of the image to get the correct padding on the right (and bottom). – Allan Nienhuis Jan 12 '18 at 00:54
8

you can use background-origin:padding-box; and then add some padding where you want, for example: #logo {background-image: url(your/image.jpg); background-origin:padding-box; padding-left: 15%;} This way you attach the image to the div padding box that contains it so you can position it wherever you want.

Adrn
  • 91
  • 2
  • 4
  • Good answer, but I think you mean content-box. https://www.w3schools.com/cssref/tryit.asp?filename=trycss3_background-origin – ESP32 Jan 09 '18 at 20:48
7

In case anyone else needs to add padding to something with background-image and background-size: contain or cover, I used the following which is a nice way of doing it. You can replace the border-width with 10% or 2vw or whatever you like.

.bg-image {
    background: url("/image/logo.png") no-repeat center #ffffff / contain;
    border: inset 10px transparent;
    box-sizing: border-box;
}

This means you don't have to define a width.

JHair
  • 194
  • 2
  • 11
0

To add space before background image, one could define the 'width' of element which is using 'background-image' object. And then to define a pixel value in 'background-position' property to create space from left side.

For example, I'd a scenario where I got a navigation menu which had a bullet before link item and the bullet graphic were changeable if corrosponding link turns into an active state. Further, the active link also had a background-color to show, and this background-color had approximate 15px padding both on left and right side of link item (so on left, it includes bullet icon of link too).

While padding-right fulfill the purpose to have background-color stretched upto 15px more on right of link text. The padding-left only added to space between link text and bullet.

So I took the width of background-color object from PSD design (for ex. 82px) and added that to li element (in a class created to show active state) and then I set background-position value to 20px. Which resulted in bullet icon shifted inside from the left edge. And its provided me desired output of having left padding before bullet icon used as background image.

Please note, you may need to adjust your padding / margin values accordingly, which may used either for space between link items or for spacing between bullet icon and link text.

Ravi Khandelwal
  • 718
  • 1
  • 5
  • 15
0

first off, to be a bit of a henpeck, its best NOT to use just the <background> tag. rather, use the proper, more specific, <background-image> tag.

the only way that i'm aware of to do such a thing is to build the padding into the image by extending the matte. since the empty pixels aren't stripped, you have your padding right there. so if you need a 10px border, create 10px of empty pixels all around your image. this is mui simple in Photoshop, Fireworks, GIMP, &c.

i'd also recommend trying out the PNG8 format instead of the dying GIF... much better.

there may be an alternate solution to your problem if we knew a bit more of how you're using it. :) it LOOKS like you're trying to add an accordion button. this would be best placed in the HTML because then you can target it with JavaScript/PHP; something you cannot do if it's in the background (at least not simply). in such a case, you can style the heck out of the image you currently have in CSS by using the following:

#hello img { padding: 10px; }

WR!

WhiteRau
  • 818
  • 14
  • 32
  • I'm just trying to shift the background image to the left 5px. Anyway to do it without editing the image? – Ian Davis Nov 01 '11 at 01:43
  • 1
    ah. again, not really. the `background-position` tag looks like it might be the bunny for you, but all it does is slide the image inside its own bounding box. what are you using it for? – WhiteRau Nov 01 '11 at 01:47
  • @Joseph's answer will work, but it can create problems for the formatting in the rest of the element because now EVERTYHING will appear as though it has a 10px pad and can be overlooked when changing the style. – WhiteRau Nov 01 '11 at 01:49
  • this will not work, as there is no
    (please see the link in my question). thanks tho for your reply!
    – Ian Davis Nov 01 '11 at 01:51
  • what i'd meant to convey was IF you went the other route, that was the code to use. :) guess i wasn't very clear. sorry. – WhiteRau Nov 01 '11 at 02:07