JSFiddle - React, Tailwind, and code Playground

by oceog

HTML

<a class="switchedBackground" href="#">
    <div class="bg1"></div>
    <div class="bg2"></div>
    <div class="label">Label</div>
</a>


<a class="switchedBackground2" href="#"> Label </a>

CSS

.switchedBackground {
    width: 300px;
    height: 200px;
    position: relative;
    display: block;
}

.switchedBackground .bg1 ,  .switchedBackground .bg2 {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left:0;
    transition: all 2s;

}
.switchedBackground .bg1 {   
   background: url('http://placehold.it/300x200/000/00ee00&text=1');
   opacity:1;    
}
.switchedBackground .bg2 {
    background: url('http://placehold.it/300x200/000/00ee00&text=2');
    opacity:0;
}

.switchedBackground .label {
    margin: auto;
    position: absolute;
    top: 0;
}

.switchedBackground:hover .bg2 {
    opacity: 1;
}

/* только в chrome */
.switchedBackground2 {
    width: 300px;
    height: 200px;
    display: block;
    background: url('http://placehold.it/300x200/000/00ee00&text=3');
    transition: all 3s;
}
.switchedBackground2:hover {
    background: url('http://placehold.it/300x200/000/00ee00&text=4');
    transition: all 3s;
}