博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
为什么CSS使用假元素?
阅读量:2289 次
发布时间:2019-05-09

本文共 6061 字,大约阅读时间需要 20 分钟。

本文翻译自:

In my class, I was playing around and found out that CSS works with made-up elements. 在我的课堂上,我一直在玩,发现CSS可以使用虚构的元素。

Example: 例:

imsocool { color:blue; }
HELLO

When my professor first saw me using this, he was a bit surprised that made-up elements worked and recommended I simply change all of my made up elements to paragraphs with ID's. 当我的教授第一次看到我使用它时,他对组成元素的工作有点惊讶,并建议我只需将所有组成元素更改为带ID的段落。

Why doesn't my professor want me to use made-up elements? 为什么我的教授不想让我使用化妆元素? They work effectively. 他们有效地工作。

Also, why didn't he know that made-up elements exist and work with CSS. 此外,为什么他不知道组成元素存在并使用CSS。 Are they uncommon? 它们不常见吗?


#1楼

参考:


#2楼

I think made-up tags are just potentially more confusing or unclear than p's with IDs (some block of text generally). 我认为制作标签可能比带有ID的p(通常是一些文本块)更令人困惑或不清楚。 We all know ap with an ID is a paragraph, but who knows what made-up tags are intended for? 我们都知道带有ID的ap是一个段落,但是谁知道哪些用于制作标签? At least that's my thought. 至少那是我的想法。 :) Therefore this is more of a style / clarity issue than one of functionality. :)因此,这更像是一种风格/清晰度问题,而不是功能之一。


#3楼

Why doesn't he want you to use them? 他为什么不要你用它们? They are not common nor part of the HTML5 standard. 它们不是常见的,也不是HTML5标准的一部分。 Technically, they are not allowed. 从技术上讲,他们是不允许的。 They are a hack. 他们是一个黑客。

I like them myself, though. 不过我自己也喜欢他们。 You may be interested in XHTML5. 您可能对XHTML5感兴趣。 It allows you to define your own tags and use them as part of the standard. 它允许您定义自己的标签并将其用作标准的一部分。

Also, as others have pointed out, they are invalid and thus not portable. 此外,正如其他人所指出的,它们是无效的,因此不便携。

Why didn't he know that they exist? 他为什么不知道他们存在? I don't know, except that they are not common. 我不知道,除了它们不常见。 Possibly he was just not aware that you could. 可能他只是不知道你可以。


#4楼

TL;DR TL; DR

  • Custom tags are invalid in HTML. 自定义标记在HTML中无效。 This may lead to rendering issues. 这可能会导致渲染问题。
  • Makes future development more difficult since code is not portable. 由于代码不可移植,因此使未来的开发更加困难。
  • Valid HTML offers a lot of benefits such as SEO, speed, and professionalism. 有效的HTML提供了许多好处,如SEO,速度和专业性。

Long Answer 答案很长

There are that code with custom tags is more usable. 有认为使用自定义标记的代码更有用。

However, it leads to invalid HTML. 但是,它会导致HTML无效。 Which is not good for your site. 这对您的网站不利。

  • It makes your web page more likely to work in browsers you haven't tested. 它使您的网页更有可能在未经测试的浏览器中工作。
  • It makes you look more professional (to some developers at least) 它让你看起来更专业(至少对一些开发者来说)
  • Compliant browsers can render [valid HTML faster] 兼容的浏览器可以更快地呈现[有效的HTML]
  • It points out a bunch of obscure bugs you've probably missed that affect things you probably haven't tested eg the codepage or language set of the page. 它指出了一些你可能错过的模糊错误,这些错误会影响你可能没有测试过的东西,例如页面的代码页或语言集。

  • Validation as a debugging tool 验证作为调试工具
  • Validation as a future-proof quality check 验证是一种面向未来的质量检查
  • Validation eases maintenance 验证可以简化维护
  • Validation helps teach good practices 验证有助于教授良好实践
  • Validation is a sign of professionalism 验证是专业化的标志

#5楼

Unknown elements are treated as div s by modern browsers. 现代浏览器将未知元素视为div That's why they work. 这就是他们工作的原因。 This is part of the oncoming HTML5 standard that introduces a modular structure to which new elements can be added. 这是即将推出的HTML5标准的一部分,该标准引入了可以添加新元素的模块化结构。

