Arranging an image to appear directly underneath another image upon hovering the mouse over it

I want image two to appear below image one when the mouse hovers over image one.

<ul>
   <li>
    <img id="img1" src="imageone.png">
    <br>
    <img id="img2" src="imagetwo.png">  
  </li>
</ul>

Any assistance would be greatly appreciated!

Currently, I've tried using this CSS but it doesn't seem to be working as expected:

#img2 {
    display: none;
    position: absolute;
}
#img1:hover + #img2,
#img2:hover {

 display:block;
}

Answer №1

The selector + chooses siblings that come directly after. In your case, the images are not direct siblings because they are separated by a <br> element.

You should use the ~ combinator instead:

#img1:hover ~ #img2

Answer №2

You are on the right track.

Start by eliminating the <br> tag because it interferes with the adjacent sibling selector.

The hover effect functions properly when hovering over the initial image.

If you add display: block to the img, there is no need for the <br> tag.

In some cases, the tilde symbol (~) selector might be suitable, especially if your layout includes other images (like a photo gallery).

ul {
  list-style: none;
}
#img2 {
  display: none;
}
#img1:hover + #img2 {
  display: block;
}
<ul>
   <li>
    <img id="img1" src="http://placehold.it/150x50">
    <img id="img2" src="http://placehold.it/150x50">  
  </li>
</ul>

Answer №3

Instead of using the + sign, opt for the tilde symbol due to the presence of a br tag in between.

#img2 {
    display: none;
    position: absolute;
}
#img1:hover ~ #img2,
#img2:hover {

 display:block;
}
<ul>
   <li>
    <img id="img1" src="imageone.png">
    <br>
    <img id="img2" src="imagetwo.png">  
  </li>
</ul>

Answer №4

If you're looking for a solution similar to this:

.container{
  position: relative;
}
.wrapper{
  position: fixed;
  width: 100px;
  height:100px;
  background-color: #efefef;
}
.image{
  width: 100px;
  height:100px;
  background-color: #ffefef;
}
#image2{
  display: none;
}
.wrapper:hover #image2{
  display: block;
}
<ul>
   <li class="container">
     <div class="wrapper">
       <img class="image" id="image1" src="imageone.png">
       <br>
       <img class="image" id="image2" src="imagetwo.png">  
    </div>
  </li>
</ul>

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

Using jQuery to create a Select All Checkbox that toggles a specific class

I have come across similar examples in the past, but I have been unable to make it work as intended. The examples I found only use standard checkboxes. I attempted to customize the checkbox using a sprite sheet by applying a class, but I am struggling to a ...

Is there a way to search for text and highlight it within an HTML string using Ionic

Welcome everyone! I am currently utilizing Ionic to load an article from a local HTML string. <ion-content padding> <p [innerHTML]="url"></p> </ion-content> The 'url' variable contains the local HTML string. My goal ...

I am encountering an issue while running npm run webpack in production mode where the CSS file is not being generated separately in the dist folder as expected. The file is not appearing as it should

Here is the code snippet from my webpack configuration file: const currentTask = process.env.nmp_lifecycle_event const path = require("path") const MiniCssExtractPlugin = require('mini-css-extract-plugin') const config = { entry: './ ...

How do I retrieve the value of a class that is repeated multiple times?

In the HTML code I am working with, there are values structured like this: <div class="item" onClick="getcoordinates()"> <div class="coordinate"> 0.1, 0.3 </div> </div> <div class="item" onClick="getcoordinates() ...

Arranging div elements into columns side by side with Flexbox

I've been working on the frontend of my chat web app and recently discovered flexbox. I'm struggling to center two elements with a blue background below the "CHAT" heading, aligned with two green elements like a black square. My challenge lies in ...

What is the best method for connecting my HTML to jQuery code?

Apologies for my lack of experience in coding, but I will try to keep this brief. To put it simply, when I insert my jQuery code directly into the HTML file below the content, it works perfectly - the element I want to animate 'hides' and then & ...

Loading elements of a webpage in a consecutive order

I am currently working on implementing a content slider within my Django application. The specific slider that I am using can be found at this link. One challenge that I am facing is the need to load close to one hundred 'contentDiv' pages, and I ...

Discover the ultimate guide to creating an interactive website using solely JavaScript, without relying on any server-side

I'm facing a challenge: I have a desire to create a website that is mainly static but also includes some dynamic components, such as a small news blog. Unfortunately, my web server only supports static files and operates as a public dropbox directory. ...

Discover the chosen image displayed within an ng-repeat showcased in a separate container

I am facing an issue with a table that uses ng-repeat to display data. One of the columns contains photos. When I edit a specific entry in the table, I want the corresponding photo to be shown. How can I achieve this? The code for my table looks like this ...

css side by side with immovable element

Seeking assistance with CSS. I am looking to create a layout as follows: I want a center-fixed menu with a width of 960px - this part is straightforward. Alongside the menu, I aim to have two divs: one spanning from the left edge of the screen to the cl ...

Tips for creating a responsive carousel slider for images

No matter how much I've tried, I can't seem to find a solution on how to make my image responsive along with the caption. Below is my HTML code: <section id="banner"> <div class="banner-bg"> <div class="banner-bg-item ...

Unfortunately, Safari and iOS do not support the video functionality

I am encountering an issue with my HTML5 sample video not loading in Safari 11 on OSX, but working perfectly fine in Chrome and Firefox. Additionally, the video is not functioning on iOS at all (neither in Safari nor in Chrome). Below is the HTML code: & ...

What is the best way to retrieve the root binding node from a viewmodel in order to apply jQuery.blockUI when performing an AJAX post request?

Within my code, I have a designated DIV element that serves as the root node for applying knockout bindings like so: ko.applyBindings(viewModel, document.getElementById('myContainerDiv')); In all of my viewmodel types, there is a generic post m ...

Improved explanation of DOM elements in raw JavaScript

Is there a more efficient way to dynamically create this DOM block: <tr> <td>text</td> <td><input type="checkbox"></td> </tr> I have a function that adds a nested tr element to my tbody: function inse ...

Different methods to retrieve content within a div that is located on a separate, included page

It's a bit tricky to come up with a title for this topic, but essentially, I'm trying to figure out how to get content from an included page into a specific div using HTML and PHP. Let me break it down: In the header.php file, we have the follo ...

Although the Flexbox is configured with 0 gap and margin, there remains a slight space between rows

I am currently attempting to utilize a flexbox in order to display several 600x300 images, however, I am encountering an unexpected small gap between the rows despite specifying a 0 gap and margin. Here is a screenshot for reference: .gallery { display: ...

Tips for seamlessly incorporating advertisements into a mixed application

I am having trouble adding banner ads to my hybrid application developed with Telerik. Despite my extensive research, I have been unable to find a suitable solution. Is there any html5 or javascript banner advertising service/API that is compatible with ...

Unlocking the Mysterious Realm of HTML

I recently stumbled upon a mysterious combination of HTML attributes: <div vanisheffect="true" blinkcolor="red" fadeopacity="0.8"> Have you ever encountered something like this? And more importantly, does it have any impact on specific browsers? Is ...

The Cyrillic text displays uniquely across three different web browsers

On our company's website, I implemented a search function. You can input the minimum and maximum cc of the glass you're looking for, and it will filter the database to display relevant results. The issue I'm facing is that when viewing the r ...

Create a dynamic animation using Angular to smoothly move a div element across the

I currently have a div with the following content: <div ng-style="{'left': PageMap.ColumnWrap.OverviewPanelLeft + 'px'}"></div> Whenever I press the right key, an event is triggered to change the PageMap.ColumnWrap.Overvie ...