2015-11-07 42 views
0

我想只爲一個瀏覽器設置div的高度,所以它在Firefox或其他方面看起來不奇怪。如何只定位一個瀏覽器?

下面是一個例子:

#example { 
 
    height: 200px; <!--How can I target Safari for example?--> 
 
    height: 250px; <!--How can I target Firefox for example?--> 
 
    height: 300px; <!--How can I target IE for example?--> 
 
    width: 250px; 
 
    background-color: #FFF; 
 
}
<div id="example"> 
 
    <img src="example.png"> 
 
    <p>Just some text.</p> 
 
    <p>Click <a href="www.example.com">here</a> to visit www.example.com</p> 
 
</div>

我已經嘗試過-moz-height: 250px;,但沒有奏效。

+0

其他可能有用的東西是['box-sizing'](http://www.paulirish.com/2012/box-sizing-border-box-ftw/),它可以指示每個瀏覽器應用高度和寬度以相同的方式。 –

+0

瀏覽器嗅探是一個非常糟糕的方法。在標記寫入正確的情況下,任何瀏覽器都不會顯示(多少)不同的頁面。 – Shikkediel

回答

0

爲此,您可以使用JavaScript。有一個字符串,您可以訪問navigator.appName。你可以把這樣的:

if(navigator.appName === "Google Chrome") 
    // Do whatever here 

與你的目標瀏覽器更換Microsoft Internet Explorer中

我真的希望這有助於!

0

您可以使用條件註釋。所以你告訴代碼爲特定的瀏覽器使用特定的樣式表。 Here's an article about it

下面是一個例子:

<link rel="stylesheet" href="normal_style.css"> 
<!--[if IE]><link rel="stylesheet" href="ie_style.css"><![endif]--> 

注:這只是與Internet Explorer的作品。如果您想爲其他瀏覽器執行此操作,則需要使用JavaScript。以下是JS的一個示例:

<link rel="stylesheet" id="stylesheet" href="normal_style.css"> 
if (navigator.appName === "Mozilla Firefox") { 
    document.getElementById("stylesheet").setAttribute("href", "special_style.css"); 
} 
0

您可以訪問導航器對象並獲取瀏覽器。

var nVer = navigator.appVersion; 
var nAgt = navigator.userAgent; 

然後

document.getElementById("example").style.height= y; 

「Y」 這是一個變量,其值依賴於瀏覽器的變化。