Receiving a sleek black overlay with dynamic text upon hovering over an image

I have been searching extensively for a solution to my issue, but I haven't been able to make it work in my application. What I want is to create a black overlay over an image when it is hovered over, with text that resembles a button appearing on top of it.

Additionally, I need this functionality to work seamlessly with the scale effect on hover. However, on my actual web page, hovering over an image only triggers the scaling and does not change the color of the parent div as intended.

Could someone please point out where I might be going wrong?

$('.home-img-block').find('img').each(function() {
   var imgClass = (this.width / this.height > 1) ? 'wide' : 'tall';
   console.log(imgClass);
   $(this).addClass(imgClass);
 });
#home-img-blocks {
width: 100%;
height: 600px;
}

.home-img-block {
width: 33.33%;
/*height: 100%;*/
display: inline-block;
overflow: hidden;
cursor: pointer;
}
.home-img-block:after {
    content: attr(data-content);
    color:#fff;
    position:absolute;
    width:100%; height:100%;
    top:0; left:0;
    background:rgba(0,0,0,0.6);
    opacity:0;
    transition: all 0.5s;
    -webkit-transition: all 0.5s;
}
.home-img-block:hover:after {
    opacity:1;
}
.home-img-block img{
    -webkit-transition: all 1s ease; /* Safari and Chrome */
    -moz-transition: all 1s ease; /* Firefox */
    -ms-transition: all 1s ease; /* IE 9 */
    -o-transition: all 1s ease; /* Opera */
    transition: all 1s ease;
}
.home-img-block:hover img{
    -webkit-transform:scale(1.25); /* Safari and Chrome */
    -moz-transform:scale(1.25); /* Firefox */
    -ms-transform:scale(1.25); /* IE 9 */
    -o-transform:scale(1.25); /* Opera */
    transform:scale(1.25);
background: rgba(0,0,0,0.3);
width: 33.33%;
max-height: 100%;
}
.home-img-block img.wide {
max-width: 100%;
    max-height: 100%;
height: auto;
}
.home-img-block img.tall {
max-width: 100%;
    max-height: 100%;
width: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="home-img-blocks">
<div data-content="FIND OUT MORE" class="home-img-block"><img src="http://optimumwebdesigns.com/images/test1.jpg"></div><div class="home-img-block"><img src="http://optimumwebdesigns.com/images/test2.jpg">
   </div><div class="home-img-block"><img src="http://optimumwebdesigns.com/images/test3.jpg"></div>
</div>

Answer №1

Here is the solution you requested. Take a look at this. I've included an overlay class for a black overlay effect.

$('.home-img-block').find('img').each(function() {
  var imgClass = (this.width / this.height > 1) ? 'wide' : 'tall';
  console.log(imgClass);
  $(this).addClass(imgClass);
});
* {
  box-sizing: border-box;
}

#home-img-blocks {
  width: 100%;
  height: 600px;
}

.home-img-block {
  width: 33.33%;
  float: left;
  /*height: 100%;*/
  display: inline-block;
  overflow: hidden;
  cursor: pointer;
  position: relative;
}

