简体   繁体   English

为什么 <link> “标题”属性会导致浏览器忽略我的样式?

[英]Why does the <link> ‘title’ attribute cause browsers to ignore my styles?

The following html document (together with the CSS) fails to render the styles in b.css . 以下html文档(与CSS一起)无法在b.css呈现样式。

<!doctype html>
<html>
    <head>
        <link rel="stylesheet" media="screen" type="text/css" title="A" href="a.css" />
        <link rel="stylesheet" media="screen" type="text/css" title="B" href="b.css" />
    </head>
    <body>
        <div id="A">A</div>
        <div id="B">B</div>
    </body>
</html>

/* a.css */
div#A   { color: blue;  }
/* b.css */
div#B   { color: red;   }

Making the titles the same (eg both <link ... title="A"> fixes it, but I don't see the reason, why it should. What is the title doing, here, that makes this wrong? 使标题相同(例如,两个<link ... title="A">修复它,但是我不知道为什么要这么做。在这里,标题在做什么,这使此错误吗?

The HTML 4.0 spec states that there are three kinds of stylesheets : persistent, preferred, and alternate. HTML 4.0规范指出存在三种样式表 :持久样式表 ,首选样式表和替代样式表

  • A stylesheet is "persistent" if it is linked with rel="stylesheet" and has no title attribute. 如果样式表与rel="stylesheet"并且没有title属性,则它是“永久”的。 All persistent stylesheets are used when rendering. 渲染时使用所有持久样式表。
  • A stylesheet is "preferred" if it is linked with rel="stylesheet" and has a title attribute; 如果样式表与rel="stylesheet"并且具有title属性,则该样式表为“首选”; preferred stylesheets with the same title are grouped together, but there should not be more than one group. 具有相同title首选样式表会分组在一起,但最多只能有一组。 It seems browsers will choose just one preferred stylesheet to render since there should be only one. 似乎浏览器将只选择一种首选样式表进行呈现,因为应该只有一种。
  • Finally, a stylesheet is "alternate" if it is linked with rel="alternate stylesheet" and has a title . 最后,如果样式表与rel="alternate stylesheet"并且具有title则它是rel="alternate stylesheet" These stylesheets are supposed to allow the user to choose stylesheets, they are grouped together by title and show up in the browser's stylesheet selector if it has one (View >> Page Style in Firefox). 这些样式表应该允许用户选择样式表,它们按title分组在一起,并显示在浏览器的样式表选择器中(如果有的话)(在Firefox中查看>>页面样式)。 Each group (by title) is mutually exclusive. 每个组(按标题)是互斥的。

By putting title attributes on your stylesheets, you're unwittingly making them into preferred stylesheets rather than the expected persistent stylesheet. 通过将title属性放在样式表中,您会无意间将它们放入首选样式表中,而不是预期的持久性样式表中。 This is also why they work when they all have the same title. 这也是为什么当他们都拥有相同标题时他们会工作的原因。

Following up on Neil Williams' answer: 跟进尼尔·威廉姆斯的回答:

Authors may specify a number of mutually exclusive style sheets called alternate style sheets.... User agents should allow users to select from alternate style sheets. 作者可以指定许多互斥的样式表,称为替代样式表。...用户代理应允许用户从替代样式表中进行选择。

(emphasis added) (强调)

Also: 也:

To make a style sheet preferred, set the rel attribute to "stylesheet" and name the style sheet with the title attribute. 要使样式表成为首选,请将rel属性设置为“ stylesheet”,并用title属性命名样式表。

This is from http://www.w3.org/TR/REC-html40/present/styles.html#h-14.3.1 这是从http://www.w3.org/TR/REC-html40/present/styles.html#h-14.3.1

Read this: http://blogs.telerik.com/dimodimov/posts/08-05-15/title_attributes_in_css_link_tags_prevent_styles_from_being_applied.aspx 阅读此内容: http : //blogs.telerik.com/dimodimov/posts/08-05-15/title_attributes_in_css_link_tags_prevent_styles_from_being_applied.aspx

I have been aware for some time now that title attributes in CSS tags trigger problems and prevent some CSS styles from being applied on the web page. 我已经知道一段时间了,CSS标记中的标题属性会触发问题并阻止某些CSS样式应用于网页。 Today I invested a couple of hours in finding out what actually happens and this is what we've got. 今天,我花了几个小时来找出实际发生的事情,这就是我们所拥有的。

If you have several tags in the page and one of them has a title attribute, then the tags coming after it must either have a title attribute with the same value or no title attribute at all, otherwise the styles in the latter CSS files the will not be applied on the page. 如果页面中有多个标签,并且其中一个标签具有title属性,那么后面的标签必须具有具有相同值的title属性或完全没有title属性,否则后面的CSS文件中的样式将不适用于该页面。

The issue can be easily reproduced in various versions of Firefox, Opera and Safari. 可以在各种版本的Firefox,Opera和Safari中轻松重现该问题。 The only browser, which does not exhibit the unexpected behavior is Internet Explorer. 唯一没有出现意外行为的浏览器是Internet Explorer。

Really weird oo 真的很奇怪

It took me about 3 hours to get to the end of it, as I was adding a new stylesheet include. 我花了大约3个小时才结束,因为我要添加一个新的样式表include。 I thought I had some conflicts between present CSS rules and the ones I was adding. 我以为我现在的CSS规则和我要添加的规则之间有些冲突。 After running out of sane options, I tried removing the title tag from the link. 用尽理智的选项后,我尝试从链接中删除标题标签。 I didnt think much of it, thought it was some useless metadata. 我没有考虑太多,认为这是一些无用的元数据。 Well, it turned out this was what was preventing the CSS from being parsed. 好吧,事实证明这就是阻止CSS解析的原因。 Turns out the stale committee of brainfart CSS standards came up with this. 原来,过时的Brainfart CSS标准委员会提出了这个建议。 Couldn't they just have added a new attribute that is clearer, for their preferred and whatnot functionality (whatever the heck it's supposed to do) instead of relying on the presence or absence of the title attribute? 他们难道不是只是为自己的首选功能和什么功能(无论该怎么做)添加了一个更清晰的新属性,而不是依靠title属性的存在或不存在? I lost all that time that should be invested in doing something productive. 我把所有的时间都浪费在做富有成效的事情上了。 During my search, I promised myself that I would take measures once I find the origin of this problem. 在搜索过程中,我向自己保证,一旦找到此问题的根源,我将采取措施。 Now, remains to decide on the measures. 现在,还有待决定采取的措施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM