Open In App

How to replace text with CSS?

Last Updated : 07 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Replacing text is usually done on the server side, but in restricted environments, CSS can offer simple workarounds. Though limited, CSS techniques can handle about 10–15% of basic text substitution needs. We'll explore these methods and where they work best.

Using :after Pseudo-element

The :after pseudo-element allows you to insert content after the selected element. You can use this technique to replace text by hiding the original text and placing new text in its place.

Steps:

  • Wrap the Text: Assign a class to the text you want to replace.
<p class="toBeReplaced">Old Text</p>
  • The text "Old Text" needs to be hidden first and a new text has to be positioned exactly where the old text was. To do so, we change the visibility of this text using CSS to hidden first.
.toBeReplaced {
visibility: hidden;
position: relative;
}
  • Then we add a new text at the exact same position, using the pseudo elements and corresponding explicit positioning.
.toBeReplaced:after {
visibility: visible;
position: absolute;
top: 0;
left: 0;
content: "This text replaces the original.";
}

Note: after is the pseudo element in use here. We use the visibility modifier once again to show the new text. The content attribute contains the new text.

Example: Implementation to replace text using :after Pseudo-element

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        Using :after Pseudo-element
    </title>
    <style>
        .toBeReplaced {
            visibility: hidden;
            position: relative;
        }
        .toBeReplaced:after {
            visibility: visible;
            position: absolute;
            top: 0;
            left: 0;
            content: "This text replaces the original.";
        }
    </style>
</head>
<body>
    <p class="toBeReplaced">Old Text</p>
</body>
</html>

Output:

Screenshot-2024-01-17-154234

Using Pseudo-elements & CSS Visibility

Another approach involves hiding the original text using a <span> element and displaying new text with the :after pseudo-element.

Steps:

  • Wrap the original text within a <span> element, serving as a child of the <p> element with the class .toBeReplaced.
  • Apply display: none; to the <span> element, ensuring the original text is hidden from view.
  • Utilize the :after pseudo-element on the .toBeReplaced class to insert content that replaces the hidden text.
.toBeReplaced span {
display: none;
}
.toBeReplaced:after {
content: "This text replaces the original.";
}

Example: Implementation to replace text using above approach.

index.html
<!DOCTYPE html>
<html>
<head>
    <title>
        Using Pseudo-elements & CSS Visibility
    </title>
    <style>
        .toBeReplaced span {
            display: none;
        }

        .toBeReplaced:after {
            content: "This text replaces the original.";
        }
    </style>
</head>
<body>
    <p class="toBeReplaced"><span>Old Text</span></p>
</body>
</html>

Output:

Screenshot-2024-01-17-154234

Note: By using these CSS techniques, you can effectively replace text on a webpage without needing to modify the server-side code. Whether you choose to utilize the :after pseudo-element or combine pseudo-elements with CSS visibility, these methods offer flexibility and control over how text is displayed.


Next Article
Article Tags :

Similar Reads

  翻译: