HTML:更改文本字符串中特定单词的颜色

Mit*_*tch 79 html css text inline colors

我有以下消息(略有变化):

"参加比赛到2011年1月30日,你可以赢得最多★ - 包括惊人的夏季旅行!"

我目前有:

<p style="font-size:14px; color:#538b01; font-weight:bold; font-style:italic;">
Run Code Online (Sandbox Code Playgroud)

格式化文本字符串,但想要将"2011年1月30日"的颜色更改为#FF0000,将"summer"更改为#0000A0.

如何使用HTML或内联CSS严格执行此操作?

Jac*_*cob 100

<p style="font-size:14px; color:#538b01; font-weight:bold; font-style:italic;">
  Enter the competition by 
  <span style="color: #ff0000">January 30, 2011</span>
  and you could win up to $$$$ — including amazing 
  <span style="color: #0000a0">summer</span> 
  trips!
</p>
Run Code Online (Sandbox Code Playgroud)

或者您可能想要使用CSS类:

<html>
  <head>
    <style type="text/css">
      p { 
        font-size:14px; 
        color:#538b01; 
        font-weight:bold; 
        font-style:italic;
      }
      .date {
        color: #ff0000;
      }
      .season { /* OK, a bit contrived... */
        color: #0000a0;
      }
    </style>
  </head>
  <body>
    <p>
      Enter the competition by 
      <span class="date">January 30, 2011</span>
      and you could win up to $$$$ — including amazing 
      <span class="season">summer</span> 
      trips!
    </p>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)


小智 36

您可以使用HTML5标记<mark>:

<p>Enter the competition by 
<mark class="red">January 30, 2011</mark> and you could win up to $$$$ — including amazing 
<mark class="blue">summer</mark> trips!</p>
Run Code Online (Sandbox Code Playgroud)

并在CSS中使用它:

p {
    font-size:14px;
    color:#538b01;
    font-weight:bold;
    font-style:italic;
}

mark.red {
    color:#ff0000;
    background: none;
}

mark.blue {
    color:#0000A0;
    background: none;
}
Run Code Online (Sandbox Code Playgroud)

标签<mark>具有默认背景颜色...至少在Chrome中.

  • mark 标签并不用于格式化。 (7认同)
  • 遗憾的是答案没有被授予.我会把它授予此(并且对那些使用不支持HTML的浏览器的人说(还有吗?)) (3认同)

Dam*_*ght 32

<p style="font-size:14px; color:#538b01; font-weight:bold; font-style:italic;">
    Enter the competition by <span style="color:#FF0000">January 30, 2011</span> and you could win up to $$$$ — including amazing <span style="color:#0000A0">summer</span> trips!
</p>
Run Code Online (Sandbox Code Playgroud)

span元素是内联的,因此不会破坏段落的流程,只能在标记之间进行样式处理.


bri*_*n_d 19

使用跨度.EX)<span style='color: #FF0000;'>January 30, 2011</span>


小智 13

<font color="red">This is some text!</font> 
Run Code Online (Sandbox Code Playgroud)

当我只想在一个句子中将一个单词改为红色时,这对我来说是最好的.

  • 这在 HTML 5 中不受支持。 (7认同)

小智 6

您还可以开设课程:

<span class="mychangecolor"> I am in yellow color!!!!!!</span>

然后在 css 文件中执行以下操作:

.mychangecolor{ color:#ff5 /* it changes to yellow */ }
Run Code Online (Sandbox Code Playgroud)


小智 6

根据您的需要定制此代码,您可以选择文本吗?在段落中填写您需要的字体或样式!:

<head>
<style>
p{ color:#ff0000;font-family: "Times New Roman", Times, serif;} 
font{color:#000fff;background:#000000;font-size:225%;}
b{color:green;}
</style>
</head>
<body>
<p>This is your <b>text. <font>Type</font></strong></b>what you like</p>
</body>
Run Code Online (Sandbox Code Playgroud)