在我的课上,我玩了一下,发现CSS与虚构的元素一起工作。
例子:
imsocool { 颜色:蓝色; } < imsocool >你好< / imsocool >
当我的教授第一次看到我使用它时,他有点惊讶于虚构元素的工作,并建议我简单地将所有的虚构元素更改为带有ID的段落。
为什么我的教授不让我使用合成元素?他们工作效率很高。
另外,为什么他不知道存在虚构元素,并与CSS一起工作。它们不常见吗?
在我的课上,我玩了一下,发现CSS与虚构的元素一起工作。
例子:
imsocool { 颜色:蓝色; } < imsocool >你好< / imsocool >
当我的教授第一次看到我使用它时,他有点惊讶于虚构元素的工作,并建议我简单地将所有的虚构元素更改为带有ID的段落。
为什么我的教授不让我使用合成元素?他们工作效率很高。
另外,为什么他不知道存在虚构元素,并与CSS一起工作。它们不常见吗?
当前回答
我认为编造的标签可能比带有id的p更令人困惑或不清楚(通常是一些文本块)。我们都知道带ID的p是一个段落,但是谁知道创建的标记是用来干什么的呢?至少我是这么想的。:)因此,这更多的是一个风格/清晰度问题,而不是功能问题。
其他回答
其他人提出了很好的观点,但值得注意的是,如果你看看像AngularJS这样的框架,自定义元素和属性是非常有效的。它们不仅向xml传递更好的语义含义,而且还可以为web页面提供行为、外观和感觉。
人们很少使用自制的标记,因为它们不太可能在当前的每个浏览器和未来的每个浏览器中都可靠地工作。
浏览器必须将HTML代码解析为它所知道的元素,然后将生成的标记转换为适合文档对象模型(DOM)的其他内容。由于web标准没有涵盖如何处理标准之外的所有内容,web浏览器倾向于以不同的方式处理非标准代码。
在不增加不确定性元素的情况下,使用一堆不同的浏览器进行Web开发已经足够棘手了,这些浏览器都有自己的怪癖。最好的办法是坚持那些标准中的东西,那是浏览器供应商试图遵循的,所以它有最好的机会实际工作。
实际上你可以使用自定义元素。下面是关于这个主题的W3C规范:
http://w3c.github.io/webcomponents/spec/custom/
这里有一个教程来解释如何使用它们:
http://www.html5rocks.com/en/tutorials/webcomponents/customelements/
正如@Quentin所指出的:这是开发早期的规范草案,它对元素名称施加了限制。
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 ->渲染。实际上,它要复杂得多,但这对于初学者来说已经足够好了。
这个答案已经太长了,所以我就到此为止。
令人惊讶的是,没有人(包括我过去的自己)提到可访问性。使用有效标记而不是自定义标记的另一个原因是为了与最大数量的软件兼容,包括屏幕阅读器和人们出于可访问性目的所需的其他工具。此外,像WAI这样的可访问性法律要求建立可访问的网站,这通常意味着要求他们使用有效的标记。
显然没人提过,所以我来。
这是浏览器大战的副产品。
回到20世纪90年代,当互联网刚开始成为主流时,浏览器市场的竞争加剧了。为了保持竞争力和吸引用户,一些浏览器(最著名的Internet Explorer)试图通过试图弄清楚页面设计者的意思来提供帮助和“用户友好”,从而允许不正确的标记(例如,<b><i>foobar</b></i>将正确地呈现为粗体-斜体)。
这在某种程度上是有道理的,因为如果一个浏览器不断抱怨语法错误,而另一个浏览器接受你扔给它的任何东西,并吐出一个(或多或少)正确的结果,那么人们自然会涌向后者。
While many thought the browser wars were over, a new war between browser vendors has reignited in the past few years since Chrome was released, Apple started growing again and pushing Safari, and IE lost its dominance. (You could call it a “cold war” due to the perceived cooperation and support of standards by browser vendors.) Therefore, it is not a surprise that even contemporary browsers which supposedly conform strictly to web standards actually try to be “clever” and allow standard-breaking behavior such as this in order to try to gain an advantage as before.
不幸的是,这种放任行为导致了大量(有些人甚至认为是恶性的)标记不良的网页的增长。因为IE是最宽容、最受欢迎的浏览器,而且由于微软对标准的持续蔑视,IE因鼓励和推广糟糕的设计、传播和延续坏页面而臭名昭著。
现在你可能可以在一些浏览器上使用这样的怪癖和漏洞,但除了偶尔的谜题或游戏或其他东西,在创建网页和网站时,你应该始终坚持网页标准,以确保它们正确显示,并避免它们在浏览器更新时被破坏(可能完全被忽略)。