Display text over image on hover
Display text over image on hover, using ::before or normal elements.
by thelifeisyours
HTML
<div id="image-1" class="image-container">
<img src="https://via.placeholder.com/340x280/#fff" />
</div>
<div id="image-2" class="image-container">
<img src="https://via.placeholder.com/340x280/#fff" />
</div>
<div id="image-3" class="image-container">
<div id="image-title">TITLE FOR AWESOME IMAGE!!!</div>
<img src="https://via.placeholder.com/340x280/#fff" />
</div>
CSS
/*--------------------------------*/
/* IMAGE CONTAINER STYLES */
/*--------------------------------*/
.image-container {
position: relative;
width: fit-content;
height: fit-content;
/*Important*/
margin: .5em;
}
/*--------------------------------*/
/* STLYE FOR FIRST IMAGE */
/*--------------------------------*/
#image-1:hover::before {
content: "HOVERING MF!!! YAAAS1!!";
text-align: center;
color: white;
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background: rgba(0, 255, 0, 0.6);
}
/*--------------------------------*/
/* STLYE FOR SECOND IMAGE */
/*--------------------------------*/
#image-2::before {
content: "";
color: white;
position: none;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background: rgba(0, 0, 0, 0);
transition: background ease .3s;
}
#image-2:hover::before {
content: "HOVERING MF!!! WITH TRANSITIONS !!! YAAAS1!!";
text-align: center;
color: white;
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background: rgba(0, 0, 255, 0.6);
}
/*--------------------------------*/
/* STLYE FOR THIRD IMAGE */
/*--------------------------------*/
#image-title {
display: flex;
opacity: 0;
color: white;
width: 100%;
height: 100%;
position: absolute;
align-items: center;
justify-content: space-around;
background: rgba(255, 0, 0, 0.6);
transition: opacity ease .3s;
}
.image-container:hover>#image-title {
opacity: 1;
}
/*--------------------------------*/
/* GENERAL STYLES */
/*--------------------------------*/
html,
body {
padding: 0em;
margin: 0 auto;
width: 100%;
height: 100%;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content:...