2011-05-22 54 views
1

這裏是我的CSS: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不會那樣做。你能看到我做錯了什麼嗎?

編輯:打開頂部中心周圍無法正常工作或

+0

所以'.header'圖像顯示,只是不保持中心? – Marcel 2011-05-22 11:46:18

+0

@Marcel正確 – Jeff 2011-05-22 11:49:55

回答

1

background-position順序是left top。嘗試翻轉這些值。

您還需要background-repeat: no-repeat

jsFiddle

+0

它必須居中 - 上面有我已經有的東西。 :P – Jeff 2011-05-22 11:41:07

+0

@Jeff現在看小提琴。那是你的追求? – alex 2011-05-22 11:43:28

+0

@亞歷確實,確切! – Jeff 2011-05-22 11:45:46

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