문제

According to the W3C, user important style declarations are supposed to have the highest priority, higher than author important declarations, but I'm not seeing that happen. If you go to jsfiddle (intentionally blank, I'm referring to the site itself), and look at the styling for the iframe, you'll see the following:

#content textarea, #content iframe
{
    background: none repeat scroll 0 0 #FFFFFF;
    border: 0 none !important;
    box-shadow: 0 1px 3px #E4E4E4 inset;
}

I made a user style (using stylish) with the following css:

@namespace url(http://www.w3.org/1999/xhtml);

@-moz-document domain("jsfiddle.net") {
    iframe
    {
       border: 4px solid red !important;
    }
}

When I applied it, nothing happened. If I use firebug to disable the rule or remove the !important specified by jsfiddle, it works. It also works if I change the selector in my user style to #content iframe.

W3C specifically states: 3. Sort rules with the same importance and origin by specificity of selector Since the user style rule should have higher importance, specificity shouldn't have any effect here, so why does the style not apply when using only iframe as the selector?

(tested using firefox 24.2 in case that matters)


Since I haven't gotten an answer, let me give an actual example of what I'm trying to do, and why changing the selector won't help. Here's a dabblet demonstrating the exact html/css/js I'm dealing with.

The following userstyle properly applies a red border, but has no effect on the text color.

@-moz-document domain("preview.dabblet.com"){
    #test 
    {
       color: white !important;
       border: 1px solid red;
    }
}

Using a userstyle, how can I force the text to always be white?

도움이 되었습니까?

해결책

You are correct that an !important declaration of origin "user" should take precedence over any declaration of origin "author", regardless of importance or specificity. However you are making an assumption that Stylish applies its styles with the "user" origin.

Since Stylish 1.4.1 for Firefox, it will apply styles with "author" origin by default. One reason for this change was compatibility with Stylish for other browsers. Their APIs only allow Stylish to add "author" origin styles, which meant that a style that worked in Firefox didn't work in Chrome. Yours is one example of where this would be the case.

The best way to fix this (and to ensure compatibility with other browsers, should you share your style on userstyles.org), is to increase the specificity of your selector to something greater than that of the site's CSS. The simplest way to do so would be to use the same selector as the site, but add a body type selector at the start:

@namespace url(http://www.w3.org/1999/xhtml);

@-moz-document domain("jsfiddle.net") {
  body #content iframe {
    border: 4px solid red !important;
  }
}

There are cases where this isn't feasible: a style that affects iframes on many sites that couldn't be so specific with its selector, or a style trying to override an !important declaration inside an HTML style attribute. Stylish for Firefox allows you to switch your style to the "agent" origin with a special comment: /* AGENT_SHEET */. This will have the effect of your !importants beating anything the site can do (much like the "user" origin), but it will not work in other browsers and can cause bad things like crashes, so this is only suggested if the above method is completely unworkable for you.

All of this is described on Stylish's wiki along with some info less relevant to your situation.

다른 팁

You're right on with the specificity idea. The problem is both your rule and jsfiddle's rule use !important which means both rules have the same priority, but the #content textarea, #content iframe rule is more specific.

To solve, you could write your rule as:

#content iframe {
  border: 4px solid red !important;
}

See this for more details: http://www.html5rocks.com/en/tutorials/internals/howbrowserswork/#CSS_parsing

That section will give you what you need, but the whole article is extremely interesting.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top