How can I replace text with CSS?

前端 未结 21 2136
暗喜
暗喜 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 07:08

    Or maybe you could wrap 'Facts' round a <span> as follows:

    <div class="pvw-title"><span>Facts</span></div>
    

    Then use:

    .pvw-title span {
      display: none;
    }
    .pvw-title:after {
      content: 'whatever it is you want to add';
    }
    
    0 讨论(0)
  • 2020-11-22 07:08

    I had an issue where I had to replace the text of link, but I couldn't use JavaScript nor could I directly change the text of a hyperlink as it was compiled down from XML. Also, I couldn't use pseudo elements, or they didn't seem to work when I had tried them.

    Basically, I put the text I wanted into a span and put the anchor tag underneath it and wrapped both in a div. I basically moved the anchor tag up via CSS and then made the font transparent. Now when you hover over the span, it "acts" like a link. A really hacky way of doing this, but this is how you can have a link with different text...

    This is a fiddle of how I got around this issue

    My HTML

    <div class="field">
        <span>This is your link text</span><br/>
        <a href="//www.google.com" target="_blank">This is your actual link</a>
    </div>
    

    My CSS

     div.field a {
         color: transparent;
         position: absolute;
         top:1%;
     }
     div.field span {
         display: inline-block;
     }
    

    The CSS will need to change based off your requirements, but this is a general way of doing what you are asking.

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

    Obligatory: This is a hack: CSS isn't the right place to do this, but in some situations - eg, you have a third party library in an iframe that can only be customized by CSS - this kind of hack is the only option.

    You can replace text through CSS. Let's replace a green button that has the word 'hello' with a red button that has the word 'goodbye', using CSS.

    Before:

    After:

    See http://jsfiddle.net/ZBj2m/274/ for a live demo:

    Here's our green button:

    <button>Hello</button>
    
    button {
      background-color: green;
      color: black;
      padding: 5px;
    }
    

    Now let's hide the original element, but add another block element afterwards:

    button {
      visibility: hidden;
    }
    button:after {
      content:'goodbye'; 
      visibility: visible;
      display: block;
      position: absolute;
      background-color: red;
      padding: 5px;
      top: 2px;
    }
    

    Note:

    • We explicitly need to mark this as a block element, 'after' elements are inline by default
    • We need to compensate for the original element by adjusting the pseudo-element's position.
    • We must hide the original element and display the pseudo element using visibility. Note display: none on the original element doesn't work.
    0 讨论(0)
  • 2020-11-22 07:12

    If you're willing to use pseudo elements and let them insert content, you can do the following. It doesn't assume knowledge of the original element and doesn't require additional markup.

    .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 */
    }
    

    JSFiddle Example

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

    Using a pseudo element, this method doesn't require knowledge of the original element and doesn't require any additional markup.

    #someElement{
        color: transparent; /* You may need to change this color */
        position: relative;
    }
    #someElement:after { /* Or use :before if that tickles your fancy */
        content: "New text";
        color: initial;
        position: absolute;
        top: 0;
        left: 0;
    }
    
    0 讨论(0)
  • 2020-11-22 07:15

    I use this trick:

    .pvw-title {
        text-indent: -999px;
    }
    .pvw-title:after {
        text-indent: 0px;
        float: left;
        content: 'My New Content';
    }
    

    I've even used this to handle internationalization of pages by just changing a base class...

    .translate-es .welcome {
        text-indent: -999px;
    }
    .translate-es .welcome:after {
        text-indent: 0px;
        float: left;
        content: '¡Bienvenidos!';
    }
    
    0 讨论(0)
提交回复
热议问题