When the parent DIV is hovered over, the image and text properties will be altered

Greetings,

I am just starting out in web development and would like to seek guidance from the Stack Overflow community. Here is a link to my code: https://codepen.io/noxwon/pen/RwrmxKX

My code is very basic and filled with repetitive lines. I have multiple hover functions for different boxes, each with similar actions. I want to simplify this code, as I find it challenging to handle repetitive tasks with loops.

Your suggestions and advice on simplifying this code would be greatly appreciated.

Thank you for your time and assistance.

Answer №1

You can achieve the desired effect without the need for javascript by utilizing CSS. By adjusting the styling of elements on hover, you can display or hide images using the CSS display property.

.box {
   background: white;
   color: black;
}

.box:hover {
   background: red;
   color: white;
}

.box:hover .img-1 {
   display: none
}

.box:hover .img-2 {
   display: block
}

.wrapper-icons {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
}

.box {
    background-color: #fafafa;
    box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.2), 0px 0px 2px rgba(0, 0, 0, 0.2);
    width: 180px;
    margin: 20px;
    display: flex;
    flex-direction: column;
    cursor: pointer;
}

.box:hover {
  background-color: #DA4E51;
}

.box:hover a.icon-link {
  color: #FFF;
}

.box:hover .icon-img-1 {
  display: none;
}

.box:hover .icon-img-2 {
  display: inline;
}

.box .text {
    padding: 24px;
    text-align: center;
}

a.icon-link {
    text-decoration: none;
    color: #000;
}

.icon-img-2 {
  display: none;
}

img { width: 70px; }
<div class="wrapper-icons">
  
  <div class="box">
    
    <a class="icon-link" href="https://google.com">
      
      <div class="text">
        
        <div class="title">
          
          <img class="icon-img-1" 
               src="http://icons.iconarchive.com/icons/fasticon/angry-birds/128/yellow-bird-icon.png" name="MyImage" />
          
          <img class="icon-img-2" 
               src="http://icons.iconarchive.com/icons/fasticon/angry-birds/128/red-bird-icon.png" name="MyImage" />
          
        </div>
        
        <p class="box-text">SET1</p>
        
        <p>Movie</p>
        
      </div>
    </a>
    
  </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

Remove a specific line from a PHP script

I have developed a system of alerts that allows users to delete an alert if they choose to do so. Below is the code for the delete button and table: <button type="button" name="Delete" Onclick="if(confirm('Are you sure you ...

Invert the motion within a photo carousel

Is there anyone who can assist me with creating a unique photo slider using HTML, CSS, and JS? Currently, it includes various elements such as navigation arrows, dots, and an autoplay function. The timer is reset whenever the arrows or dots are clicked. Ev ...

Including an image side by side with clickable text within a list

Check out my progress here: http://jsfiddle.net/dDK6z/ I've experimented with using display:inline(-block), as well as adjusting margins, padding, and more, but I can't seem to get the image to move down or the text to move up. Does anyone have ...

illuminate selected row in datatable using a jquery plugin

My data is displayed using the jQuery plugin DataTable, and everything appears to be working well. I am retrieving my data from PHP using AJAX. However, I encountered an issue when trying to highlight the row that was hovered over while navigating around ...

Remove HTML tags from a table cell containing a combination of radio buttons and labels

Javascript Function: My JavaScript function posted below is designed to iterate through the column indexes specified in the 2nd parameter and also iterate through the element ids provided in the 3rd parameter. It will then populate the textbox, radiobutto ...

Display an AJAX div when the image is hovered over and have it track the movement of

I am seeking assistance with my website project, which involves providing users with download links to movies. However, I am struggling to make the preview_block div(id) appear when the mouse hovers over the movie_block div(id). Additionally, I am having ...

Is it possible to determine the height of a div after it has been rendered using the .resize method?

In my current structure, <div id="A"> <div id="A1"> <div id="B1"></div> <div id="B2"></div> </div> <div id="A2"></div> </div> A2 and B2 contain tables, while B1 has 4 checkboxes. I&a ...

Accessing Form Data with jQuery for AJAX Request

Is there a way to send form values through AJAX to a PHP file? How can I gather the form values and pass them as data in the AJAX request? $.ajax({ type: "POST", data: "submit=1&username="+username+"&email="+email+"&password="+ ...

A serene web service that delivers XML data

I'm currently trying to navigate a restful webservice that responds with xml data. I'm fairly new to working with restful web services, so I'm feeling a bit lost on what steps to take next. From what I understand, it seems like the issue lie ...

Is there a way to make a link clickable but also prevent another link from being activated when clicked?

Have you ever utilized #link to navigate to a specific section on a webpage? You can also incorporate animate and scrollTop() to ensure a smooth scroll. However, when the #link (Hash link) is situated in the navigation menu, it must be structured as exampl ...

Unable to perform updates for personalized fonts

I've been trying to incorporate custom fonts into my website, but I seem to be facing difficulties in actually implementing them. Could someone please let me know if I am doing it correctly? .pattern{ background:rgba(0,0,0,.5) url(../images/pattern ...

The Jquery Ajax image loading function will only work successfully when an alert statement is included within the $(window).one function

I'm facing a unique challenge. I am loading a JSON file containing the src, width, and height of multiple images. My goal is to load them into the smooth script jQuery plugin and automatically scroll through the images. The "viewing area" div can ac ...

Is there a way for me to access the property value utilized in the Cypress test?

I am currently working on a Cypress example that can be found at the following link: : cy.get('[data-test-id="test-example"]') .invoke('css', 'position') .should('equal', 'static') Despite my ...

I have successfully implemented a click effect on one image and now I wish to apply the same effect to the remaining images

I've exhausted all my options and still can't crack this puzzle. Let me break it down for you: Upon entering the page, users are greeted with a collection of images each tagged with classes (for example, img01 and img02). When an image is clicke ...

Having trouble changing a CSS property (margin-right)?

I have created a simple HTML list that consists of various items: <div id="menu"> <ul> <li>apples</li> <li>&#149</li> <li>bananas</li> <li>oranges</li> <li>grape ...

The Jquery onclick function is executing multiple times, with each iteration increasing in

I have encountered an interesting problem that I can't seem to resolve. The issue involves dataTables and the data that is retrieved via jQuery ajax post after a selection change on a select element. Furthermore, I have an onclick function for multipl ...

Incorporate the casper function within the casper.evaluate() method

Is it possible to use the casper function inside casper.evaluate() with jQuery code? I need to iterate through elements in a way similar to how jQuery does. I have loaded the jquery.js library. Here is the script I have tried: casper.evaluate(function() ...

Tips for creating a responsive graphic using Bootstrap CSS

I am currently trying to determine how to create individual graphics that can be directly loaded via CSS and made responsive. When I resize the screen, the graphics also adjust accordingly. However, on smaller displays such as mobile phones, the graphics ...

Which is better: Starting with rows or columns in Bootstrap 4 layout design?

Many Bootstrap 4 grid layout demos feature columns within rows, but is it considered improper to have rows within columns? For example: <div class="col col-md-6"> <div class="row"> </div> </div> ...

additional space within bootstrap columns

I'm looking to eliminate the empty space above the 4th span and replace it with the next span. Edit: Without adding a new row. Here's the fiddle: http://jsfiddle.net/Dzh5E/7/ <div class="main-app"> <div class="row-fluid"> ...