ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# CSS background-position 属性 ## 实例 如何定位背景图像: ``` body { background-image:url('bgimage.gif'); background-repeat:no-repeat; background-attachment:fixed; background-position:center; } ``` ## 浏览器支持 | IE | Firefox | Chrome | Safari | Opera | | --- | --- | --- | --- | --- | 所有浏览器都支持 background-position 属性。 注释:任何版本的 Internet Explorer (包括 IE8)都不支持属性值 "inherit"。 ## 定义和用法 background-position 属性设置背景图像的起始位置。 这个属性设置背景原图像(由 [background-image](/cssref/pr_background-image.asp "CSS background-image 属性") 定义)的位置,背景图像如果要重复,将从这一点开始。 提示:您需要把 background-attachment 属性设置为 "fixed",才能保证该属性在 Firefox 和 Opera 中正常工作。 | 默认值: | 0% 0% | | --- | --- | | 继承性: | no | | --- | --- | | 版本: | CSS1 | | --- | --- | | JavaScript 语法: | _object_.style.backgroundPosition="center" | | --- | --- | ## 可能的值 | 值 | 描述 | | --- | --- | | * top left * top center * top right * center left * center center * center right * bottom left * bottom center * bottom right | 如果您仅规定了一个关键词,那么第二个值将是"center"。 默认值:0% 0%。 | | x% y% | 第一个值是水平位置,第二个值是垂直位置。 左上角是 0% 0%。右下角是 100% 100%。 如果您仅规定了一个值,另一个值将是 50%。 | | xpos ypos | 第一个值是水平位置,第二个值是垂直位置。 左上角是 0 0。单位是像素 (0px 0px) 或任何其他的 CSS 单位。 如果您仅规定了一个值,另一个值将是50%。 您可以混合使用 % 和 position 值。 | ## TIY 实例 [如何放置背景图像](/tiy/t.asp?f=csse_background-position) 本例演示如何在页面上放置背景图像。 ``` <html> <head> <style type="text/css"> body { background-image:url('/i/eg_bg_03.gif'); background-repeat:no-repeat; background-attachment:fixed; background-position:center; } </style> </head> <body> <body> <p><b>提示:</b>您需要把 background-attachment 属性设置为 "fixed",才能保证该属性在 Firefox 和 Opera 中正常工作。</p> </body> </body> </html> ``` [如何使用%来定位背景图像](/tiy/t.asp?f=csse_background-position_percent) 本例演示如何使用百分比来在页面上定位背景图像。 ``` <html> <head> <style type="text/css"> body { background-image: url('/i/eg_bg_03.gif'); background-repeat: no-repeat; background-attachment:fixed; background-position: 30% 20%; } </style> </head> <body> <p><b>注释:</b>为了在 Mozilla 中实现此效果,background-attachment 属性必须设置为 "fixed"。</p> </body> </html> ``` [如何使用像素来定位背景图像](/tiy/t.asp?f=csse_background-position_pixel) 本例演示如何使用像素来在页面上定位背景图像。 ``` <html> <head> <style type="text/css"> body { background-image: url('/i/eg_bg_03.gif'); background-repeat: no-repeat; background-attachment:fixed; background-position: 50px 100px; } </style> </head> <body> <p><b>注释:</b>为了在 Mozilla 中实现此效果,background-attachment 属性必须设置为 "fixed"。</p> </body> </html> ``` ## 相关页面 CSS 教程:[CSS 背景](/css/css_background.asp "CSS 背景") CSS 参考手册:[background-image 属性](/cssref/pr_background-image.asp "CSS background-image 属性") HTML DOM 参考手册:[backgroundPosition 属性](/jsref/prop_style_backgroundposition.asp "HTML DOM backgroundPosition 属性")