Display an additional div when the mouse hovers over the image

I'm currently working on a "cast" section for one of my projects, and I'd like the actor's character to be revealed when the actor is hovered over. How can I make this happen without leaving a gap on the page when hiding the display of the deadpool div? Ideally, I want the character to only show up when Ryan Reynolds is hovered over.

article {
  display: flex;
  flex-wrap: wrap;
  margin: auto;
  padding-top: 12px;
  padding-bottom: 12px;
  background-color: #8b2323;
  width: 48vw;
  min-height: 200px;
  min-width: 391px;
  font-family: verdana, sans-serif;
  justify-content: center;
}

.castcontainer {
  flex-wrap: wrap;
  min-width: 215px;
  width: 20%;
  height: 30%;
  overflow: hidden;
  padding: 5px;
}

#cast {
  border-radius: 50%;
  width: 100%;
}

.cast2 {
  display: none;
  text-align: center;
  background-color: #8b1a1a;
  border-radius: 10px;
  padding: 10px;
}

.cast:hover+.cast2 {
  display: block;
}

.cast {
  text-align: center;
  background-color: #8b1a1a;
  border-radius: 10px;
  padding: 10px;
}

p {
  background: white;
}
<article>

  <div class="castcontainer">
    <div class="cast">
      <img src="https://pbs.twimg.com/profile_images/741703039355064320/ClVbjlG-.jpg" id="cast">
      <p><b>Ryan Reynalds</b></p>
    </div>
  </div>

  <div class="castcontainer">
    <div class="cast2">
      <img src="http://cdn03.cdn.justjared.com/wp-content/uploads/headlines/2017/08/joi-harris-rip.jpg" id="cast">
      <p><b>Wade Wilson</b></p>
    </div>
  </div>

</article>

Answer №1

Here is a fresh take on your existing code:

.cast * {
box-sizing: border-box;
}

.cast {
border-radius: 10px;
background: #8b2323;
font-family: Verdana, sans-serif;
text-align: center;
padding: 12px;
}

.cast img {
border-radius: 50%;
max-height: 300px;
}

.cast strong {
background: white;
display: block;
border-radius: 10px;
margin-top: 5px;
}

.cast .actor, 
.cast .role {
width: 100%;
}

.cast .actor {
display: block;
z-index: 2;
}

.cast .role {
display: none;
z-index: 1;
}

.cast:hover .actor {
display: none;
}

.cast:hover .role {
display: block;
}
<article class="cast">
<div class="actor">
      <img src="https://pbs.twimg.com/profile_images/741703039355064320/ClVbjlG-.jpg">
      <strong>Ryan Reynalds</strong>
</div>

<div class="role">
<img src="http://cdn03.cdn.justjared.com/wp-content/uploads/headlines/2017/08/joi-harris-rip.jpg">
<strong>Wade Wilson</strong>
</div>
</article>

This approach simplifies the structure and optimizes the show/hide functionality by utilizing the parent wrapper's :hover event to target elements with classes instead of specific IDs. This can make managing transitions more efficient without limiting reusability.

It's important to ensure that the images within each element are of consistent dimensions to prevent awkward resizing effects during transitions between elements.

Answer №2

Is this the solution you've been searching for? Recently added:

article:hover .cast {
  display: none;
}

article:hover .cast2 {
  display: block;
}

article {
  display: flex;
  flex-wrap: wrap;
  margin: auto;
  padding-top: 12px;
  padding-bottom: 12px;
  background-color: #8b2323;
  width: 48vw;
  min-height: 200px;
  min-width: 391px;
  font-family: verdana, sans-serif;
  justify-content: center;
}

article:hover .cast {
  display: none;
}

article:hover .cast2 {
  display: block;
}

.castcontainer {
  flex-wrap: wrap;
  min-width: 215px;
  width: 20%;
  height: 30%;
  overflow: hidden;
  padding: 5px;
}

#cast {
  border-radius: 50%;
  width: 100%;
}

.cast2 {
  display: none;
  text-align: center;
  background-color: #8b1a1a;
  border-radius: 10px;
  padding: 10px;
}

.cast:hover+.cast2 {
  display: block;
}

.cast {
  text-align: center;
  background-color: #8b1a1a;
  border-radius: 10px;
  padding: 10px;
}

