我认为答案是否定的,但是你可以用CSS定位一个背景图像,这样它就离右边有一个固定数量的像素?
如果我设置x和y的背景位置值,似乎它们只分别从左边和上面给出固定的像素调整。
我认为答案是否定的,但是你可以用CSS定位一个背景图像,这样它就离右边有一个固定数量的像素?
如果我设置x和y的背景位置值,似乎它们只分别从左边和上面给出固定的像素调整。
当前回答
据我所知,CSS规范并没有提供您所要求的内容,当然,除了CSS表达式之外。除去你不想使用表达式或Javascript的假设,我看到了三个hack的解决方案:
Make sure your background image matches the size of the container (at least in width) and set background-repeat: repeat or repeat-x if only the width is equalized. Then, having something appear x pixels from the right is as simple as background-position: -5px 0px. Using percentages for background-position exhibits special behaviour that is better seen than described here. Give it a shot. Essentially, background-position: 90% 50% will make the right edge of the background image line up 10% away from the right edge of the container. Create a div containing the image. Explicitly set the position of the containing element position: relative if not already set. Set the image container to position: absolute; right: 10px; top: 10px;, obviously adjusting the final two as you see fit. Place the image div container into the containing element.
其他回答
可以使用属性边框作为从右边开始的长度
background: url('/img.png') no-repeat right center;
border-right: 10px solid transparent;
如果容器有固定高度: 微调百分比(背景位置),直到它正确匹配。
如果容器有一个动态高度: 如果你想在你的背景和容器之间填充(比如当自定义样式输入时,选择),将你的填充添加到你的图像,并将背景位置设置为右侧或底部。
当我试图让一个选择框的背景适合我选择的右边5像素时,我偶然发现了这个问题。在我的例子中,我的背景是一个箭头,它将取代基本的下拉图标。在我的例子中,背景的填充将始终保持不变(右边5-10个像素),所以对实际的背景图像进行修改是很容易的(使右侧的尺寸宽5-10个像素)。
希望这能有所帮助!
最合适的答案是background-position的新四值语法,但在所有浏览器都支持它之前,最好的方法是按照以下顺序组合之前的响应:
background: url(image.png) no-repeat 97% center; /* default, Android, Sf < 6 */
background-position: -webkit-calc(100% - 10px) center; /* Sf 6 */
background-position: right 10px center; /* Cr 25+, FF 13+, IE 9+, Op 10.5+ */
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>