How to Change the Color of the First Word in a Text

Solutions with CSS and HTML

If you want to change the color of the first word of a text, you can use the CSS :before pseudo-element, which is used to add any element. Its value is defined with the content property. If it is not used, the content will not be generated and inserted.

In the example below, we use a <div> element with a class "word" and specify its color. Then, we add the :before pseudo-element to the "word" class and add the word the color of which we want to change with the color property. After that, we specify its color.

Example of changing the color of the first word of a text:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .word {
        color: #000;
      }
      .word:before {
        color: #f00000;
        content: "Lorem";
      }
    </style>
  </head>
  <body>
    <div class="word">
      Ipsum is simply dummy text of the printing and typesetting industry. 
      Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
      when an unknown printer took a galley of type and scrambled it to make a
      type specimen book. It has survived not only five centuries, but also 
      the leap into electronic typesetting, remaining essentially unchanged.
      It was popularised in the 1960s with the release of Letraset sheets 
      containing Lorem Ipsum passages, and more recently with desktop 
      publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
  </body>
</html>

Result

Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Of course, visually, we achieved our goal of changing the color of the first word, but this is not good for accessibility. Some screen readers may skip over CSS-generated content. Besides, this breaks the concept of separating content from formatting.

Example of changing the color of the first word:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      div[data-highlightword] {
        position: relative;
        color: #666666;
      }
      div[data-highlightword]::before {
        content: attr(data-highlightword);
        color: purple;
        position: absolute;
        top: 0;
        left: 0;
      }
    </style>
  </head>
  <body>
    <div data-highlightword="Example">
      Example for you.
    </div>
  </body>
</html>

Example of changing the color of the first word with the HTML <span> tag:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      span {
        color: green;
      }
    </style>
  </head>
  <body>
    <div>
      <span>Example</span> for you.
    </div>
  </body>
</html>