在我的课上,我玩了一下,发现CSS与虚构的元素一起工作。
例子:
imsocool { 颜色:蓝色; } < imsocool >你好< / imsocool >
当我的教授第一次看到我使用它时,他有点惊讶于虚构元素的工作,并建议我简单地将所有的虚构元素更改为带有ID的段落。
为什么我的教授不让我使用合成元素?他们工作效率很高。
另外,为什么他不知道存在虚构元素,并与CSS一起工作。它们不常见吗?
在我的课上,我玩了一下,发现CSS与虚构的元素一起工作。
例子:
imsocool { 颜色:蓝色; } < imsocool >你好< / imsocool >
当我的教授第一次看到我使用它时,他有点惊讶于虚构元素的工作,并建议我简单地将所有的虚构元素更改为带有ID的段落。
为什么我的教授不让我使用合成元素?他们工作效率很高。
另外,为什么他不知道存在虚构元素,并与CSS一起工作。它们不常见吗?
当前回答
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 ->渲染。实际上,它要复杂得多,但这对于初学者来说已经足够好了。
这个答案已经太长了,所以我就到此为止。
其他回答
实际上你可以使用自定义元素。下面是关于这个主题的W3C规范:
http://w3c.github.io/webcomponents/spec/custom/
这里有一个教程来解释如何使用它们:
http://www.html5rocks.com/en/tutorials/webcomponents/customelements/
正如@Quentin所指出的:这是开发早期的规范草案,它对元素名称施加了限制。
虽然CSS有一个叫做“标签选择器”的东西,但它实际上并不知道标签是什么。这将留给文档的语言来定义。CSS被设计成不仅可以与HTML一起使用,还可以与XML一起使用,其中(假设您没有使用DTD或其他验证方案)标签可以是任何东西。您也可以将它用于其他语言,尽管您需要提出自己的语义,以明确“标签”和“属性”对应的内容。
浏览器通常将CSS应用于HTML中的未知标记,因为这被认为比完全破坏要好:至少它们可以显示一些东西。但故意使用“假”标签是非常糟糕的做法。这样做的一个原因是,新标签确实会时不时地定义,如果一个定义看起来有点像你的假标签,但不完全以相同的方式工作,这可能会导致你的网站在新浏览器上出现问题。
令人惊讶的是,没有人(包括我过去的自己)提到可访问性。使用有效标记而不是自定义标记的另一个原因是为了与最大数量的软件兼容,包括屏幕阅读器和人们出于可访问性目的所需的其他工具。此外,像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因鼓励和推广糟糕的设计、传播和延续坏页面而臭名昭著。
现在你可能可以在一些浏览器上使用这样的怪癖和漏洞,但除了偶尔的谜题或游戏或其他东西,在创建网页和网站时,你应该始终坚持网页标准,以确保它们正确显示,并避免它们在浏览器更新时被破坏(可能完全被忽略)。
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 ->渲染。实际上,它要复杂得多,但这对于初学者来说已经足够好了。
这个答案已经太长了,所以我就到此为止。
CSS是一种样式表语言,可用于表示XML文档,而不仅仅是(X)HTML文档。带有自制标记的代码片段可以是合法XML文档的一部分;如果你把它包含在一个根元素中,它就是1。可能你已经有一个<html>…</html> around it?当前的任何浏览器都可以显示XML文档。
当然,它不是一个很好的XML文档,它缺少语法和XML声明。如果使用HTML声明头(可能是发送正确mime类型的服务器配置),那么它将是非法的HTML。
HTML比普通XML有优势,因为元素有语义意义,在网页表示的上下文中是有用的。工具可以使用这种语义,其他开发人员知道它的含义,它更不容易出错,更易于阅读。
但在其他上下文中,最好使用CSS和XML和/或XSLT来表示。这就是你所做的。因为这不是你的任务,你不知道你在做什么,HTML/CSS是更好的方式,大多数时候你应该在你的场景中坚持使用它。
您应该在文档中添加一个(X)HTML头,以便工具能够为您提供有意义的错误消息。