What is a neat trick to conceal an image when we hover over it?

Within my project, I have a grid layout showcasing images of various items through ng-repeat. My goal is to have the images disappear upon hover, and be replaced by a new div of equal size containing all the sub-components of the item. However, instead of this desired behavior, the images are flipping when hovered over. I am encountering difficulties in achieving the intended outcome. Is there a method to accomplish this?

  **hovers.html**
    <html>
   <div   ng-repeat="a in modules">
   <md-card  class="image">   <img src="a.image_url" />
    <div class="overlay">
          /*Some items  */                 
        </div>          
        </md-card> </div>   </html>


   **style.css**


  .image {
  position: relative;
 display: inline-block;
 width: 100%; }

.overlay {
 display: none; }

  .image:hover {display:none;}

  .image:hover .overlay {
  width: 100%;
 height: 100%;
 background: rgba(1, 0, 0, 0.2);
  position: absolute;
 top: 0;
  overflow: auto;
  left: 0;
  display: inline-block;
 -webkit-box-sizing: border-box;
 -moz-box-sizing: border-box;
  box-sizing: border-box;
  }
  .image:hover .overlay img {
    vertical-align: top;
    }

          **home.html**

  <div    class="columns"  width="100%" height="100%">
   <custom-hover    ng-repeat="a in modules" 
            module="a" layout="row">

    </custom-hover>

           </div>



          home.js

   "use strict";

 function customHover($compile){
return {
    restrict: 'E',
     scope: {
             module: "=",

             },

          templateUrl:  "hovers.html",                                                                                                     
    controller: function($scope, $mdDialog) {
        console.log("=======",$scope.module)


    }

         }
            }; 

         module.exports = customHover;

Answer №1

In order to maintain element spacing, modify the display attribute to visibility

.image {
  position: relative;
  visibility: visible;
  width: 100%;
 
}

.overlay {
  display: none;
}

.image:hover img{
  visibility: hidden;
}

