Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Without using JavaScript, only CSS and HTML: When using ":hover" css selector, is it possible to change text instead of the color?

Here is the basic button hover program. I can control what the color of the button is depending on it’s hover state, but can I change the text inside the button if someone hovers over it?

for example: a button that says "Understand?" and when you hover over it, the text changes to "Yes"

.button {
    width: 100px;
    height: 50px;
    background-color: gold;
    border: 2px solid firebrick;
    border-radius: 10px;
    font-weight: bold;
    color: black;
    cursor: pointer;
}

.button:hover {
    background-color:rgb(0, 255, 0)
}
<button class="button">hover over me!</button>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This should work. I think I’d probably just go ahead and use js for this, but I don’t see any glaring issues w/ this approach. Not ideal for accessibility maybe.

.button {
    width: 100px;
    height: 50px;
    background-color: gold;
    border: 2px solid firebrick;
    border-radius: 10px;
    font-weight: bold;
    color: black;
    cursor: pointer;
}

.button::before {
    display: block;
    content: "Hover over me!"
}

.button:hover::before {
    content: "FOO"
}
<button class="button"></button>

We just move the text content of the button into a ::before pseudo-element, and can control its content via css now.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading