How can I replace text with CSS?

前端 未结 21 2133
暗喜
暗喜 2020-11-22 06:17

How can I replace text with CSS using a method like this:

.pvw-title img[src*=\"IKON.img\"] { visibility:hidden; }

Instead of ( img

相关标签:
21条回答
  • 2020-11-22 06:55

    Text replacement with pseudo-elements and CSS visibility

    HTML

    <p class="replaced">Original Text</p>
    

    CSS

    .replaced {
        visibility: hidden;
        position: relative;
    }
    
    .replaced:after {
        visibility: visible;
        position: absolute;
        top: 0;
        left: 0;
        content: "This text replaces the original.";
    }
    
    0 讨论(0)
  • 2020-11-22 06:56

    I had better luck setting the font-size: 0 of the outer element, and the font-size of the :after selector to whatever I needed.

    0 讨论(0)
  • 2020-11-22 06:58

    After eight years, I faced the same challenge when trying to use the Stylish browser extension to change something on a website (not mine). And this time I made it work by looking at the source code using "inspect element" and created the CSS code based on that.

    This it what it looked like before:

    <table>
      <tbody>
        <tr>
          <td role="gridcell">
            <span title="In progress" style="background-color: #e2047a;color:White;margin:2px;border-radius:2px;padding-left: 2px; padding-right: 2px;text-align: center;width: 45px; display: block;overflow: hidden;text-overflow: ellipsis;">In progress</span>
          </td>
        </tr>
      </tbody>
    </table>

    This is the same piece of the HTML and the CSS I used to modify the style:

    td span[style="background-color: #e2047a;color:White;margin:2px;border-radius:2px;padding-left: 2px; padding-right: 2px;text-align: center;width: 45px; display: block;overflow: hidden;text-overflow: ellipsis;"] {
      width: 100px!important;
    }
    <table>
      <tbody>
        <tr>
          <td role="gridcell">
            <span title="In progress" style="background-color: #e2047a;color:White;margin:2px;border-radius:2px;padding-left: 2px; padding-right: 2px;text-align: center;width: 45px; display: block;overflow: hidden;text-overflow: ellipsis;">In progress</span>
          </td>
        </tr>
      </tbody>
    </table>

    You can run the code above and you will see that it works (tested in Chrome).


    This is simply what I wanted back in the days when I asked this question.

    I was using some sort of community blog/Myspace similar stuff and the only thing you had when styling your profile was their CSS editor, and that's why I wanted to select it based on the style.

    I found the answer here:

    • Advanced CSS Selector - Select based on styling

    • CSS selector by inline style attribute

    0 讨论(0)
  • 2020-11-22 07:00

    Unlike what I see in every single other answer, you don't need to use pseudo elements in order to replace the content of a tag with an image

    <div class="pvw-title">Facts</div>
    
        div.pvw-title { /* No :after or :before required */
            content: url("your URL here");
        }
    
    0 讨论(0)
  • 2020-11-22 07:01

    I found a solution like this where a word, "Dark", would be shortened to just "D" on a smaller screen width. Basically you just make the font size of the original content 0 and have the shortened form as a pseudo element.

    In this example the change happens on hover instead:

    span {
      font-size: 12px;
    }
    
    span:after {
      display: none;
      font-size: 12px;
      content: 'D';
      color: red;
    }
    
    span:hover {
      font-size: 0px;
    }
    
    span:hover:after {
      display: inline;
    }
    <span>Dark</span>

    0 讨论(0)
  • 2020-11-22 07:02

    The way to make this work is to add line-height to the CSS content. This will make the block to be seen above the hidden, thus this will not hide the changed text.

    Example with use before:

    .pvw-title span {
      display: none;
    }
    
    .pvw-title:before {
      content: 'Whatever it is you want to add';
      line-height: 1.5em
    }
    
    0 讨论(0)
提交回复
热议问题