CSS 将图像置于页面顶部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2878633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:07:37  来源:igfitidea点击:

CSS Put image at top of page

cssbackground-imagebackground-position

提问by nosedive25

I need to have an image at the top-center of a web page in css. Now, Im just using the background-image:in css but this puts it at the middle of the page.

我需要在 css 网页的顶部中心有一个图像。现在,我只是使用background-image:in css 但这将它放在页面的中间。

Here's my code:

这是我的代码:

body {
  background-image: url("theimageurlgoeshere"); //The image is 842 x 508
  background-attachment:fixed;
  background-position: center top;
  background-repeat: no-repeat;
}

Also, Changing the background-position:seems to have no effect on the outcome of the site. Any help is appreciated.

此外,更改background-position:似乎对网站的结果没有影响。任何帮助表示赞赏。

采纳答案by Riley

Change the arrangement of these values from center topto top center

将这些值的排列从 更改center toptop center

background-position: top center;

Tested in Safari 4, Chrome, and FF 3.5

在 Safari 4、Chrome 和 FF 3.5 中测试

The code I used in testing was:

我在测试中使用的代码是:

 body { background: url(./image.png) no-repeat top center; }

回答by Alec

And if you want it on a single line:

如果你想要它在一行上:

body {
  background: url('path') center top fixed no-repeat;
}

More info: http://www.w3schools.com/css/pr_background-position.asp

更多信息:http: //www.w3schools.com/css/pr_background-position.asp

回答by user2490985

If this dosen't work. Do margin:0 auto;

如果这不起作用。做保证金:0自动;