In older browsers (I think IE7-) you can apply a Javascript-trick after which they will work as well. 在旧的浏览器中(我认为IE7-)你可以应用一个Javascript技巧,之后它们也会起作用。

Here is a I found when looking for an example. 这是我在寻找示例时发现的 。

Here is a . 这是一个 。 Turns out it is indeed IE7 that doesn't support these elements out of the box. 事实证明,IE7确实不支持开箱即用的这些元素。

Also; 也; why didn't he know that made-up tags existed and worked with CSS. 为什么他不知道伪造的标签存在并使用CSS。 Are they uncommon? 它们不常见吗?

Yes, quite. 是的,相当。 But especially: they don't serve additional purpose. 但尤其是:它们没有其他用途。 And they are new to html5. 而且他们是html5的新手。 In earlier versions of HTML an unknown tag was invalid. 在早期版本的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. 我曾经被拘留过,因为老师以为我编程了病毒,只因为我可以让电脑在GWBasic中使用play命令播放音乐。 (True story, and yes, long ago). (真实的故事,是的,很久以前)。 But whatever the reason, I think the advice not to use custome elements is a sound one. 但无论什么原因,我认为不使用客户元素的建议是合理的。


#6楼

Why does CSS work with fake elements? 为什么CSS使用假元素?

(Most) browsers are designed to be (to some degree) forward compatible with future additions to HTML. (大多数)浏览器被设计为(在某种程度上)向前兼容HTML的未来添加。 Unrecognised elements are parsed into the DOM, but have no semantics or specialised default rendering associated with them. 无法识别的元素被解析到DOM中,但没有与它们相关联的语义或专门的默认呈现。

When a new element is added to the specification, sometimes CSS, JavaScript and ARIA can be used to provide the same functionality in older browsers (and the elements have to appear in the DOM for those languages to be able to manipulate them to add that functionality). 当一个新元素被添加到规范中时,有时可以使用CSS,JavaScript和ARIA在旧版浏览器中提供相同的功能(并且元素必须出现在DOM中,以便这些语言能够操作它们以添加该功能)。

(Although it should be noted that work is underway to define a means to , but this work is in the early stages of development at present so it should probably be avoided until it has matured.) (虽然应该注意的是,正在开展工作来定义一种的方法,但是这项工作目前处于开发的早期阶段,因此在它成熟之前应该避免它。)

Why doesn't my professor want me to use made-up elements? 为什么我的教授不想让我使用化妆元素?

  • They are not allowed by the HTML specification HTML规范不允许使用它们
  • They might conflict with future standard elements with the same name 它们可能与具有相同名称的未来标准元素冲突
  • There is probably an existing HTML element that is better suited to the task 可能存在更适合该任务的现有HTML元素

Also; 也; why didn't he know that made-up elements existed and worked with CSS. 为什么他不知道组成元素存在并使用CSS。 Are they uncommon? 它们不常见吗?

Yes. 是。 People don't use them because they have the above problems. 人们不使用它们因为它们有上述问题。

转载地址:http://ivcnb.baihongyu.com/

你可能感兴趣的文章
poj2516——Minimum Cost(最小费用最大流)
查看>>
hdu4280——Island Transport(最大流SAP算法)
查看>>
hdu4292——Food(SAP+拆点)
查看>>
hdu4289——Control(最大流最小割+SAP)
查看>>
hdu3605——Escape(二分图多重匹配)
查看>>
hdu1029——Ignatius and the Princess IV
查看>>
hdu1003——Max Sum(在线算法+求起点终点)
查看>>
hdu1002——A + B Problem II(大数加)
查看>>
hdu1087——Super Jumping! Jumping! Jumping!(最大递增子序列和)
查看>>
hdu1176——免费馅饼(简单dp)
查看>>
hdu1257——最少拦截系统(贪心)
查看>>
hdu5982——Relic Discovery(水)
查看>>
hdu5984——Pocky(数学期望)
查看>>
poj3186——Treats for the Cows(区间dp)
查看>>
poj3616——Milking Time(dp最大子序列)
查看>>
poj2318——TOYS(计算几何+点与线段的位置)
查看>>
poj2398——Toy Storage(计算几何)
查看>>
poj3304——Segments(判断直线与多个线段相交)
查看>>
poj1269——Intersecting Lines(判断线段交点)
查看>>
poj2653——Pick-up sticks(判断线段是否相交)
查看>>