HTML/CSS 字体颜色与跨度样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4182554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:17:27  来源:igfitidea点击:

HTML/CSS font color vs span style

htmlcss

提问by Isis

What should I use?

我应该使用什么?

<span style="color:red">test</span>

or

或者

<font color="red">test</font>

and why?

为什么?

回答by Kyle

You should use <span>, because as specified by the spec, <font>has been deprecated and probably won't display as you intend.

您应该使用<span>,因为正如 spec 所指定的<font>已被弃用,并且可能不会按您的预期显示。

回答by Peter

Neither. You should separate content and presentation, giving your HTML code logical codes. Think of it this way; to a blind person, or on a browser that cannot display colors, what is left of your code? Why do you want it to be red?

两者都不。您应该将内容和展示分开,为您的 HTML 代码提供逻辑代码。这样想;对于盲人或无法显示颜色的浏览器,您的代码还剩下什么?你为什么想要它是红色的?

Most probably, your decision to make text red is because you want to give it emphasis. So your HTML code should be:

最有可能的是,您决定将文本设为红色是因为您想强调它。所以你的 HTML 代码应该是:

<em>test</em>

This way, even non-visual browsers can make sure they give the text emphasis in one way or another.

这样,即使是非可视化浏览器也可以确保它们以一种或另一种方式强调文本。

Next step is to make the text red. But you don't want to add the color code everywhere, much more efficient to just add it once:

下一步是使文本变红。但是你不想在任何地方添加颜色代码,只添加一次更有效:

<style>
  em { color: red; }
</style>

This way, allemphasized code on your website becomes red, making it more constant.

这样,您网站上所有强调的代码都会变成红色,使其更加稳定。

回答by Michael

1st preference external style sheet.

第一偏好外部样式表。

<span class="myClass">test</span>

css

css

.myClass
{
color:red;
}

2nd preference inline style

第二偏好内联样式

<span style="color:red">test</span>

<font>as mentioned is deprecated.

<font>如上所述已被弃用。

回答by fncomp

Use style. The font tag is deprecated (W3C Wiki).

使用风格。不推荐使用字体标签(W3C Wiki)。

回答by Cody Gray

The <font>tag has been deprecated, at least in XHTML. That means that it's use is officially "frowned upon," and there is no guarantee that future browsers will continue to display the text as you intended.

<font>标签已经过时,至少在XHTML。这意味着它的使用被正式“反对”,并且不能保证未来的浏览器会继续按您的预期显示文本。

You have to use CSS. Go with the <span>tag, or a separate style sheet. According to its specification, the <span>tag has no semantic meaning and just allows you to change the style of a particular region.

你必须使用 CSS。使用<span>标签或单独的样式表。根据其规范<span>标签没有语义意义,仅允许您更改特定区域的样式。

回答by Damien

Actually I would say the 1st preference would be an external style sheet (External CSS), the 2nd preference would be writing CSS in style tags in the header section of the current page (Internal CSS)

实际上我会说第一个偏好是外部样式表(外部 CSS),第二个偏好是在当前页面的标题部分的样式标签中编写 CSS(内部 CSS)

<style type="text/css">
<!-- CSS goes here -->
</style>

And as a 3rd option - or last resort rather - I'd use CSS in the tags themselves (Inline CSS).

作为第三种选择 - 或者说是最后的手段 - 我会在标签本身(内联 CSS)中使用 CSS。

回答by Amar Pratap

<span style="color:#ffffff; font-size:18px; line-height:35px; font-family: Calibri;">Our Activities </span>

This works for me well:) As it has been already mentioned above "The font tag has been deprecated, at least in XHTML. It always safe to use span tag. font may not give you desire results, at least in my case it didn't.

这对我很有效:) 正如上面已经提到的“字体标签已被弃用,至少在 XHTML 中。使用 span 标签总是安全的。字体可能不会给你想要的结果,至少在我的情况下它没有'不。