Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positioning background image, adding padding [duplicate]

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!

like image 239
Ian Davis Avatar asked Nov 01 '11 01:11

Ian Davis


People also ask

Does padding affect background image?

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.

Can you add padding to an image?

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.

Does background color apply to padding?

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.

Can you have 2 background images CSS?

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.


2 Answers

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; } 
like image 156
Joseph Marikle Avatar answered Oct 07 '22 10:10

Joseph Marikle


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

like image 23
John Fish Avatar answered Oct 07 '22 10:10

John Fish