.home-img-block:hover .overlay {
  background: rgba(0, 0, 0, 0.6);
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

.home-img-block:after {
  content: attr(data-content);
  color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  opacity: 0;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  border: 1px solid #fff;
  padding: 5px;
  text-align: center;
}

.home-img-block:hover:after {
  opacity: 1;
}

.home-img-block img {
  -webkit-transition: all 1s ease;
  /* Safari and Chrome */
  -moz-transition: all 1s ease;
  /* Firefox */
  -ms-transition: all 1s ease;
  /* IE 9 */
  -o-transition: all 1s ease;
  /* Opera */
  transition: all 1s ease;
}

.home-img-block:hover img {
  -webkit-transform: scale(1.25);
  /* Safari and Chrome */
  -moz-transform: scale(1.25);
  /* Firefox */
  -ms-transform: scale(1.25);
  /* IE 9 */
  -o-transform: scale(1.25);
  /* Opera */
  transform: scale(1.25);
  background: rgba(0, 0, 0, 0.3);
  width: 33.33%;
  max-height: 100%;
}

.home-img-block img.wide {
  max-width: 100%;
  max-height: 100%;
  height: auto;
  width: 100%;
}

.home-img-block img.tall {
  max-width: 100%;
  max-height: 100%;
  width: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="home-img-blocks">
  <div data-content="FIND OUT MORE" class="home-img-block">
    <img src="http://optimumwebdesigns.com/images/test1.jpg">
    <div class="overlay"></div>
  </div>
  <div data-content="FIND OUT" class="home-img-block">
    <img src="http://optimumwebdesigns.com/images/test2.jpg">
    <div class="overlay"></div>
  </div>
  <div data-content="FIND" class="home-img-block">
    <img src="http://optimumwebdesigns.com/images/test3.jpg">
    <div class="overlay"></div>
  </div>
</div>

Answer №2

It seems like there are a few missing elements in your CSS that are preventing you from achieving the desired effect. Adding a position relative to the .home-img-block class and repositioning some elements should fix the weird shaking issue when hovering over it.

If you'd like to experiment with the code, here is a link to the jsfiddle:

https://jsfiddle.net/kriscoulson/y32ekgdm/

#home-img-blocks {
  width: 100%;
  height: 600px;
}

.home-img-block {
  width: 33.33%;
  /*height: 100%;*/
  display: inline-block;
  overflow: hidden;
  cursor: pointer;
  position: relative;
}
.home-img-block:after {
  content: attr(data-content);
  color:#fff;
  position:absolute;
  width:100%; height:100%;
  top:0; left:0;
  background:rgba(0,0,0,0.6);
  opacity:0;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
}
.home-img-block:hover:after {
  opacity:1;
}
.home-img-block img{
  -webkit-transition: all 1s ease; Safari and Chrome
  -moz-transition: all 1s ease; Firefox
  -ms-transition: all 1s ease; IE 9
  -o-transition: all 1s ease; Opera
  transition: all 1s ease;
}
.home-img-block:hover img{
  transform:scale(1.25);
  background: rgba(0,0,0,0.3);
}
.home-img-block img.wide {
  max-width: 100%;
  max-height: 100%;
  height: auto;
}
.home-img-block img.tall {
  max-width: 100%;
  max-height: 100%;
  width: auto;
}

Answer №3

It appears that there may be a css issue, specifically relating to the following:

home-img-block:hover img

and

.home-img-block img

It seems like .home-img-block is enclosing the HTML content without being too specific. Additionally, the first CSS line for hover setting appears to be incorrect. I have created a fiddle with the modified code.

https://jsfiddle.net/kuape5np/

Could you please confirm if this aligns with your intended outcome?

Answer №4

Take a look at this, I think it's exactly what you're searching for

.home-img-block {
width: 33.33%;
display: inline-block;
overflow: hidden;
cursor: pointer;
    position: relative;
}

.home-img-block:hover:after {
    opacity:1;
}
.home-img-block img{
    -webkit-transition: all 1s ease; /* Safari and Chrome */
    -moz-transition: all 1s ease; /* Firefox */
    -ms-transition: all 1s ease; /* IE 9 */
    -o-transition: all 1s ease; /* Opera */
    transition: all 1s ease;
width: 100%;
}
.home-img-block:hover img{
    -webkit-transform:scale(1.25); /* Safari and Chrome */
    -moz-transform:scale(1.25); /* Firefox */
    -ms-transform:scale(1.25); /* IE 9 */
    -o-transform:scale(1.25); /* Opera */
    transform:scale(1.25);
background: rgba(0,0,0,0.3);
max-height: 100%;
}
.home-img-block:after {
    background: rgba(0,0,0,.5);
    content: "";
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    opacity: 0;
-webkit-transition: all 1s ease; /* Safari and Chrome */
    -moz-transition: all 1s ease; /* Firefox */
    -ms-transition: all 1s ease; /* IE 9 */
    -o-transition: all 1s ease; /* Opera */
    transition: all 1s ease;
}

.hover {
    position: absolute;
    z-index: 99;
    top: 50%;
    left: 50%;
    transform: translate(-50% , -50%);
    opacity: 0;
-webkit-transition: all 1s ease; /* Safari and Chrome */
    -moz-transition: all 1s ease; /* Firefox */
    -ms-transition: all 1s ease; /* IE 9 */
    -o-transition: all 1s ease; /* Opera */
    transition: all 1s ease;
}
.hover h2 {
color:#fff;
opacity: 0;
-webkit-transition: all 1s ease; /* Safari and Chrome */
    -moz-transition: all 1s ease; /* Firefox */
    -ms-transition: all 1s ease; /* IE 9 */
    -o-transition: all 1s ease; /* Opera */
    transition: all 1s ease;
}

.home-img-block:hover:after,.home-img-block:hover .hover, .home-img-block:hover .hover h2 {
    opacity: 1;
}
    <div id="home-img-blocks">
<div class="home-img-block">
     <img src="http://optimumwebdesigns.com/images/test1.jpg">
 <div class="hover">
    <h2>FIND OUT MORE</h2>
  </div>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to adjust the vertical background-position independently?

While creating a navigation bar using the <ul> element, I have come up with the following CSS code: .nav { list-style: none; background: url(/images/nav.png) no-repeat; width: 666px; height: 60px; } .nav li { font-size: 0px; backgr ...

What is the best way to merge variable name in AngularJS?

How to combine variable name in HTML code: app.controller example $scope.name ='abc'; $scope.abc123 = response.data; HTML example <h1>{{name}}</h1> <h1>{{{{name}}123}}</h1> <!-- I want the value of abc ...

When should vuex be used for storing data instead of relying on local component data?

Currently I am tackling a complex project that is built using Vue. Our team has opted to use Vuex as our state management system, however there are certain components where the data is not needed elsewhere. Should I follow convention and store this data ...

Images showing Strava heat maps retrieved through API

Check out this amazing heatmap created by Strava! I'm curious about how they were able to achieve this - it seems like they are using the API to request overlay images based on the network tab. I have my own geo data, but I'm wondering how I can ...

What is the best method to deallocate and remove the background-color property of CSS once it has been defined?

There is a perplexing issue that I am unable to resolve. The "Shop" section on this page has an intriguing translucent blue-green background: This background is set using the ID "#left-area". Interestingly, on another page, which can be found at , the sam ...

Creating a code script for the purpose of automating npm commands

Currently, I am immersed in an angular js project and I have a desire to streamline the execution of the following two commands. ./node_modules/protractor/bin/webdriver-manager update ./node_modules/protractor/bin/webdriver-manager start The challenge li ...

Utilizing Selenium to inject javascript permanently or on every page refresh

In my selenium webdriver test using Python 3, I have implemented a semi-automated process. This means that some routines are automated while other tasks require user interaction with the browser, such as page refreshes or redirects. My challenge is to inj ...

Position the button to the right using the Bootstrap float-right class

Currently, I am utilizing reactstrap within my React component. My primary intention is to position a button to the right side of the container but unfortunately, the following code doesn't achieve the desired result: <td class="col-md-4 clearfix" ...

How to reference an image located in one folder from a page in a different folder using React

Imagine there is Folder A containing an image called 'image.jpg' and Folder B with an HTML page that needs to access the image in Folder A in order to display it. The following code successfully accomplishes this task: <img src={require(&apo ...

GeoJson with timestamp and marked directional indicator

I am looking to create an animation of a boat traveling along a specific route. Currently, I am able to display the route as a line and the boat as a circle using TimestampedGeoJson: # circle with following line features = [ { 'type': ...

No data returned in AJAX response while trying to connect to a RESTful service

After creating a WCF REST service that returns a JSON response like {"Id":10,"Name":"Peter"}, I noticed that when trying to access it using the provided HTML5 code snippet, the status returned is always 0. Can anyone help me figure out what might be caus ...

Incorporating video.js into an Angular website application

I've encountered a strange issue while attempting to integrate video.js into my angular app. <video id="example_video_1" class="video-js vjs-default-skin" controls preload="none" width="300" height="264" poster="http://video-js.zenco ...

While utilizing a dynamic PHP navigation header, the nested divs are shifting below the container div

After successfully implementing a dynamic PHP header on my site, I encountered issues when trying to incorporate the remaining content within the container div along with the header div. The additional content is not displaying inside the container but is ...

Display an alert using JQuery if the page remains inactive for a specific amount of time, ensuring it only triggers once

I've been browsing different solutions on how to display an alert or popup when there is no activity on a webpage. However, none of the examples I found actually match what I want to achieve. Here's what I'm looking for: Load the webpage. ...

update the componentWillMount() function

I am currently exploring the code found at https://github.com/Spyna/react-store/blob/master/src/createStore.js What adjustments do I need to make in order to align with the deprecated componentWillMount lifecycle method? CreateStore const createStore = ...

The second AJAX request isn't functioning properly; it should only be executed once the first one has successfully completed

I am experiencing an issue with my ajax calls. The second call should only be triggered after the first one is successful. However, even though the first call always returns success, I keep getting an error for the second one. function proceedWithUnlock(t ...

Using npm to install packages with multiple package.json files

My current project includes a submodule with another submodule, each having their own package.json file. This is how I set up my project: |root ----|node_modules ----|package.json ----|someFolder ----|submodule_1 -------- |package.json -------- |someFold ...

Create dynamic animations on HTML text using CSS

Looking to add some flair to your website with a text animation? How about making the text automatically glide from one side to the other like a ticker display? Any suggestions for simple CSS code to achieve this would be greatly appreciated! Thanks in ad ...

Is there a way to modify the text color within the thumb-label of the Vuetify v-slider component?

Lately, I've been facing some challenges and my objective is to change the color of the thumb label on my v-slider to a custom one that is defined in the component's design. Can anyone provide guidance on how to achieve this? Regards, Joost ...

Safari is experiencing issues with the fill path of SVG icons, unlike Chrome where it is functioning properly

https://i.stack.imgur.com/XaKE4.jpgI'm having trouble with my SVG icons displaying correctly in Chrome but not in Safari. Does anyone have any tips or suggestions to fix this issue? Any help is appreciated. If anything is unclear about the problem, p ...