p {
  background: white;
}
<article>

  <div id="one" class="castcontainer">
    <div class="cast">
      <img src="https://pbs.twimg.com/profile_images/741703039355064320/ClVbjlG-.jpg" id="cast">
      <p><b>Ryan Reynalds</b></p>
    </div>
  </div>

  <div id="two"class="castcontainer">
    <div class="cast2">
      <img src="http://cdn03.cdn.justjared.com/wp-content/uploads/headlines/2017/08/joi-harris-rip.jpg" id="cast">
      <p><b>Wade Wilson</b></p>
    </div>
  </div>

</article>

Answer №3

<section>

  <div class="performercontainer" id="show1">
    <div class="performer">
      <img src="https://pbs.twimg.com/profile_images/741703039355064320/ClVbjlG-.jpg" class="performerImg" id="PerformerImgRef">
      <p><b>Ryan Reynalds</b></p>
    </div>
  </div>

  <div class="performercontainer" id="show2">
    <div class="performer2">
     <img src="http://cdn03.cdn.justjared.com/wp-content/uploads/headlines/2017/08/joi-harris-rip.jpg" class="performerImg">
     <p><b>Wade Wilson</b></p>
    </div>
  </div>

</section>

jQuery(function ($) {
   $('#show1').hover(function () {
       $(this).find('img').attr('src', function (i, src) {
           return src.replace('https://pbs.twimg.com/profile_images/741703039355064320/ClVbjlG-.jpg', 'http://cdn03.cdn.justjared.com/wp-content/uploads/headlines/2017/08/joi-harris-rip.jpg')
        })
       $('#textChange').text('Wade Wilson');
   }, function () {
       $(this).find('img').attr('src', function (i, src) {
           return src.replace('http://cdn03.cdn.justjared.com/wp-content/uploads/headlines/2017/08/joi-harris-rip.jpg', 'https://pbs.twimg.com/profile_images/741703039355064320/ClVbjlG-.jpg')
        })
        $('#textChange').text('Ryan Reynalds');
   })
})

Include this jQuery code for smooth functionality.

https://jsfiddle.net/dLwxm2ox/8/

Answer №4

article {
  display: flex;
  flex-wrap: wrap;
  margin: auto;
  padding-top: 12px;
  padding-bottom: 12px;
  background-color: #8b2323;
  width: 48vw;
  min-height: 200px;
  min-width: 391px;
  font-family: verdana, sans-serif;
  justify-content: center;
}

article:hover .cast {
  display: none;
}

article:hover .cast2 {
  display: block;
}

.castcontainer {
  flex-wrap: wrap;
  min-width: 215px;
  width: 20%;
  height: 30%;
  overflow: hidden;
  padding: 5px;
}

#cast {
  border-radius: 50%;
  width: 100%;
}

.cast2 {
  display: none;
  text-align: center;
  background-color: #8b1a1a;
  border-radius: 10px;
  padding: 10px;
}

.cast:hover+.cast2 {
  display: block;
}

.cast {
  text-align: center;
  background-color: #8b1a1a;
  border-radius: 10px;
  padding: 10px;
}

p {
  background: white;
}
<article>

  <div id="one" class="castcontainer cast">
      <img src="https://pbs.twimg.com/profile_images/741703039355064320/ClVbjlG-.jpg" id="cast">
      <p><b>Ryan Reynalds</b></p>
  </div>

  <div id="two"class="castcontainer cast2">
      <img src="http://cdn03.cdn.justjared.com/wp-content/uploads/headlines/2017/08/joi-harris-rip.jpg" id="cast">
      <p><b>Wade Wilson</b></p>
  </div>

</article>

The unnecessary inner div with classes "cast" and "cast2" should be removed and the classes should be added to its parent instead.

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

Can multiple links be hrefed at the same time?

On my website, I have a particular anchor tag that links to another page: <li><a class="dropdown-item" href="/<%= term._id %>"><%= term.term %></a></li> Now, I am looking to add an additional href li ...

Displaying images with Javascript when they are clicked

