How can I replace text with CSS?

前端 未结 21 2135
暗喜
暗喜 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:03

    Based on mikemaccana’s answer, this worked for me

    button {
      position: absolute;
      visibility: hidden;
    }
    
    button:before {
      content: "goodbye";
      visibility: visible;
    }
    

    § Absolute positioning

    an element that is positioned absolutely is taken out of the flow and thus takes up no space when placing other elements.

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

    Well, as many said this is a hack. However, I'd like to add more up-to-date hack, which takes an advantage of flexbox and rem, i.e.

    • You don't want to manually position this text to be changed, that's why you'd like to take an advantage of flexbox
    • You don't want to use padding and/or margin to the text explicitly using px, which for different screen sizes on different devices and browsers might give different output

    Here's the solution, in short flexbox makes sure that it's automatically positioned perfectly and rem is more standardized (and automated) alternative for pixels.

    CodeSandbox with code below and output in a form of a screenshot, do please read a note below the code!

    h1 {
      background-color: green;
      color: black;
      text-align: center;
      visibility: hidden;
    }
    h1:after {
      background-color: silver;
      color: yellow;
      content: "This is my great text AFTER";
      display: flex;
      justify-content: center;
      margin-top: -2.3rem;
      visibility: visible;
    }
    h1:before {
      color: blue;
      content: "However, this is a longer text to show this example BEFORE";
      display: flex;
      justify-content: center;
      margin-bottom: -2.3rem;
      visibility: visible;
    }
    

    Note: for different tags you might need different values of rem, this one has been justified for h1 and only on large screens. However with @media you could easily extend this to mobile devices.

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

    This is simple, short, and effective. No additional HTML is necessary.

    .pvw-title { color: transparent; }
    
    .pvw-title:after {
            content: "New Text To Replace Old";
            color: black; /* set color to original text color */
            margin-left: -30px;
            /* margin-left equals length of text we're replacing */
        }
    

    I had to do this for replacing link text, other than home, for WooCommerce breadcrumbs

    Sass/Less

    body.woocommerce .woocommerce-breadcrumb > a[href$="/shop/"] {
        color: transparent;
        &:after {
            content: "Store";
            color: grey;
            margin-left: -30px;
        }
    }
    

    CSS

    body.woocommerce .woocommerce-breadcrumb > a[href$="/shop/"] {
        color: transparent;
    }
    
    body.woocommerce .woocommerce-breadcrumb > a[href$="/shop/"]&:after {
        content: "Store";
        color: @child-color-grey;
        margin-left: -30px;
    }
    
    0 讨论(0)
  • 2020-11-22 07:05

    This worked for me with inline text. It was tested in Firefox, Safari, Chrome, and Opera.

    <p>Lorem ipsum dolor sit amet, consectetur <span>Some Text</span> adipiscing elit.</p>
    
    span {
        visibility: hidden;
        word-spacing: -999px;
        letter-spacing: -999px;
    }
    
    span:after {
        content: "goodbye";
        visibility: visible;
        word-spacing: normal;
        letter-spacing: normal;
    }
    
    0 讨论(0)
  • 2020-11-22 07:05

    This implements a checkbox as a button which shows either Yes or No depending on its 'checked' state. So it demonstrates one way of replacing text using CSS without having to write any code.

    It will still behave like a checkbox as far as returning (or not returning) a POST value, but from a display point of view it looks like a toggle button.

    The colours may not be to your liking, they're only there to illustrate a point.

    The HTML is:

    <input type="checkbox" class="yesno" id="testcb" /><label for="testcb"><span></span></label>
    

    ...and the CSS is:

    /* --------------------------------- */
    /* Make the checkbox non-displayable */
    /* --------------------------------- */
    input[type="checkbox"].yesno {
        display:none;
    }
    /* --------------------------------- */
    /* Set the associated label <span>   */
    /* the way you want it to look.      */
    /* --------------------------------- */
    input[type="checkbox"].yesno+label span {
        display:inline-block;
        width:80px;
        height:30px;
        text-align:center;
        vertical-align:middle;
        color:#800000;
        background-color:white;
        border-style:solid;
        border-width:1px;
        border-color:black;
        cursor:pointer;
    }
    /* --------------------------------- */
    /* By default the content after the  */
    /* the label <span> is "No"          */
    /* --------------------------------- */
    input[type="checkbox"].yesno+label span:after {
        content:"No";
    }
    /* --------------------------------- */
    /* When the box is checked the       */
    /* content after the label <span>    */
    /* is "Yes" (which replaces any      */
    /* existing content).                */
    /* When the box becomes unchecked the*/
    /* content reverts to the way it was.*/
    /* --------------------------------- */
    input[type="checkbox"].yesno:checked+label span:after {
        content:"Yes";
    }
    /* --------------------------------- */
    /* When the box is checked the       */
    /* label <span> looks like this      */
    /* (which replaces any existing)     */
    /* When the box becomes unchecked the*/
    /* layout reverts to the way it was. */
    /* --------------------------------- */
    input[type="checkbox"].yesno:checked+label span {
        color:green;
        background-color:#C8C8C8;
    }
    

    I've only tried it on Firefox, but it's standard CSS so it ought to work elsewhere.

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

    If you just want to show different texts or images, keep the tag empty and write your content in multiple data attributes like that <span data-text1="Hello" data-text2="Bye"></span>. Display them with one of the pseudo classes :before {content: attr(data-text1)}

    Now you have a bunch of different ways to switch between them. I used them in combination with media queries for a responsive design approach to change the names of my navigation to icons.

    jsfiddle demonstration and examples

    It may not perfectly answer the question, but it satisfied my needs and maybe others too.

    0 讨论(0)
提交回复
热议问题