Open In App

How to replace text with CSS?

Last Updated : 15 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Replacing text on a webpage is typically done on the server side, but there are situations where you might need to do this directly in CSS. This can be useful when you don’t have server access or are working within certain restrictions. CSS offers a couple of methods to replace text, which we’ll explore here.

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

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 aabove approach.

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

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