How can I make a speech bubble appear when the image of the person is clicked? <div> <p style="float: left;"><img src="person.png" border="1px"></p> </div> <script> function bubblespeech() { document.getEl ...

Fill the FILE column with predetermined text

Struggling to repurpose code that creates FILE fields for database entries, my goal is to display them grayed out and disabled when viewing or editing existing data. Unfortunately, I'm having trouble populating the field with text using this snippet: ...

The DOMException occurred when attempting to run the 'querySelector' function on the 'Document' object

Currently, I am engaged in a project that was initiated with bootstrap version 4.3.1. I have a keen interest in both JavaScript and HTML coding. <a class="dropdown-item" href="{{ route('user.panel') }}"> User panel </a& ...

Shorten/Alternate coding:

Sometimes, creating the content section of a website can be time-consuming. I recently spent half a day crafting the index page of my website at . However, when attempting to add an additional image to the list of six already present, it turned into a leng ...

Aligning text at the center using bootstrap

I'm struggling with centering text within my website links, specifically in the navigation section "about me," "portfolio," "skills," and "contact." I've tried adding the text-center class and using the text-align function in the CSS, but nothing ...

When I use .fadeToggle, my div transitions smoothly between visible and hidden states

Looking to create a sleek navigation menu that showcases a colored square when hovered over? I'm currently experiencing an issue where the squares are not aligning correctly with the items being hovered. Switching the position to absolute would likely ...

Bootstrap Card breaking out from the confines of its parent container

Attempting to replicate Bootstrap's Cards example in my project, I noticed that the top margin of each Card extends beyond the parent element, a border div. If you need more information on how to utilize Card, refer to Bootstrap's documentation ...

Customers can access the order details after completing a Paypal payment

My hotspot system requires users to pay through PayPal for Internet access, after which they are redirected back to the hotspot and given a code. The issue I'm facing is that many users are forgetting or failing to write down their code. To address th ...

What is the best way to customize the look of the v-calendar component in Vue.js?

I've recently started a project that involves Vue.js, and I needed to create a datepicker for it. After some research, I opted to utilize the v-calendar package. The implementation of the component went smoothly and functioned as expected right out o ...

What's the name of the auto-triggered dropdown menu feature?

Visit Amazon's official website Description: Within the 'Shop by Department' section- A drop-down menu that remains visible without requiring you to hover over it. However, when the browser is in a non-full-screen mode, the menu disappears ...

What could be causing my Ionic button to not initialize in the expected state while using ngIf with a boolean property connected to an Ionic checkbox?

I'm currently in the process of setting up a list of ingredients with checkboxes and conditional buttons, but I'm facing some challenges with the default state. Ideally, I only want the button to be visible when the checkbox is unchecked so that ...

MUI component with a stylish gradient border

I'm struggling to locate the alternative for the border-image-source in CSS My objective is to create a button component with a gradient border ...

The opacity behavior of jQuery seems to behave strangely on IE10

Within my project, the left side of the content is contained within a div called ".container", and there's a preloader located in an element with the ID "#preloader." Across all major browsers, the functionality works smoothly as intended - when all ...

What causes the text-align property to function in varying ways across different tags?

Can you explain why the text-align property behaves differently on different tags? In two cases, I am trying to center my text. In the first case, I use a parent div tag with an anchor tag inside. When I need to center align, I apply the property to the p ...

Footer to display exclusively on the final printed page's bottom

I am facing an issue with my certificate template. It contains dynamic content in the header, body, and footer sections. When printing a single page, everything looks fine. However, when printing multiple pages, the footer does not display at the bottom of ...

Can AJAX Delete requests be executed without using jQuery?

Is it possible to use AJAX delete request without using jQuery? My JSON object at localhost:8000 appears like this: { "Students":[{"Name":"Kaushal","Roll_No":30,"Percentage":94.5}, {"Name":"Rohit","Roll_No":31,"Percentage":93.5}, {"Name":"Kumar ...

Is it possible to use Prettier in ESLint for formatting Markdown and CSS files?

At the moment, I have Prettier set up with ESLint using the configuration provided in my .eslintrc.json file: { "extends": ["react-app", "plugin:prettier/recommended"] } Everything works smoothly for linting and formatting JavaScript files. However, I ...

What is the best way to convert a repetitive string into a reusable function?

I am currently retrieving data from an API and I want to display it on my website in a more user-friendly manner. The challenge I'm facing is that the number of variables I need is constantly changing, along with their corresponding values. So, I&apos ...

Is the original image source revealed when clicked?

I've implemented an expand and collapse feature using jQuery/JavaScript. Clicking on the DIV causes the image inside to change state. However, clicking on the same DIV again doesn't return the image to its original state; it remains stuck in the ...