Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个我想要在中心的 div 的背景图像,但略有偏移。如果我这样做background-position: x y;,它不会在页面变宽时保持居中。
background-position: x y;
您可以使用百分比来按比例指定背景位置,如下所示:
element { background-image: url(' ... '); background-repeat: no-repeat; background-position: 55% 50%; }
见jsFiddle
将此添加到您的CSS
background-position{55% 0%;}
你不能。你要么得到中心,要么得到偏移,但不能两者兼而有之。甚至 % 也不会按您期望的方式工作。如果图像和容器大小相同,则 % 偏移量完全没有任何作用。CSS背景图像定位很糟糕。
有时,您可以做的实际上是抵消图像本身。在一侧或另一侧使用额外的透明像素保存它 - 然后将结果居中。