如何在底部右侧定位背景图像?(CSS)

66
我有一张500x500像素的图片,它被设置为我的网站的背景图片(作为<body>的背景图像)。但是我需要将这张图片放在网页的右下角。如何实现这一目标?(最好使用CSS方法)
谢谢。
4个回答

129

这就是:

body {
   background-color: #000; /*Default bg, similar to the background's base color*/
   background-image: url("bg.png");
   background-position: right bottom; /*Positioning*/
   background-repeat: no-repeat; /*Prevent showing multiple background images*/
}

背景属性可以结合在一起,放在一个背景属性中。

另请参见:https://developer.mozilla.org/en/CSS/background-position


1
谢谢,你今天帮了我很多))) - Ilja
@Legionar 对我来说很好,在Chrome 43和Firefox 38中。 - Rob W
抱歉,是我的错,它本来是可以工作的,但是 jquerybottom 改成了 0... :-/ - Legionar
2
我不得不添加 background-attachment: fixed; 才能使其正常工作。 - Brian Risk
如果body元素的大小小于视口/内容大小,则可能无法按预期工作。根据评论中的建议添加background-attachment: fixed;可以避免这个问题。 - Brian Moeskau

24

更精确地定位:

      background-position: bottom 5px right 7px;

3
感谢您提供示例,演示如何通过像素微调背景图像对“停靠点”的对齐方式。 - Dimitry K
1
这是一种更灵活的方法。干得好。 - Usman Ahmed

13

8

您是否尝试过类似以下的操作:

body {background: url('[url to your image]') no-repeat right bottom;}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接