我认为答案是否定的,但是你可以用CSS定位一个背景图像,这样它就离右边有一个固定数量的像素?

如果我设置x和y的背景位置值,似乎它们只分别从左边和上面给出固定的像素调整。


当前回答

你可以在CSS3中这样做:

background-position: right 20px bottom 20px;

它适用于Firefox, Chrome, IE9+

来源:中数

其他回答

可以使用属性边框作为从右边开始的长度

background: url('/img.png') no-repeat right center;
border-right: 10px solid transparent;

这在Chrome 27中工作,我不知道它是否有效,也不知道其他浏览器能做什么。我对此感到很惊讶。

background: url(../img/icon_file_upload.png) top+3px right+10px no-repeat;

另一个我没有看到提到的解决方案是使用伪元素,我相信这个解决方案将适用于任何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/

图像工作与透明像素在右边作为右边缘。

图像的变通方法是创建一个PNG或GIF图像(支持透明度的图像文件格式),在图像的右侧有一个透明的部分,恰好等于你想要给予右边距的像素数(例如:5px, 10px等)。

这在固定宽度和百分比宽度上都能很好地工作。 实际上,这是一个很好的解决方案,手风琴头有一个加/减或向上/向下箭头图像在头的右边!

缺点:不幸的是,你不能使用JPG,除非容器的背景部分和CSS背景图像的背景颜色是相同的平面颜色(没有渐变/小插图),主要是白色/黑色等。

对大家都好

background: url('../images/bg-menu-dropdown-top.png') left 20px top no-repeat !important;