Sunday Lalbiaknia
Sunday Lalbiaknia

Reputation: 179

how to create hover animation on a div

I wanted to create a div which has a background image and a transparent background color over it with some text. When hover, the transparent background color should slide out towards the bottom of the div as shown in the image:

https://i.ibb.co/pJFPvFB/Screenshot-2019-03-26-Zeplin-Project.png

Left block is default. Right block is hover state.

I modified this snippet: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_image_overlay_fade

I modified the provided style to:

<style>
.container {
  position: relative;
  width: 50%;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100%;
  width: 100%;
  opacity: 0;
  transition: .5s ease;
  background-color: #008CBA;
}

.container .overlay {
  opacity: 0.75;
}

.container:hover .overlay {
  opacity: 0;
}

.text {
  color: white;
  font-size: 20px;
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  text-align: center;
}
</style>

Edited: My Problem

I tried to achieve a simple slideout animation on my as shown in the image I provided. See this: https://i.ibb.co/pJFPvFB/Screenshot-2019-03-26-Zeplin-Project.png

I have tried something like this - https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_image_overlay_fade

I edited the css they provided to the css I provided above.

Please see the image I provided. I wanted to achieve that.

Upvotes: 1

Views: 82

Answers (3)

shaheer
shaheer

Reputation: 50

hope this is what you are looking

.box {
width: 200px; height: 100px;
background-size: 100% 200%;
background-image: linear-gradient(to bottom, red 50%, black 50%);
-webkit-transition: background-position 1s;
-moz-transition: background-position 1s;
transition: background-position 1s;}

http://jsfiddle.net/jxgrtmvy

Upvotes: 0

Praveen
Praveen

Reputation: 1005

Try this.

.container {
  position: relative;
  width: 50%;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}

.overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100%;
  width: 100%;
  transition: .5s ease;
  background-color: #008CBA;
}

.container:hover .overlay {
  transition: .5s ease;
  height: 0;
}

.text {
  color: white;
  font-size: 20px;
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  text-align: center;
}
<div class="container">
  <img src="https://i.ibb.co/pJFPvFB/Screenshot-2019-03-26-Zeplin-Project.png" alt="Avatar" class="image">
  <div class="overlay">
    <div class="text">Hello World</div>
  </div>
</div>

Hope this code may help you.

Upvotes: 2

Rajesh
Rajesh

Reputation: 262

Hi hope this is what you are looking for

Try this fiddle

.container:hover .overlay {
 animation-name: slideDown;
-webkit-animation-name: slideDown;
 animation-duration: 1s;    
-webkit-animation-duration: 1s;
 animation-timing-function: ease;   
-webkit-animation-timing-function: ease;
opacity: 1 !important;              
}

Upvotes: 0

Related Questions