简体   繁体   English

将css类添加到有效的html / css标记中

[英]Is adding a css class to a <b> tag valid html/css

Is adding a css class to a <b> tag valid html/css 将css类添加到<b>标签有效的html / css中

Example, can I do this: 例如,我可以这样做:

<b class="myclass"> Foo Bar </b>

Is this valid html/css? 这是有效的html / css吗?
I need to add a class to a b tag as an identifier so I can use it in jQuery/js. 我需要将一个类添加到b标记作为标识符,以便我可以在jQuery / js中使用它。 It wont have any css styles 它没有任何CSS样式

Yes , b tag can have all global attributes , including class . 是的b标签可以包含所有全局属性 ,包括class The full list of attributes, you can add to b element: 完整的属性列表,可以添加到b元素:

  • accesskey ACCESSKEY
  • class
  • contenteditable CONTENTEDITABLE
  • contextmenu 上下文菜单
  • dir DIR
  • draggable 拖动
  • dropzone 拖放区
  • hidden
  • id ID
  • inert 惰性的
  • itemid 的itemid
  • itemprop itemprop
  • itemref 的itemref
  • itemscope 的itemscope
  • itemtype 物品种类
  • lang
  • spellcheck 拼写检查
  • style 样式
  • tabindex tabindex属性
  • title 标题
  • translate 翻译

You can also use any custom data attributes . 您还可以使用任何自定义数据属性

Finally, you can add also ARIA role attribute. 最后,您还可以添加ARIA role属性。

Of course. 当然。 There's nothing wrong with that. 这没什么不对。

However, it's generally a bad idea to use class purely for identifying an element. 但是, 纯粹使用class来识别元素通常是个坏主意。 Consider using something like data-reference or something, as this will be more correct (and more efficient on the browser not having to keep track of a class that's not used as a class) 考虑使用像data-reference东西,因为这会更正确(并且在浏览器上更高效,而不必跟踪未被用作类的类)

Yes, this is perfectly valid. 是的,这是完全有效的。 Absolutely nothing wrong with it. 绝对没有错。

There's nothing strictly wrong with it, except that the <b> bold tag is deprecated, in favor of using the <strong> tag. 除了不推荐使用<strong>标记<b>粗体标记外,它没有严格的错误。

I never give the strong (or b) tag a class because I only use it when I want the text strong to call attention to it, similar to <em> emphasizing words in text. 我从不给强类(或b)标签一个类,因为我只使用它,当我希望文本强烈引起注意时,类似于<em> 强调文本中的单词。

If I am bolding or emphasizing the text for some other reason I use a div or span with a class — for example, it is common to italicize the title of a book or article, and in that case I do not use <em> around the title, I use <span class="title">This Is That Title</span> to semantically mark what this thing is, then use a stylesheet to say "titles are italic". 如果我因为某些其他原因而加粗或强调文本,我会使用div或span与类 - 例如,将书或文章的标题用斜体标记是常见的,在这种情况下我不使用<em>标题,我用<span class="title">This Is That Title</span>来语义标记这个东西是什么 ,然后使用样式表来说“标题是斜体”。

There are no “CSS classes”. 没有“CSS类”。 CSS has class selectors, but that's a different issue and postulates the existence of a class attribute in a markup language. CSS有类选择器,但这是一个不同的问题,并假定在标记语言中存在class属性。 Thus, the question is meaningless as far as CSS is considered. 因此,就CSS而言,这个问题毫无意义。

In HTML, the class attribute is valid (formally correct) on b elements, in any HTML version from HTML 4.0 (which introduced the attribute) onwards. 在HTML中, class属性在b元素上是有效的(正式正确),在HTML 4.0(引入属性)的任何HTML版本中都是有效的。 Whether it makes sense or not is a different issue, but there are no formal restrictions on its use. 它是否有意义是一个不同的问题,但它的使用没有正式的限制。 Although class is most often used for styling, it can be used for other purposes, too, especially in scripting. 尽管class通常用于样式,但它也可以用于其他目的,尤其是在脚本编写中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM