我认为答案是否定的,但是你可以用CSS定位一个背景图像,这样它就离右边有一个固定数量的像素?
如果我设置x和y的背景位置值,似乎它们只分别从左边和上面给出固定的像素调整。
我认为答案是否定的,但是你可以用CSS定位一个背景图像,这样它就离右边有一个固定数量的像素?
如果我设置x和y的背景位置值,似乎它们只分别从左边和上面给出固定的像素调整。
当前回答
如果你碰巧在现代浏览器中碰到这个话题,你可以使用伪类:after来做任何与后台相关的事情。
.container:after{
content:"";
position:absolute;
right:20px;
background:url(http://lorempixel.com/400/200) no-repeat right bottom;
}
这个CSS会把背景放在“”的右下角。元素的右侧有20px的空间。
看看这个小提琴,例如http://jsfiddle.net/h6K9z/226/
其他回答
background-position: right 30px center;
它适用于大多数浏览器。完整列表见:http://caniuse.com/#feat=css-background-offsets。
更多信息:http://www.w3.org/TR/css3-background/#the-background-position
求负值的解。调整右填充来移动图像。
<div style='overflow:hidden;'>
<div style='width:100% background:url(images.jpg) top right; padding-right:50px;'>
</div>
</div>
您可以在编辑器中将背景图像定位为距离右侧x像素。
background: url(images_url) no-repeat right top;
背景图像将位于右上方,但将显示为距离右侧x个像素。
有一种方法,但不是每个浏览器都支持(见这里的覆盖)
element {
background-position : calc(100% - 10px) 0;
}
它适用于所有现代浏览器,但IE9可能会崩溃。也没有覆盖=< IE8。
另一个我没有看到提到的解决方案是使用伪元素,我相信这个解决方案将适用于任何CSS 2.1兼容的浏览器(≥IE8,≥Safari 2,…),它也应该是响应:
element::after
{
content:' ';
position:relative;
display:block;
width:100%;
height:100%;
bottom:0;
right:-5px; /* 10 px from the right of element inner-margin (padding) see example */
background:url() right center no-repeat;
}
Example: The element eg. a square sized 100px (without considering borders) has a 10px padding and a background image should be shown inside the right padding. This means the pseudo-element is a 80px sized square. We want to stick it to the right border of the element with right:-10px;. If we'd like to have the background-image 5px away from the right border we need to stick the pseudo-element 5px away from the right border of the element with right:-5px;... Test it for your self here : http://jsfiddle.net/yHucT/