HTML\CSS:背景图片定位
我知道我们可以这样定位
background:url(../images/bg.jpg) top left;
,但我想将
背景定位到顶部中心并将其向右移动 400 像素...如何才能完成
i know we can position like this
background:url(../images/bg.jpg) top left;
but i want to do this
position the background to top center and move it 400 pixels to right... how can it be done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
目前无法使用经典的背景位置表示法来完成此操作。
您必须使用 JavaScript 来计算元素的宽度并相应地设置位置,或者在图像左侧提供一些透明空间以将其推到右侧。这可能是最简单的解决方案。
This can't currently be done using classical
background-position
notation.You would have to use JavaScript to calculate the element's width and set the position accordingly, or give the image some transparent space to the left to push it to the right. This might be the easiest solution.
编辑:这在 IE 中很难做到,因此我添加了一些额外的信息以使其在 IE 中也能正常工作。
编辑2:
使用以下 CSS 嵌套您的对象:
您将拥有与此类似的代码(将
DOCTYPE
设置为 strict 很重要) ):根据您的图片更改childDiv中
width
和height
的值。Edit: This was stubborn to do in IE, so I've added some extra information to make it work there as well.
Edit2: Also used Microsoft's SuperPreview application to make this work in IE6. Tested in FF3.6 and IE8, as well as IE6 in SuperPreview.
Use the following CSS to nest your object:
You would have code similar to this (it's important to set
DOCTYPE
to strict):Change the value of
width
andheight
in childDiv based on your image.可以使用background-clip和background-origin来完成,但那是在未来: http://www.css3.info/preview/background-origin-and-background-clip/
包装 DIV 或 JavaScript 应该可以做到这一点。
It could be done with background-clip and background-origin, but that is in the future: http://www.css3.info/preview/background-origin-and-background-clip/
A wrapper DIV or JavaScript should do it.
我会尝试用
I would try to manipulate with <div> with positioning and z-index
用Javascript可能会更简单。
It may be simpler with Javascript.