2

I have a unordered list with in a div

<div class="myClass">
  <ul>
      <li>Login to your account</li>
      <li>Visit the Accounts ? Invoices page</li>
      <li>Click the <span class="bold">Pay Now</span> button</li>
   </ul>
 </div>

How can i replace "?" in the second li with "-->" using pure css

7
  • 3
    It's not possible to alter text nodes via CSS. you probably need to use JavaScript to achieve that. Commented Oct 7, 2014 at 12:19
  • Using pure css You cant. Commented Oct 7, 2014 at 12:19
  • Css is not suited for that Commented Oct 7, 2014 at 12:21
  • btw, CSS is a 'Style' sheet.. it's not used for content, except arguebly special cases Commented Oct 7, 2014 at 12:21
  • stackoverflow.com/questions/7896402/… check this SO link, it may helps you. Commented Oct 7, 2014 at 12:24

5 Answers 5

2

There is now way to do that with css only. You have to use js. BUT if this character was enclosed by <div class="element">?</div> for example ,you could do this one:http://jsfiddle.net/csdtesting/uc8h6pz2/

.element {
  text-indent: -9999px;
  line-height: 0;
  /* Collapse the original line */
}
.element::after {
  content: "New text";
  text-indent: 0;
  display: block;
  line-height: initial;
  /* New content takes up original line height */
}
<div class="myClass">
  <ul>
    <li>Login to your account</li>
    <li>Visit the Accounts
      <div class="element">?</div>Invoices page</li>
    <li>Click the <span class="bold">Pay Now</span> button</li>
  </ul>
</div>

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

Comments

1

It's very hacky and in no way the right way to go, but it works (however only for the exact case described above).

.myClass ul li:nth-child(2) {
  content: '->';
  position: absolute;
  left: 10.5em;
  background: #fff;
}

As mentioned by others, for a job like this, CSS is not right tool. Add some javascript to do this job, it's way better as it doesn't care about the user's text zoom for example (which would break the CSS "solution" probably).

var el = document.getElementsByClassName('myClass')[0].children[0].children[1];
var text = el.innerHTML;
el.innerHTML = text.replace('?','-->');

See DEMO for the vanilla js solution.

Though, even better would be to find the source of the question mark ?, it looks like there's some encoding problem (utf-8?).

Comments

0

Using pure css you can do it, but only if the question mark is in the exact same position every time:

<div class="myClass">
  <ul>
      <li>Login to your account</li>
      <li class="replace">Visit the Accounts ? Invoices page</li>
      <li>Click the <span class="bold">Pay Now</span> button</li>
   </ul>
 </div>

.replace{
    position: relative;
}
.replace:after{
    position: absolute;
    content: '-->';
    background: #fff;
    left: 120px;
    top: 0;
    font-size: 12px;
}

fiddle: http://jsfiddle.net/kw4838dt/

6 Comments

I'm commenting on your answer, but it goes for all of them.. there's really no help in giving a hack solution to a question that the answer is simply no.. Yes maybe it works for this specific sentence with the exact position and font size.. but it's definitely bad practice and not the right approach
@webkit - OP did not define in his question what exactly he needs, If it's a static page that's never going to change, why not use this approach?
Because, first I think posting answers on SO should be such that other people can learn from.. No one could take and use this solution for a similar issue.. and if they tweaked it and DID.. then it's even worse because using CSS for this type of stuff is bad code and shouldn't be taught..
Secondly, the OP did use the word 'replace' in his title and in his question.. which none of these answers really do.. am i wrong?
@webkit - first of all, i specifically mentioned that it's only suitable for specific use case, secondly, people can definitely learn from these answers, the technique of using pseudo elements is not known to all people, and even if they don't use it for this case, they can sure use it in other cases for other purposes and third, the fact that the OP used the word replace does not prove anything, maybe he didn't know he can hide it?
|
0

I found this article https://www.geeksforgeeks.org/how-to-replace-text-with-css/

<html>
  
<head>
    <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>

Comments

-1

You can't do that with CSS but you can try it with HTML. Look at the following:

&ndash;&ndash;&gt;

This will make the "-->"

Hope it was useful

Comments

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.