我认为答案是否定的,但是你可以用CSS定位一个背景图像,这样它就离右边有一个固定数量的像素?
如果我设置x和y的背景位置值,似乎它们只分别从左边和上面给出固定的像素调整。
我认为答案是否定的,但是你可以用CSS定位一个背景图像,这样它就离右边有一个固定数量的像素?
如果我设置x和y的背景位置值,似乎它们只分别从左边和上面给出固定的像素调整。
当前回答
这在Chrome 27中工作,我不知道它是否有效,也不知道其他浏览器能做什么。我对此感到很惊讶。
background: url(../img/icon_file_upload.png) top+3px right+10px no-repeat;
其他回答
这个问题已经被问到很久了,但我刚刚遇到了这个问题,我是这样做的:
background-position:95% 50%;
从左边调整百分比对我来说有点脆弱。当我需要这样的东西时,我倾向于将我的容器样式添加到包装器元素中,然后在内部元素上应用background-position: right bottom的背景
<style>
.wrapper {
background-color: #333;
border: solid 3px #222;
padding: 20px;
}
.bg-img {
background-image: url(path/to/img.png);
background-position: right bottom;
background-repeat: no-repeat;
}
.content-breakout {
margin: -20px
}
</style>
<div class="wrapper">
<div class="bg-img">
<div class="content-breakout"></div>
</div>
</div>
content-breakout类是可选的,如果需要,它将允许您的内容进入填充(负边距值应该与包装器填充中的相应值匹配)。这有点冗长,但工作可靠,无需考虑图像相对于其宽度和高度的相对位置。
如果你只想指定x轴,你可以这样做:
background-position-x: right 100px;
求负值的解。调整右填充来移动图像。
<div style='overflow:hidden;'>
<div style='width:100% background:url(images.jpg) top right; padding-right:50px;'>
</div>
</div>
另一个我没有看到提到的解决方案是使用伪元素,我相信这个解决方案将适用于任何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/