简体   繁体   English

将<code>tag in CSS class</code>

[英]Putting <code> tag in CSS class

I am new to CSS. 我是CSS新手。

Is there any way to put code tag in CSS class? 有什么办法可以将代码标签放入CSS类中?

For displaying code, at first I was using inline style: 为了显示代码,起初我使用内联样式:

<table>
  <tr>
    <td style="border:solid 2px #5882FA">
         <code> 
               some codes 
         </code>
    </td>
  </tr>
</table>

Then I found out about internal style sheet. 然后我发现了内部样式表。 So I used 所以我用

<header>
   <style>
      td.bordered{border:solid 2px #5882FA}
   </style>
</header>
 .
 .
 .
<table>
  <tr>
    <td class="bordered">
         <code> 
               some codes 
         </code>
    </td>
  </tr>
</table>

I was wondering whether somehow I can also put the code tag into style sheet, so I don't need to write it every time I want to display codes. 我想知道是否也可以将代码标签放入样式表中,因此我不需要每次显示代码时都编写代码。

No. You could apply styles to the table data cell that would duplicate a given browser's default styling for a code element, and you can use ::before and ::after to generate pseudo-elements, but there is no way to apply the semantic meaning associated with an HTML element using CSS. 不能。您可以将样式应用于表数据单元,该样式将复制给定浏览器的代码元素默认样式,并且可以使用::before::after生成伪元素,但是无法应用语义含义与使用CSS的HTML元素相关联。

The CSS spec goes so far as to explicitly warn against trying to do that sort of thing: CSS规范甚至明确警告不要尝试执行此类操作:

Note. 注意。 CSS gives so much power to the "class" attribute, that authors could conceivably design their own "document language" based on elements with almost no associated presentation (such as DIV and SPAN in HTML) and assigning style information through the "class" attribute. CSS为“ class”属性提供了如此强大的功能,以至于作者可以根据几乎没有关联表示的元素(例如HTML中的DIV和SPAN)设计自己的“文档语言”,并通过“ class”属性分配样式信息。 Authors should avoid this practice since the structural elements of a document language often have recognized and accepted meanings and author-defined classes may not. 作者应避免这种做法,因为文档语言的结构元素通常具有公认的含义,而作者定义的类则可能没有。

On the subject of semantics, a one-by-one table suggests you are abusing it for its shrink-wrapping layout features. 关于语义的问题,一张一张一张的表格建议您滥用它的收缩包装布局功能。 Use display: inline-block instead. 使用display: inline-block代替。

Thus your code should probably look like this: 因此,您的代码应该看起来像这样:

<style>
    code {
        display: inline-block;
        border: solid 2px #5882FA;
    }
</style>

<code> 
    some codes 
</code>

No. First, there are no “CSS classes”. 否。首先,没有“ CSS类”。 A class is a markup language (HTML) concept. 类是标记语言(HTML)的概念。 It may be referred to in CSS, using a class selector, but this is simply a way to refer to elements by their class. 可以使用类选择器在CSS中对其进行引用,但这只是按其类引用元素的一种方式。 Second, tags are markup language concept too, and you cannot create tags or elements in CSS; 其次,标签也是标记语言的概念,您不能在CSS中创建标签或元素。 in CSS, you just refer to elements by their names. 在CSS中,您只需按元素名称来引用它们。

However, if we interpret the question as “can I format some elements in CSS as if they had code elements inside them”, as I think it is, then the answer is “yes, with reservations”. 但是,如果我们将问题解释为“我可以格式化CSS中的某些元素,就像它们内部包含code元素一样”,那么答案是“是的,有保留”。

For the most of it, all that code markup does is that it sets the font face to a browser-dependent monospace font and the font size to a browser-dependent reduced size, though the latter (poorly documented) applies in some contexts and some browsers only. 在大多数情况下, code标记所做的只是将字体设置为与浏览器相关的等宽字体,并将字体大小设置为与浏览器相关的缩小字体,尽管后者(记录不足)适用于某些情况和某些情况。仅浏览器。 In addition to this, it may eg affect automatic translation programs (like Google Translate) so that they do not translate the content, regarding it as being “not in any human language”—this is normally good, but you cannot do that in CSS. 除此之外,它可能会影响自动翻译程序(例如Google Translate),以使它们不翻译内容,因为它“不是用任何人类语言” —通常很好,但是您不能在CSS中做到这一点。 And code markup could have other effects too. code标记也可能具有其他影响。

Thus, you might use eg 因此,您可以使用例如

td.bordered {
  /* put your settings for the border here */
  font-family: monospace;
  font-size: 90%;
}

But beware that this does not necessarily produce the same effect as the use of code elements. 但是请注意,这不一定会产生与使用code元素相同的效果。 On the other hand, this should not really be an issue: decide on the desired rendering, and implement it in CSS, instead of trying to imitate some assumed default rendering of code . 另一方面,这实际上不是问题:确定所需的呈现并在CSS中实现,而不是尝试模仿某些假定的默认code呈现。

If your goal is to display a border around the contents of the <code> tag wherever it appears, you could use the following CSS rule: 如果您的目标是在<code>标记的内容周围显示边框,则可以使用以下CSS规则:

code {
    border: solid 2px #5882FA;
}

The code selector, without any dots or # characters before it, is called an element selector . code选择器之前没有任何点或#字符,称为元素选择器

The other selector that you used, td.bordered combines an element selector that matches all <td> tags with a class selector that matches the bordered class. 您使用的另一个选择器td.bordered将匹配所有<td>标签的元素选择器与匹配带bordered类的类选择器组合在一起。

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

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