在我的课上,我玩了一下,发现CSS与虚构的元素一起工作。

例子:

imsocool { 颜色:蓝色; } < imsocool >你好< / imsocool >

当我的教授第一次看到我使用它时,他有点惊讶于虚构元素的工作,并建议我简单地将所有的虚构元素更改为带有ID的段落。

为什么我的教授不让我使用合成元素?他们工作效率很高。

另外,为什么他不知道存在虚构元素,并与CSS一起工作。它们不常见吗?


当前回答

其他人提出了很好的观点,但值得注意的是,如果你看看像AngularJS这样的框架,自定义元素和属性是非常有效的。它们不仅向xml传递更好的语义含义,而且还可以为web页面提供行为、外观和感觉。

其他回答

他为什么不想让你用?它们既不常见,也不是HTML5标准的一部分。 从技术上讲,这是不允许的。他们就是个笨蛋。

不过我自己也喜欢。您可能对XHTML5感兴趣。它允许您定义自己的标记,并将它们作为标准的一部分使用。

此外,正如其他人指出的那样,它们是无效的,因此不能携带。

为什么他不知道他们的存在?我不知道,只知道它们不常见。可能他只是不知道你可以。

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头,以便工具能够为您提供有意义的错误消息。

令人惊讶的是,没有人(包括我过去的自己)提到可访问性。使用有效标记而不是自定义标记的另一个原因是为了与最大数量的软件兼容,包括屏幕阅读器和人们出于可访问性目的所需的其他工具。此外,像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因鼓励和推广糟糕的设计、传播和延续坏页面而臭名昭著。

现在你可能可以在一些浏览器上使用这样的怪癖和漏洞,但除了偶尔的谜题或游戏或其他东西,在创建网页和网站时,你应该始终坚持网页标准,以确保它们正确显示,并避免它们在浏览器更新时被破坏(可能完全被忽略)。

现代浏览器将未知元素视为div。这就是它们有效的原因。这是即将到来的HTML5标准的一部分,该标准引入了模块化结构,可以向其中添加新元素。

在较老的浏览器(我认为是IE7-)中,你可以应用javascript技巧,之后它们也能正常工作。

下面是我在寻找例子时发现的一个相关问题。

这里有一个关于Javascript修复的问题。事实证明IE7确实不支持这些开箱即用的元素。

也;为什么他不知道自制标签的存在和CSS的工作。它们不常见吗?

是的,相当。但特别的是:它们没有额外的用途。他们是html5的新手。在HTML的早期版本中,未知标记是无效的。

Also, teachers seem to have gaps in their knowledge, sometimes. This might be due to the fact that they need to teach students the basics about a given subject, and it doesn't really pay off to know all ins and outs and be really up to date. I once got detention because a teacher thought I programmed a virus, just because I could make a computer play music using the play command in GWBasic. (True story, and yes, long ago). But whatever the reason, I think the advice not to use custome elements is a sound one.

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 ->渲染。实际上,它要复杂得多,但这对于初学者来说已经足够好了。

这个答案已经太长了,所以我就到此为止。