Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change content on hover

Tags:

html

css

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 content from 'NEW' to 'ADD' using only CSS.

body{      font-family: Arial, Helvetica, sans-serif;  }  .item{      width: 30px;  }  a{      text-decoration:none;  }  .label {      padding: 1px 3px 2px;      font-size: 9.75px;      font-weight: bold;      color: #ffffff;      text-transform: uppercase;      white-space: nowrap;      background-color: #bfbfbf;      -webkit-border-radius: 3px;      -moz-border-radius: 3px;      border-radius: 3px;      text-decoration: none;  }  .label.success {      background-color: #46a546;  }    .item a p.new-label span{    position: relative;    content: 'NEW'  }  .item:hover a p.new-label span{    display: none;  }  .item:hover a p.new-label{    content: 'ADD';  }
<div class="item">      <a href="">           <p class="label success new-label"><span class="align">New</span></p>      </a>  </div>

Here's a JSFiddle to show you what I'm working with.

like image 883
Modelesq Avatar asked Sep 27 '13 19:09

Modelesq


People also ask

How can I change image on hover?

Answer: Use the CSS background-image property You can simply use the CSS background-image property in combination with the :hover pseudo-class to replace or change the image on mouseover.

How to change image on hover in CSS?

The approach of this article is to change an image when the user hovering the mouse over it. This task can be simply done by using theCSS background-image property in combination with the :hover pseudo-class to replace or change the image on mouseover. Example: HTML <!DOCTYPE html> <htmllang="en"> <head> <title> How to Change Image on Hover in CSS

How do you use transitions on hover button?

Transition on Hover. CSS transitions allows you to change property values smoothly (from one value to another), over a given duration. Add a transition effect (opacity and background color) to a #R# button on hover: Example.

How do I change the button text when hovering over the button?

Under the content tab, update the button text as follows: Then deploy the hover option for the button text and enter the replacement button text under the hover tab as follows: The text under the hover button text will replace the default button text when hovering over the button.

How to avoid :before or after pseudo elements on hover?

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:


2 Answers

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

like image 132
Sachin Avatar answered Sep 22 '22 16:09

Sachin


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.

like image 29
Adam Zielinski Avatar answered Sep 22 '22 16:09

Adam Zielinski