【问题标题】:CSS background-position not applying to my classCSS背景位置不适用于我的班级
【发布时间】:2011-05-22 11:37:16
【问题描述】:

这是我的 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 不会那样做。你能看出我做错了什么吗?

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

【问题讨论】:

  • 所以.header 图像正在显示,只是没有保持居中?

标签: html css background-image background-position


【解决方案1】:

您的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(或edit it

【讨论】:

    【解决方案2】:

    background-position 的顺序是left top。尝试翻转值。

    您还需要background-repeat: no-repeat

    jsFiddle.

    【讨论】:

    • 它必须居中 - 上面有我已经拥有的东西。 :P
    • @Jeff 现在看小提琴。这就是你所追求的吗?
    • 查看我的编辑,应用您的更改,但它们不适用于我的页面
    • @Jeff 您可能没有正确实现它,因为您可以看到它在小提琴上确实有效。任何方式我都可以看到它在哪里实施?我确定我可以看到发生了什么:)
    • @Alex - 它只是在本地 - 通过应用边框,我看到它确实居中,但我的 DIV 标签也需要居中 - 我该怎么做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 2011-08-14
    相关资源
    最近更新 更多