Home > Web Front-end > CSS Tutorial > How can I change the color of specific words within an HTML text string?

How can I change the color of specific words within an HTML text string?

Patricia Arquette
Release: 2024-11-16 00:48:02
Original
210 people have browsed it

How can I change the color of specific words within an HTML text string?

Changing Colors of Specific Words in HTML Text

To alter the color of specific words within a string of text in HTML, you can utilize inline CSS or CSS classes.

Inline CSS:

<p>  Enter the competition by <br>  <span>  and you could win up to $$$$ — including amazing <br>  <span>  trips!<br></p><br>

This approach adds a span element around the words you want to color differently and applies the color style to that span.

CSS Classes:

Alternatively, you can define CSS classes:

<html><br>  <head></p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><style type="text/css">
  p { 
    font-size:14px; 
    color:#538b01; 
    font-weight:bold; 
    font-style:italic;
  }
  .date {
    color: #ff0000;
  }
  .season { /* Contrived example... */
    color: #0000a0;
  }
</style>
Copy after login


<p>
  Enter the competition by 
  <span>
Copy after login



This method involves creating CSS rules for the desired colors, assigning those rules to specific classes, and then applying those classes to the text you want to style.

The above is the detailed content of How can I change the color of specific words within an HTML text string?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template