在我的课上,我玩了一下,发现CSS与虚构的元素一起工作。
例子:
imsocool { 颜色:蓝色; } < imsocool >你好< / imsocool >
当我的教授第一次看到我使用它时,他有点惊讶于虚构元素的工作,并建议我简单地将所有的虚构元素更改为带有ID的段落。
为什么我的教授不让我使用合成元素?他们工作效率很高。
另外,为什么他不知道存在虚构元素,并与CSS一起工作。它们不常见吗?
在我的课上,我玩了一下,发现CSS与虚构的元素一起工作。
例子:
imsocool { 颜色:蓝色; } < imsocool >你好< / imsocool >
当我的教授第一次看到我使用它时,他有点惊讶于虚构元素的工作,并建议我简单地将所有的虚构元素更改为带有ID的段落。
为什么我的教授不让我使用合成元素?他们工作效率很高。
另外,为什么他不知道存在虚构元素,并与CSS一起工作。它们不常见吗?
当前回答
为什么CSS使用假元素?
(大多数)浏览器被设计成(在某种程度上)向前兼容未来添加的HTML。未识别的元素被解析到DOM中,但没有与它们相关的语义或专门的默认呈现。
当一个新元素被添加到规范中时,有时CSS、JavaScript和ARIA可以用来在旧的浏览器中提供相同的功能(元素必须出现在DOM中,以便这些语言能够操作它们来添加该功能)。
(有一个自定义元素的规范,但是它们有特定的命名要求,并且需要使用JavaScript进行注册。)
为什么我的教授不让我使用合成元素?
它们不被HTML规范所允许 它们可能与将来同名的标准元素冲突 可能有一个现有的HTML元素更适合这个任务
也;为什么他不知道构成元素的存在并与CSS一起工作。它们不常见吗?
是的。人们不使用它们是因为它们有上述问题。
其他回答
为什么CSS使用假元素?因为它不会伤害任何人因为你不应该使用它们。
为什么我的教授不让我使用合成元素?因为如果该元素在未来由规范定义,那么您的元素将具有不可预测的行为。
另外,为什么他不知道存在虚构元素,并与CSS一起工作。它们不常见吗?因为他和大多数其他web开发人员一样,明白我们不应该使用将来可能会随机损坏的东西。
实际上你可以使用自定义元素。下面是关于这个主题的W3C规范:
http://w3c.github.io/webcomponents/spec/custom/
这里有一个教程来解释如何使用它们:
http://www.html5rocks.com/en/tutorials/webcomponents/customelements/
正如@Quentin所指出的:这是开发早期的规范草案,它对元素名称施加了限制。
他为什么不想让你用?它们既不常见,也不是HTML5标准的一部分。 从技术上讲,这是不允许的。他们就是个笨蛋。
不过我自己也喜欢。您可能对XHTML5感兴趣。它允许您定义自己的标记,并将它们作为标准的一部分使用。
此外,正如其他人指出的那样,它们是无效的,因此不能携带。
为什么他不知道他们的存在?我不知道,只知道它们不常见。可能他只是不知道你可以。
YADA(又一个(不同的)答案)
编辑:请参阅下面BoltClock关于类型、标签和元素的评论。我通常不担心语义学,但他的评论非常合适,信息量很大。
Although there are already a bunch of good replies, you indicated that your professor prompted you to post this question so it appears you are (formally) in school. I thought I would expound a little bit more in depth about not only CSS but also the mechanics of web browsers. According to Wikipedia, "CSS is a style sheet language used for describing ... a document written in a markup language." (I added the emphasis on "a") Notice that it doesn't say "written in HTML" much less a specific version of HTML. CSS can be used on HTML, XHTML, XML, SGML, XAML, etc. Of course, you need something that will render each of these document types that will also apply styling. By definition, CSS does not know / understand / care about specific markup language tags. So, the tags may be "invalid" as far as HTML is concerned, but there is no concept of a "valid" tag/element/type in CSS.
现代的可视化浏览器不是单一的程序。它们是不同“引擎”的混合体,各自承担着特定的任务。至少我能想到3个引擎,渲染引擎,CSS引擎和javascript引擎/VM。不确定解析器是否是呈现引擎的一部分(反之亦然),或者它是否是一个单独的引擎,但您可以理解。
Whether or not a visual browser (others have already addressed the fact that screen readers might have other challenges dealing with invalid tags) applies the formatting depends on whether the parser leaves the "invalid" tag in the document and then whether the rendering engine applies styles to that tag. Since it would make it more difficult to develop/maintain, CSS engines are not written to understand that "This is an HTML document so here are the list of valid tags / elements / types." CSS engines simply find tags / elements / types and then tell the rendering engine, "Here are the styles you should apply." Whether or not the rendering engine decides to actually apply the styles is up it.
这里有一个简单的方法来考虑从引擎到引擎的基本流程:解析器-> CSS ->渲染。实际上,它要复杂得多,但这对于初学者来说已经足够好了。
这个答案已经太长了,所以我就到此为止。
我认为编造的标签可能比带有id的p更令人困惑或不清楚(通常是一些文本块)。我们都知道带ID的p是一个段落,但是谁知道创建的标记是用来干什么的呢?至少我是这么想的。:)因此,这更多的是一个风格/清晰度问题,而不是功能问题。