1

这是我的CSS:

.header
{
    background-image:url(Images/head.png);
    background-position: center top;
    background-repeat:no-repeat;
    width:1010px;
    height:269px;
}

这是我的 HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>xSky Software - Most likely the only software on Clickbank that exists.</title>
<link href="style.css" rel="stylesheet" type="text/css" /> 
</head>
<body>

<div class="header"></div>
</div>
</body>
</html>

我试图让我的图像以 X 轴为中心,并在 Y 轴上置顶。但是我的 CSS 类.header不会那样做。你能看到我做错了什么吗?

编辑:转动顶部中心也不起作用

4

2 回答 2

2

你的background-position价值观是错误的。它应该是:

background-position: 50% 0; /* Short values FTW! */

不要忘记,如果你想让它保持居中,你还需要做一些事情,比如将内容包装在一个容器中并使用margin:0 auto它来保持一切居中。

HTML:

<div class="wrap">
    <div class="header"></div>
</div>

CSS:

.wrap {
    width:1010px;
    margin:0 auto;
}
.header {
    width:1010px;
    height:265px;
    background:transparent url('//placehold.it/1010x265') no-repeat;
}

演示:jsfiddle.net/UGDxU/show(或编辑它

于 2011-05-22T11:40:28.807 回答
1

的顺序background-positionleft top。尝试翻转值。

您还需要background-repeat: no-repeat.

js小提琴

于 2011-05-22T11:40:10.867 回答