Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 888
Mitch Avatar asked Jan 07 '11 05:01

Mitch


People also ask

How do you color certain words in HTML?

To set the font color in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property color. HTML5 do not support the <font> tag, so the CSS style is used to add font color.

How do you color individual letters in HTML?

Make separate elements Another way is creating a text with separate elements by using the HTML <span> tag, which is an empty container. This means that you create a multicolor text by putting each letter in a separate element to define a different color for each letter of your text.


2 Answers

<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> 
like image 62
Jacob Avatar answered Nov 15 '22 23:11

Jacob


You could use the HTML5 Tag <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> 

And use this in the 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; } 

The tag <mark> has a default background color... at least in Chrome.

like image 32
Juan Pablo Pinedo Avatar answered Nov 15 '22 22:11

Juan Pablo Pinedo