Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

HTML-CSS: Image overlay on hover

HTML-CSS : Exercise-3 with Solution

Using HTML, CSS create display an image overlay effect on hover.

  • Use the :before and :after pseudo-elements for the top and bottom bars of the overlay respectively. Set their opacity, transform and transition to produce the desired effect.
  • Use the <figcaption> for the text of the overlay. Set display: flex, flex-direction: column and justify-content: center to center the text into the image.
  • Use the :hover pseudo-selector to update the opacity and transform of all the elements and display the overlay.

HTML Code:

<!--License: https://bit.ly/3GjrtVF-->
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Using HTML, CSS create display an image overlay effect on hover</title>
</head>
<body>
<figure class="hover-img"><strong>Preview:</strong><br>
  <img src="https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-2.jpeg"/>
  <figcaption>
    <h3>Pansy <br/>Flower</h3>
  </figcaption>
</figure>
</body>
</html>

CSS Code:

<style> 

.hover-img {
  background-color: #000;
  color: #fff;
  display: inline-block;
  margin: 8px;
  max-width: 250px;
  min-width: 240px;
  overflow: hidden;
  position: relative;
  text-align: center;
  width: 100%;
}

.hover-img * {
  box-sizing: border-box;
  transition: all 0.45s ease;
}

.hover-img:before,
.hover-img:after {
  background-color: rgba(0, 0, 0, 0.5);
  border-top: 32px solid rgba(0, 0, 0, 0.5);
  border-bottom: 32px solid rgba(0, 0, 0, 0.5);
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  content: '';
  transition: all 0.3s ease;
  z-index: 1;
  opacity: 0;
  transform: scaleY(2);
}

.hover-img img {
  vertical-align: top;
  max-width: 100%;
  backface-visibility: hidden;
}

.hover-img figcaption {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  align-items: center;
  z-index: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  line-height: 1.1em;
  opacity: 0;
  z-index: 2;
  transition-delay: 0.1s;
  font-size: 24px;
  font-family: sans-serif;
  font-weight: 400;
  letter-spacing: 1px;
  text-transform: uppercase;
}

.hover-img:hover:before,
.hover-img:hover:after {
  transform: scale(1);
  opacity: 1;
}

.hover-img:hover > img {
  opacity: 0.7;
}

.hover-img:hover figcaption {
  opacity: 1;
}

</style>

HTML-CSS Editor:

See the Pen html-css-practical-exercises by w3resource (@w3resource) on CodePen.


What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



HTML-CSS: Tips of the Day

What is the difference between id and class in CSS, and when should I use them?

Example:

<div id="header_id" class="header_class">
Text
</div>
#header_id {font-color:#fff}
.header_class {font-color:#000}

(Note that CSS uses the prefix # for IDs and . for Classes.)

However color was an HTML 4.01 <font> tag attribute deprecated in HTML 5. In CSS there is no "font-color", the style is color so the above should read:

Example:

<div id="header_id" class="header_class">
Text
</div>
#header_id {color:#fff}
.header_class {color:#000} 

The text would be white.

Ref: https://bit.ly/39Stv4P