2

I need to change the content of a link, but I can only change the CSS of the page I'm working on. I tried using the content property in :before and :after pseudo elements. But I didn't find a way to replace the value with the new one. I was able only to append the content to the value specified between tags, at the beginning and at the end, instead of completely replacing it like I need.

a:before {
  content: 'ciao 1'
}

a:after {
  content: 'ciao 2'
}
<a href="https://www.w3schools.com/css/">CSS Tutorial</a>

What can I do to actually "replace" the content of the link tag?

4
  • 2
    JS, innerHTML ... Commented Feb 21, 2018 at 13:33
  • which value you want to remove and no any button in your code..? Commented Feb 21, 2018 at 13:33
  • What is your expected result? Commented Feb 21, 2018 at 13:35
  • 2
    Took me 15 seconds Commented Feb 21, 2018 at 13:43

2 Answers 2

8

Use a font-size trick like this:

a {
  font-size: 0;
}

a:before {
  content: 'ciao 1';
  font-size: initial;
}
<a href="https://www.w3schools.com/css/">CSS Tutorial</a>

Or visibility like this:

a {
  visibility: hidden;
}

a:before {
  content: 'ciao 1';
  visibility: visible;
}
<a href="https://www.w3schools.com/css/">CSS Tutorial</a>

Sign up to request clarification or add additional context in comments.

Comments

0

It is possible, although it is kind of hacky. But then again: changing content through CSS is kind of hacky anyway. You can set the font-size of the actual link (<a>) to 0px, and then "reset" the font-size for the pseudo elements.

a{
  font-size: 0px;
}
a:before,
a:after{
  font-size: 16px;
}
a:before {
  content: 'ciao 1'
}

a:after {
  content: 'ciao 2'
}
<a href="https://www.w3schools.com/css/">CSS Tutorial</a>

2 Comments

Rare to find accepted negatives.
@ManojKumar I think it is down-voted because even though this answer is a copy-pasted version of TemaniAfif's answer below, Temani's answer wasn't marked as the right one.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.