2015-04-18 48 views
-1

我有一個div,用於背景,具有設置的寬度和高度。尺寸爲1280x960,對於div和所使用的圖像。但在Firefox或IE中,div的寬度爲1600。我嘗試添加最大寬度,將填充設置爲0,我甚至進行了雙重檢查以確保我的瀏覽器窗口未放大。我認爲我不需要爲寬度和寬度等任何-webkit-moz-添加項高度,但Chrome是三大顯示器中唯一一個顯示它的人。寬度設置爲1280的div在Firefox中顯示寬度爲1600,IE爲

我的代碼:

<style> 
#background 
{ 
    width: 1280px; 
    max-width: 1280px; 
    height: 960px; 
    background-image: url("img/background.png"); 
    position: relative; 
    margin-left: auto; 
    margin-right: auto; 
    padding: 0; 
} 
</style> 
</head> 
<body> 
    <div id="background"> 
    </div> 
</body> 
+1

我沒有看錯,http://codepen.io/anon/pen/aOoWMq。 –

+0

您的代碼也向我展示了1600像素寬的圖像。 :( –

+0

你確定你沒有使用任何插件的FF或可能放大視口? –

回答

0

這樣可以幫助你

#background { 
    width: expression(document.body.clientWidth > 1279 ? "1280px" : "auto"); 
    /* sets max-width for IE */ 
} 

#background { 
    max-width: 1280px; 
    /* this sets the max-width value for all standards-compliant browsers */ 
} 
+0

IE支持自版本7以來的最大寬度。 – Oriol

0

您需要設置background-repeat: no-repeat;的背景IMG也background-size:cover;

#background { 
    width: 1280px; 
    height:960px; 
    max-width: 1280px; 
    background-image: url("http://placehold.it/1280x960"); 
    background-size:cover; 
    background-repeat:no-repeat; 
    position: relative; 
    margin:0 auto; 
    padding: 0; 
} 

DEMOhttp://jsfiddle.net/a_incarnati/mLxn46kj/5/

+0

背景是如何與寬度相關的? – Oriol

+0

Ops,true :)謝謝! –

+0

我知道你的意思,但我沒有看到代碼中的任何錯誤。也許這是她在Firefox上緩存的,或者是在打破的代碼中。當所顯示的代碼只有幾行而不是整個代碼的jsfiddle時,會發生這種情況。 –

相關問題