HTML: Changing colors of specific words in a string of text

Mitch picture Mitch · Jan 7, 2011 · Viewed 504.8k times · Source

I have the below message (slightly changed):

"Enter the competition by January 30, 2011 and you could win up to $$$$ — including amazing summer trips!"

I currently have:

<p style="font-size:14px; color:#538b01; font-weight:bold; font-style:italic;">

formatting the text string, but want to change the color of "January 30, 2011" to #FF0000 and "summer" to #0000A0.

How do I do this strictly with HTML or inline CSS?

Answer

Jacob picture Jacob · Jan 7, 2011
<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>

Or you may want to use CSS classes instead:

<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>