Home > Article > Web Front-end > How to Change Content on Hover Using CSS: Can you Replace \"NEW\" with \"ADD\" using the `content` Property?
Introduction
Changing content on hover is a common requirement in web development, allowing users to interact with elements by displaying additional information or altering the text. CSS offers a powerful solution for this using the content property along with ::after and ::before pseudo-elements.
Understanding the Problem:
The goal is to change the content of a label from 'NEW' to 'ADD' when the mouse hovers over it. In the given code snippet, the developer attempted to achieve this using CSS alone, but encountered difficulties.
Exploring the Solution:
The key CSS property for this task is content. It allows us to insert or modify the content of an element dynamically. By using the ::after pseudo-element, we can add new content after the existing content. Here's the modified CSS:
<code class="css">.item:hover a p.new-label:after { content: 'ADD'; }</code>
Implementation:
Example:
<code class="html"><div class="item"> <a href=""> <p class="label success new-label"><span class="align">New</span></p> </a> </div></code>
Conclusion:
By combining the content property with ::after, we can effectively change the content of an element on hover. This versatile technique empowers developers to create interactive and responsive user interfaces without relying on JavaScript or other scripting methods.
The above is the detailed content of How to Change Content on Hover Using CSS: Can you Replace \"NEW\" with \"ADD\" using the `content` Property?. For more information, please follow other related articles on the PHP Chinese website!