1

Is there a way using pure CSS to replace part of content text, for example if the text is ABCDE -

<span class="replacer">ABCDE</span>

But instead of ABCDE the first 3 chars will be * so it will displayed as ***DE.

1
  • 2
    Hi, doing this in pure CSS is not a good option try using JS instead Commented May 8, 2018 at 10:57

2 Answers 2

5

No, in pure CSS it's not possible to change the content, but you could cheat by overlapping something, e.g.

.replacer { 
  font-family: monospace; 
  position: relative; }

.replacer::before {
   background: #fff;
   position: absolute;
   content: "***";
}

Codepen demo

or, another tricky way could be creating and loading a special font that maps the symbols A, B and C into the asterisk sign (through the unicode-range property, https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/unicode-range)


in javascript you would only need to replace the textContent property of the element

var el = document.getElementsByClassName('replacer')[0]
el.textContent = el.textContent.replace(/^.{3}/, '***');
Sign up to request clarification or add additional context in comments.

Comments

5

A solution which works as you want, try to take a look at it

.replacer {
  display: inline-block;
  position: relative;
  font-family:monospace;
  font-size:18px;
}

.replacer:before {
  content: attr(stars);
  background-color: #fff;
  position: absolute;
  left: 0;
}
<span stars="***" class="replacer">ABCDE</span>

To increase the number of stars in beginning, just change the stars attribute in html, similarly same can be done for .replacer::after, see example below

.replacer {
  display: inline-block;
  position: relative;
  font-family:monospace;
  font-size:18px;
}

.replacer:after {
  content: attr(stars);
  background-color: #fff;
  position: absolute;
  right : 0;
}
<span stars="***" class="replacer">ABCDE</span>

5 Comments

pay attention, your are not hiding 3 letter
The OP asked for ***DE, that's what this CSS does
you need a monospaced font, I can still see the C in your example
@fcalderan I made my changes
by the way don't need to change to after, simply change right/left since the element is position:absolute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.