.image:hover .overlay{
  position:absolute;
  top:0;
  left:0;
  width: 100%;
  height: 100%;
  background: rgba(1, 0, 0, 0.2);
  overflow: auto;
  display: inline-block;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
<div class="image" ng-repeat="a in modules">
  <md-card>
    <img src="https://image.freepik.com/free-icon/global-symbol_318-68107.jpg" />
    <div class="overlay">
      <p>Text</p>
    </div>
  </md-card>
</div>

Answer №2

If you want to hide the image and display the description when hovering over an image container with the class name image, you can use the following CSS:

  .image:hover img{
    display: none;
  }

  .image:hover .overlay{
    display: block;
  }

Here is the corresponding HTML structure:

  <html>
    <head>
      <style>
        .image {
          position: relative;
          display: inline-block;
        }

        .image:hover img{
          display: none;
        }

        .image:hover .overlay{
          display: block;
        }

        .myCard{
          width: 200px;
          height: 120px;
          overflow: hidden;
        }

        .overlay{
          display: none;
        }

      </style>
      <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.css">
      <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.js"></script>
      <script src="app.js"></script>
    </head>

    <body ng-app="app" ng-controller="com">
      <div  class="image" ng-repeat="a in modules">
        <md-card class="myCard">
          <img ng-src="{{a.image_url}}"/>
          <div class="overlay">
            {{a.desc}}
          </div>
        </md-card>
      </div>
    </body>
  </html>

For AngularJS logic, you can use the following code:

  angular.module("app", [])
    .controller("com", function($scope){
      $scope.modules = [
        {
          "image_url": "a.jpg",
          "desc": "cocktail"
        },
        {
          "image_url": "b.jpg",
          "desc": "cookie"
        },
        {
          "image_url": "c.jpg",
          "desc": "cake"
        }
      ];
    });

The default behavior will show pictures, but upon hovering, it will hide the picture and reveal the description.

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

Adjust the arc angle on the canvas

I am currently working on creating a Semi Arc using Canvas. My question is regarding changing the overall angle of the arc to match the expected image. Expected Image Below is the HTML code for the Semi arc: <canvas id="myCanvas" width="578" height=" ...

Stylish mobile navigation styles with CSS

Hey there, I appreciate any help you can provide. I'm having trouble figuring out the right CSS code to modify the colors of my Mobile Menu only... I'd like to change the placeholder text as well as the text and background of the drop-down menu ...

Attempting to close a basic angular modal leads to an error message stating: Property 'dismiss' cannot be read because it is undefined

I am attempting to create a straightforward modal that appears when a user clicks on a section of my demo app that is still in progress. Everything is functioning correctly until the point where I require the user to click the 'Close' button on ...

Changing Underscores in ES6 Techniques

My current project is built using AngularJS, Redux, and Underscore. Within this project, I have the following code snippet: const selectedBroker = _.findWhere(state.brokers, { brokerId: action.payload }); return state.merge({ selectedBroker, sel ...

Props and theme merge to create uniquely designed styled components with a thematic twist

Out of sheer curiosity, I've been incorporating styled-components into my React application. Within this framework, I make use of the recommended theme to ensure consistency in colors and sizes throughout. Currently, my approach looks something like ...

show certain DIVs based on the URL

Is it possible to dynamically display different sections based on the URL? My event website has 3 subevents, and I'd like to show the date of each event in the navigation bar for their respective URLs. <div id="us" class="us-web-date">&nbs ...

Is there a way to locate a child div using the mouse within a parent div that only has a class name assigned to it?

Hey there, I've been searching for a solution to my issue but haven't found exactly what I need. I'm new to jquery and currently working on creating a newspaper layout form. Adding new divs within the layout canvas is easy with this code: ...

Want to know how to get images to show up when sharing a link on Twitter?

For illustration purposes, I am using a CNN article page: The HTML head section of the webpage contains the following tag: <meta name="twitter:image" content="http://i2.cdn.turner.com/money/dam/assets/150114084151-inside-tracker-120x90.png"> Howe ...

Having difficulty modifying the width of the BODY element using CSS

For my webpage, I want the body to have a silver background that fills only 75% of the page. This means that only 75% of the page will be painted silver, while the remaining part will be left unused and painted according to the browser's defaults. The ...

Wait for the scope value to become available before executing the directive or div

I have a unique directive created for SoundCloud that necessitates the SoundCloud URL. The URL is fetched from the database using the $http service, but the issue arises when the div for the directive is loaded before the URL value is defined. The code fo ...

Ruby on Rails allows for the selection of values in a dropdown menu to trigger the visibility of different sections of HTML

As a newcomer to Rails, I was able to successfully implement the onclick function for a f.checkbox element. Now, my challenge lies in achieving a similar functionality for a f.select element within a _form. Below is the code snippet that works for a chec ...

Iterate through the contents of a directory and display the HTML content corresponding to each file

Currently, I am diving into the world of PHP Laravel. The project I am working on involves repeating blocks of HTML code in my template, which I want to streamline using a loop. My idea is to automate this process by implementing a foreach loop in my Vue ...

Parse the JSON object to retrieve a numerical value and present it as text

When displaying names and points in my table, the points are shown as numbers like 10, 20, 30, 40. I want to assign membership levels based on these points; for example, someone with 10 points should be a bronze member, 20 points for silver, 30 points for ...

What is the best method for removing quotation marks from HTML generated by Django?

I need to show a hyperlink in a form based on information retrieved from a database. Since Django doesn't seem to have built-in support for this feature, I am attempting to create the HTML code manually. Within the model form, I am customizing the in ...

Having trouble retrieving the content within an HTML tag using jQuery?

I'm facing some challenges when it comes to extracting the content of an html tag using the Chrome console. I've been trying different methods for about half an hour, but nothing seems to work. That's why I need some help :) The code I want ...

How does md-select determine duplicate options within md-options based on the criteria set forth?

After working with Angular Material for some time in my project, I encountered a challenge while using md-select. I am facing an issue where I keep getting a Duplicate md-option values error. I understand that md-options requires unique values and I am pa ...

svg xlink attribute not functioning properly when embedded as svg object within html

Within the SVG, I have multiple A elements linking to various pages. When the SVG is loaded into a browser as a standalone, the links function properly. However, when it is embedded as an object within an HTML page, the links do not work. It seems like th ...

Switching over to the latest version of Material-UI, v1.x.x

Currently, my app relies on Material-UI v0.17.0 which is not compatible with React v16.0.0. In order to make it work, I need to upgrade to Material-UI v1.0.0. I came across a migration tool here, but it only updates import statements. Many props have chan ...

Error encountered in onclick handler due to unterminated string literal in PHP and jQuery

Trying to utilize PHP to send variables into the onclick of an element is resulting in an "Unterminated string literal" error due to long strings. Below is a snippet of my PHP code: $query = $conn->prepare("SELECT Name, Image, Description, Link, Price, ...

What could be causing my PHP code to not execute properly alongside the HTML?

I am currently sharpening my skills in PHP by working on a simple web form project. To put it simply, I need to develop a basic web form that accepts a two-dimensional array of European cities along with the distance in kilometers between each pair of citi ...