Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Background image and padding

Tags:

html

css

padding

I want to add a background image on the right side of the list items, and want to have some padding from the right side as well, but I'm unable to do that. Please have a look at following example:

HTML:

<ul>     <li>Hello</li>     <li>Hello world</li> </ul> 

CSS:

ul{     width:100px;   }  ul li{     border:1px solid orange;     background: url("arrow1.gif") no-repeat center right; }  ul li:hover{      background:yellow url("arrow1.gif") no-repeat center right; } 

I know we can set the image position by pixels, but since each of the li has different width, I can't do that.

Here's JSFiddle link: http://jsfiddle.net/QeGAd/1/

like image 494
user966582 Avatar asked Apr 17 '12 09:04

user966582


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.

How do you put a background image on a padding?

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.

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.

What does background-origin do in CSS?

The background-origin is a property defined in CSS which helps in adjusting the background image of the webpage. This property is used to set the origin of the image in the background. By default, this property sets the background image origin to the upper-left corner of the screen/webpage.


2 Answers

You can be more precise with CSS background-origin:

background-origin: content-box; 

This will make image respect the padding of the box.

like image 150
Mladen Janjetovic Avatar answered Sep 19 '22 06:09

Mladen Janjetovic


You can use percent values:

background: yellow url("arrow1.gif") no-repeat 95% 50%; 

Not pixel perfect, but…

like image 27
Juan G. Hurtado Avatar answered Sep 18 '22 06:09

Juan G. Hurtado