How to change content on hover

前端 未结 4 1188
走了就别回头了
走了就别回头了 2020-11-29 05:01

I\'ve been playing around with this, and I thought it would be pretty simple. What I\'m trying to do is hover over the \'NEW\' label. Once in its hover state, change the con

相关标签:
4条回答
  • 2020-11-29 05:28

    The CSS content property along with ::after and ::before pseudo-elements have been introduced for this.

    .item:hover a p.new-label:after{
        content: 'ADD';
    }
    

    JSFiddle Demo

    0 讨论(0)
  • 2020-11-29 05:28

    This exact example is present on mozilla developers page:

    ::after

    As you can see it even allows you to create tooltips! :) Also, instead of embedding the actual text in your CSS, you may use content: attr(data-descr);, and store it in data-descr="ADD" attribute of your HTML tag (which is nice because you can e.g translate it)

    CSS content can only be usef with :after and :before pseudo-elements, so you can try to proceed with something like this:

    .item a p.new-label span:after{
      position: relative;
      content: 'NEW'
    }
    .item:hover a p.new-label span:after {
      content: 'ADD';
    }
    

    The CSS :after pseudo-element matches a virtual last child of the selected element. Typically used to add cosmetic content to an element, by using the content CSS property. This element is inline by default.

    0 讨论(0)
  • 2020-11-29 05:32

    This little and simple trick I just learnt may help someone trying to avoid :before or :after pseudo elements altogether (for whatever reason) in changing text on hover. You can add both texts in the HTML, but vary the CSS 'display' property based on hover. Assuming the second text 'Add' has a class named 'add-label'; here is a little modification:

    span.add-label{
     display:none;
    }
    .item:hover span.align{
     display:none;
    }
    .item:hover span.add-label{
     display:block;
    }
    

    Here is a demonstration on codepen: https://codepen.io/ifekt/pen/zBaEVJ

    0 讨论(0)
  • 2020-11-29 05:34

    .label:after{
        content:'ADD';
    }
    .label:hover:after{
        content:'NEW';
    }
    <span class="label"></span>

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