如何将背景图像放置在偏离中心的位置?
我有一个背景图像设置为中心并且需要固定位置。不过我想将它稍微偏离中心 100px,有什么想法吗?
CSS 背景:url(styles/images/bg.png) 无重复中心0; 背景附件:固定;
I have a background image set to center and needs to be fixed position. However I would like to just nudge it 100px off center, any ideas?
CSS
background: url(styles/images/bg.png) no-repeat center 0;
background-attachment: fixed;
This is the working sample
http://www.warface.co.uk/clients/detail-shoppe/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建宽 300 像素(分别为左或右)的较大背景图像将使背景从中心移动 150 像素。背景属性中没有 css 样式可以执行此操作。如果您不想创建更多 div,这是唯一的方法。
Creating a larger background image that is 300px wider (left or right respectively) will shift the background 150px from center. There is no css style in the background property that will do this. If you don't want to create more div's, this is the only way.
您可以通过将背景放置在距左侧或右侧接近 50% 的位置来实现此目的。如果您需要距中心左/右特定数量的像素,则需要使用 calc() 来添加不同单位的偏移量。例如,如果您希望背景从中心向左移动 100 像素,则需要使用
background-position: left calc(50% - 100px)
。请特别注意括号内的空格,因为它们是强制性的。如果您希望距离背景图像边缘和中心线有 100 个像素,则还需要减去背景图像宽度的一半。为了进一步详细说明这一点(如果它有用的话),我试图解决两个单独的场景。我一直在寻找如何在不更改任何 HTML 的情况下始终将背景图像定位在响应元素的一侧。该元素始终位于页面中央,并根据屏幕尺寸更改宽度。第一个示例符合您的问题,即该元素的宽度是固定的像素数,因此我的背景图像除了背景图像宽度的一半之外,还必须是中心左侧像素数的一半(这就是第一段描述)。
第二种情况是主要元素占页面大小的 85%。这使得左侧边距为 7.5%,但要正确处理此问题,您需要将
background-position
设置为以下内容:calc(7.5% - var(--image-width) * 0.925)
(将var(--image-width)
替换为以像素为单位的背景图像宽度,0.925 是 1 与小数表示形式 7.5% 之间的差值,或 <代码>1 - 0.075)。以下是一些示例:
You can do this by positioning your background something close to 50% from the left or right. If you need it a specific number of pixels left/right from the center, you will need to use
calc()
to add an offset of a different unit. For instance if you want the background shifted 100 pixels left from bein center, you'll need to usebackground-position: left calc(50% - 100px)
. Pay special attention to the spacing inside the parentheses as they are mandatory. If you want there to be 100 pixels from the edge of the background image and the center line, you'll also need to subtract half the width of the background image.To elaborate on this further (in case it's useful), I had two separate scenarios I was trying to solve. I was looking for how to position a background image to start at the side of a responsive element at all times without changing any HTML. The element was centered on the page at all times and changed widths depending on the screen size. The first example matches your question, which is that the element was a fixed number of pixels wide so my background image had to be half that number of pixels to the left of center in addition to half the width of the background image (this is what the first paragraph describes).
The second scenario is that the main element was 85% of the page size. This makes the margin on the left side 7.5%, but to correctly handle this you need to set
background-position
to the following:calc(7.5% - var(--image-width) * 0.925)
(replacingvar(--image-width)
with the background image width in units of pixels and 0.925 being the difference between 1 and the decimal representation of 7.5%, or1 - 0.075
).Here are some examples:
看起来
background-position
就是您正在寻找的。查看此链接了解更多详细信息
looks like
background-position
is what you are looking for.Check out this link for more details