2011-12-12 45 views
1

我有一個圖像,我必須用於我的標題,我不能使用固定圖像作爲標題寬度根據標題文本而變化,因此我做了3片圖像,重複,中間片將重複與文本和結束切片圖像,應該關閉標題,我已經包括了圖像和我正在使用的CSS,結束切片不起作用,任何人都可以告訴我我做錯了什麼:爲標題使用背景圖像

<div class="product-item"> 
<div class="product-title"> 

<span><h3>@Model.Name</h3></span> 

</div> 

</div> 

CSS:

.product-grid .product-item .product-title 
{ 
background-image: url('images/first-slice.png'); 
background-repeat: no-repeat; 
background-position: 0% 0%; 
height:37px; 
} 
.product-grid .product-item .product-title span 
{ 
background-image: url('images/last-slice.png'); 
background-repeat: no-repeat; 
background-position: 100% 50%; 
height:37px; 
} 

.product-grid .product-item .product-title h3 
{ 
background-position: 50%; 
background-image: url('images/middle-slice.png'); 
background-repeat: repeat-x; 
height:37px; 
margin-left:5px; 
} 

現場測試網站:Website

首先圖片:First-slice 中圖片:enter image description here 結束切片:enter image description here

回答

1

你應該改變:

<span><h3>@Model.Name</h3></span> 

到:

<div><h3>@Model.Name</h3></div> 

的問題是,span是內聯元素,在其上設置height將不起作用。

<span><h3>@Model.Name</h3></span>也是無效的HTML。


正如@ptriek所指出的那樣,您還需要交換元素之間背景的順序。

這是你需要的CSS(假設你改變了spandiv):

.product-grid .product-item .product-title 
{ 
    background-image: url('images/middle-slice.png'); 
    background-repeat: repeat-x; 
} 
.product-grid .product-item .product-title div 
{ 
    background-image: url('images/first-slice.png'); 
    background-repeat: no-repeat; 
    background-position: left; 
} 
.product-grid .product-item .product-title h3 
{ 
    background-image: url('images/last-slice.png'); 
    background-repeat: no-repeat; 
    background-position: right; 
    height: 37px; 
    margin-left: 5px; 
} 
+0

我只想補充的高端形象,ü意味着應該增加而不是跨度有點像另一個類:

...

,然後添加就可以了 –

+0

的風格,雖然它的語義上不正確的有一個跨度H3,這是這個問題的不是原因 - H3的高度,使得跨接管37px高度 – ptriek

+0

所以正確的方法是將包一切的div然後添加造型 –

2

的H3(含重複中圖)坐落在跨度的頂部,包含你的右圖像。

在h3上設置正確的圖像,並在跨度上設置重複的圖像 - 應該修復它。

而且跨度更好地改變一個div,因爲跨度不能包含塊元素,如H3(語義上不正確)

+0

但採用div新的塊級元素將被創建,我被避免,通過包括跨越只是爲了改變風格和最後的形象.. –

+0

添加塊級元素而不是內聯元素會更糟糕嗎? html規則聲明內聯元素(如跨度)不能包含塊級元素(如h3)。你的代碼工作,但它只是不正確的... – ptriek

+0

我試圖使用div內的另一個div,但圖像寬度不會改變取決於文本。 –

1

添加更多的div,它會更容易:

<div id=wrapper> 
<div id="bg_header">&nbsp;</div> 
    <div id="content"> 
    &nbsp; 
    </div> 
<div id="bg_footer">&nbsp;</div> 
</div> 

我我已經在很多項目中完成了這項工作,並且這適用於所有主要的瀏

3

主要問題是h3元素與包含span的寬度相同。此span也被設置爲display:block,以使background-position正常工作。

但是,正如評論中所說,爲了讓HTML也有效,這個跨度必須改爲div。

然後,我剛剛在h3元素中添加了margin-right:5px

最後,爲了使文本垂直居中,請使用line-height而不是height

.product-grid.product-item.product-title 
{ 
    background-image: url('images/last-slice.png'); 
    background-repeat: no-repeat; 
    background-position: 100%; 
    line-height:37px; 
} 

.product-grid.product-item.product-title h3 
{ 
    background-position: 50%; 
    background-image: url('images/middle-slice.png'); 
    background-repeat: repeat-x; 
    line-height:height:37px; 
    margin-left:5px; 
    margin-right:5px; 
} 

.product-grid.product-item.product-title div { 
    background-image: url('images/first-slice.png'); 
    background-repeat: no-repeat; 
    background-position: 0%; 
    line-height:37px; 
} 
+0

的HTML *應*改變,因爲[它是無效的(http://validator.w3.org/check?uri = HTTP%3A%2F%2Fjsbin.com%2Felavid&字符集=%28detect +自動%29&DOCTYPE =內嵌&組= 0&用戶代理= W3C_Validator%2F1.2)。 – thirtydot

+0

那麼,謝謝你的評論,我擴展了它。 –