The scenario:
If a user hovers over a text (e.g. an h1-tag), it should change to a new text. The new text should appear smoothly.
What I've done so far:
I was able to replace a text with a new one with the "display:none" and the "content: 'This is the new text' "-property. My problem is that the new text does not appear smoothly (it does not fade in/transitions). I've also tried to use opacity, but it doesn't replace my old text (instead it just disappears and the new text appears next to it).
Here is a JSFiddle and code snippet:
.my_div {
background-color: red;
}
.my_div:hover {
background-color: green;
transition: all 500ms ease-in-out;
}
.my_div:hover .title span {
display: none;
}
.my_div:hover .title:after {
content: "A wild text appears";
}
<div class="my_div">
<h1 class="title"><span>This is the old text</span></h1>
</div>
Here is a really simple sample
(This fiddle use pseudo elements instead of below inner div's)
The question has already been answered, but I think the best approach would be to use a pseudo element. It's very simple and clean. BUT: you lose the transition effect.
OK, so first part, you cannot animate the display property, you need a work-around. To do this we fall back to what we can animate, opacity and width/height
For what you are trying to accomplish, I'd use two spans inside the
<h1>
- one with each text version. Since spans are inline elements we give themdisplay: block
so we can control there dimensions more cleanly.