2015-05-28 107 views
0

不嵌合我有固定的寬度和高度的一個div如下:CSS背景 - 在一個div

<div class="fontTest">A</div> 

和我製成尺寸800像素* 169px的JPEG背景圖像以適應這個div。以下是我正在使用的CSS:

.fontTest { 
    font-family:'Noto Serif', serif; color:#744801; 
    margin-left:auto; 
    width:800px; 
    height:169px; 
    margin-right:auto; 
    background: #FDEEEE url('img/bkg1.jpg') 0 0 no-repeat fixed; 
} 

我使用速記背景CSS屬性來定義1行中的所有背景屬性。由於圖像的寬度和高度與div的寬度和高度相匹配 - 圖像應完全覆蓋div,但這種情況不會發生。背景圖片僅部分覆蓋了div(shown in the attached image)。請幫忙。

+0

試試'background-size:cover;' –

+0

就你而言,圖像的尺寸似乎與容器不同。所以,有一個空間。嘗試妥協容器或圖像的尺寸。或者使用'background-repeat:repeat-x'來水平重複背景。 –

+0

#fiddle會很好... – Nix

回答

1

在您的速記屬性聲明中刪除背景附件聲明fixed

Jsfiddle Demo

重新大小的撥弄的視圖以查看差。

此關鍵字意味着背景對於視口是固定的。即使元素具有滾動機制,「固定」背景也不會隨元素一起移動。

.fontTest { 
    font-family:'Noto Serif', serif; 
    color:#744801; 
    margin-left:auto; 
    width:800px; 
    height:169px; 
    margin-right:auto; 
    background: #FDEEEE url('http://lorempixel.com/output/city-q-c-800-169-4.jpg') 0 0 no-repeat; 
} 
+0

這完全有效。謝謝。我真的很想知道整個問題出現在背景位置屬性(0,0)的時候了。 這裏是一個小提琴 - https://jsfiddle.net/probosckie/cmx661e1/ 總得讀一點關於背景附件。 非常感謝! – Prabhas

0

使用

background-size: cover; 

和圖像將覆蓋整個股利。