Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all, a disclaimer. I don't really advocate for the solution I present below. The only browser specific CSS I write is for IE (especially IE6), although I wish it wasn't the case.</p> <p>Now, the solution. You asked it to be elegant so I don't know how elegant is it but it's sure going to target Gecko platforms only.</p> <p>The trick is only working when JavaScript is enabled and makes use of Mozilla bindings (<a href="https://developer.mozilla.org/en/XBL/XBL_1.0_Reference" rel="noreferrer">XBL</a>), which are heavily used internally in Firefox and all other Gecko-based products. For a comparison, this is like the behavior CSS property in IE, but much more powerful.</p> <p>Three files are involved in my solution:</p> <ol> <li>ff.html: the file to style</li> <li>ff.xml: the file containg the Gecko bindings</li> <li>ff.css: Firefox specific styling</li> </ol> <p><strong>ff.html</strong></p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;style type="text/css"&gt; body { -moz-binding: url(ff.xml#load-mozilla-css); } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;This should be red in FF&lt;/h1&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>ff.xml</strong></p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;bindings xmlns="http://www.mozilla.org/xbl"&gt; &lt;binding id="load-mozilla-css"&gt; &lt;implementation&gt; &lt;constructor&gt; &lt;![CDATA[ var link = document.createElement("link"); link.setAttribute("rel", "stylesheet"); link.setAttribute("type", "text/css"); link.setAttribute("href", "ff.css"); document.getElementsByTagName("head")[0] .appendChild(link); ]]&gt; &lt;/constructor&gt; &lt;/implementation&gt; &lt;/binding&gt; &lt;/bindings&gt; </code></pre> <p><strong>ff.css</strong></p> <pre><code>h1 { color: red; } </code></pre> <p><strong>Update:</strong> The above solution is not that good. It would be better if instead of appending a new LINK element it will add <em>that</em> "firefox" class on the BODY element. And it's possible, just by replacing the above JS with the following:</p> <pre><code>this.className += " firefox"; </code></pre> <p><strong>The solution is inspired by <a href="http://dean.edwards.name/moz-behaviors/" rel="noreferrer">Dean Edwards' moz-behaviors</a>.</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload