Display a different element upon CSS hover

I am attempting to use CSS to display div id='b' when I hover over div id='a', but I am struggling because div 'a' is nested within another div that does not contain div 'b'.

<div id='content'>
     <div id='a'>
         Hover me
     </div>
</div>
<div id='b'>
    Show me
</div>

Answer №1

Let's demonstrate how to display a hidden label div on hover:

<style>
#label {
    display: none;
}

#hoverMe:hover~#label{
    display: block;
}

</style>

Answer №2

Yes, you can achieve this functionality by using the code below:

<div href="#" id='x'>
  Hover over me
</div>
    
<div id='y'>
  Show me
</div>

Along with the following CSS:

#x {
  display: block;
}
    
#x:hover + #y {
  display:block;
}
    
#y {
  display:none;
}

Now when hovering on element #x, element #y will be displayed.

Answer №3

If you want to make selections using an axe, here are two methods:

1. Immediate Parent axe Selector (<)

#a:hover < #content + #b

This particular axe style rule will target the element with ID #b. It is the immediate sibling of the element with ID #content, which is the direct parent of the element with ID #a that has a :hover state.

div {
display: inline-block;
margin: 30px;
font-weight: bold;
}

#content {
width: 160px;
height: 160px;
background-color: rgb(255, 0, 0);
}

#a, #b {
width: 100px;
height: 100px;
line-height: 100px;
text-align: center;
}

#a {
color: rgb(255, 0, 0);
background-color: rgb(255, 255, 0);
cursor: pointer;
}

#b {
display: none;
color: rgb(255, 255, 255);
background-color: rgb(0, 0, 255);
}

#a:hover < #content + #b {
display: inline-block;
}
<div id="content">
<div id="a">Hover me</div>
</div>

<div id="b">Show me</div>

<script src="https://rouninmedia.github.io/axe/axe.js"></script>


2. Remote Element axe Selector (\)

#a:hover \ #b

This axe selector rule will choose the element with ID #b, as long as it is in the same document as the element with ID #a that is in a :hover state.

div {
display: inline-block;
margin: 30px;
font-weight: bold;
}

#content {
width: 160px;
height: 160px;
background-color: rgb(255, 0, 0);
}

#a, #b {
width: 100px;
height: 100px;
line-height: 100px;
text-align: center;
}

#a {
color: rgb(255, 0, 0);
background-color: rgb(255, 255, 0);
cursor: pointer;
}

#b {
display: none;
color: rgb(255, 255, 255);
background-color: rgb(0, 0, 255);
}

#a:hover \ #b {
display: inline-block;
}
<div id="content">
<div id="a">Hover me</div>
</div>

<div id="b">Show me</div>

<script src="https://rouninmedia.github.io/axe/axe.js"></script>

Answer №4

Utilizing the adjacent sibling combinator, denoted as +, which specifically targets the immediate next sibling element.
This method allows for the utilization of classes to trigger independent hover effects:

<div class='a'>
  Hover over me 1
</div>
<div class='b'>
  Display me 1
</div>


<div class='a'>
  Hover over me 2
</div>
<div class='b'>
  Display me 2
</div>
.b {
  display: none;
}

.a:hover + .b {
  display: block;
}

Answer №5

Using position:absolute can lead to issues with the methods mentioned above, so it's important to also consider how to handle hover events on absolute elements.

<div class='content'>
  Hover me 1
</div>
<div class='hover'>
  Show me 1
</div>
.hover {
  position: absolute;
  visibility: hidden;
}
.content:hover + .hover {
  visibility: visible !important;
}
.hover:hover {
  visibility: visible !important;
}

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

Nested foreach loops interacting with one another

I am attempting to execute multiple nested foreach loops and stop at 12 iterations, however the current code I have is not functioning as expected. While it displays the desired output, there seems to be an issue where only the first image in each director ...

What is the best way to ensure that bootstrap cards are displayed next to each other when they are dynamically created by Django?

https://i.sstatic.net/KMrO1.pngI have encountered an issue with my Django blog. When I add a new blog post, it appears below the previous posts instead of alongside them, causing a vertical layout rather than a horizontal one. {% extends 'portfolio/b ...

Modify HTML text using conditional CSS without JavaScript

Apologies for asking such a beginner question, but I've searched high and low for a similar post, to no avail. Here's my query: I have a paragraph text in my HTML code that I want to automatically replace with new text when I click a specific B ...

iOS does not support Css3 Transition

My Transition Code works on PC and android devices, but it does not work on iOS devices. I am only using HTML and CSS. /***** BOX 3 *****/ #box3 { height:240px; width:198px; border:1px solid #dedede; background-color:#fcfcfc; position:relative; overflow:h ...

Limiting the number of characters in PHP/MySQL

Here is a glimpse into the scenario I am currently facing: The content, including the heading, text, and image, is all dynamically generated based on user input. Users have the option to include an image and choose the text for both the heading and main c ...

"Enhance Your Website with a WordPress Plugin: The Sticky News Footer

Currently seeking a plugin that can smoothly scroll text, preferably from posts, located at the end of the page in a sticky footer format. I am going for a news-style appearance with clickable links included. In the past, I utilized the Ditty News Ticker ...

Error: JavaScript Framework (JSF) application experiencing issues with jQuery functionality

I am a beginner when it comes to jQuery. In my Netbeans project, I have the jquery-1.7.2.js file where all HTML files are stored. Currently, I am working on creating a JSF 2.0 project. Below is the code snippet I am using: <?xml version='1.0&apo ...

Why is align working fine while justify-content is experiencing issues?

When I try to center items vertically using the justify-content property with the flex-column applied, the align-items works correctly but the content doesn't respond on the Y axis. I have the align-items commented out because I only want to center on ...

Customize the width and padding of your data tables

I have several tables with different padding and width settings. Previously, I had set these inline without any issues. However, now that I am using data tables, the inline styles are not working. If you need more information about Datatables, check out t ...

Listening for select elements that have remained unchanged

My current situation involves a select box with predetermined options. One of these options is preselected when the page loads: <select name="select" class="js-choice-select"> <option value="option-1">Option 1</option> <option ...

How can I use CSS to customize the appearance of the elements within an iframe?

<iframe src="#link"> #document <!doctype html> <html> ... </html> </iframe> Is there a way to customize the styling of elements within the #document using CSS only, without needing to use Jquery? ...

The issue of height and width always being zero in Chrome when using Classic ASP with JavaScript

Within my markup, I have an image field that I am setting the source for using JavaScript. I am in need of its height and width, so I utilized the image.height() and image.width() methods. Despite working properly in Internet Explorer, these methods do not ...

javascript how to retrieve the custom attribute value of an HTML style

I am dealing with an HTML element that has some inline styles as well as a custom CSS property. I am able to access every style attribute except for my custom style property. Here is the code I am working with: <img id="myimg" class="ImgClass" style=" ...

Validating Angular Forms within a NgFor loop

Consider the scenario where there is a form implemented as shown below: <form #form="ngForm"> <div *ngFor="let action of member_plan.actions"> <div class="container"> <h4 class="text-sm-center black mb-3">{{ ...

Steps for executing ASP code pulled from the database

In my ASP project, I have a single page where clicking on different links retrieves corresponding HTML from the database and loads it. Some links are always present on the page (static HTML), while other divs display dynamic content fetched from the DB. Wi ...

What is the best way to target the first child element in this scenario?

Is there a way to target only the p tag with id="this" using .root p:first-child selector? Here is the code snippet: Link to CodePen .root p:first-child { background-color: green; } p { margin: 0; } .container { display ...

Modify code on click using JavaScript

Here is some code I found for a custom style switcher: I am thinking about integrating it into my bootstrap dropdown button. The current code for the style switcher is as follows: <form id="switchform"> <input type="radio" name="choice" value= ...

rearranging the positioning of links in the HTML navbar

Is there a way to prevent other links in the navbar from moving up slightly when hovering over one link and creating a line (border bottom)? ` .n24{ margin-top: 2px; padding: 14px 14px; } .n25{ margin-top: 2px; padding: 14px 14px; } . ...

Using HTML in radiobutton choiceNames within R Shiny applications

I am currently struggling to showcase Radiobutton choiceNames that were dynamically created from a text input. These choiceNames consist of a Greek letter (beta) and two subscripts - one numeric and the other, an alphabet 'j'. My attempt involve ...

Encountering yet another frustrating issue with z-index not functioning properly in versions of IE above 7, despite extensive research yielding no solution

I have scoured numerous resources and read countless articles on how to resolve z-index issues in IE 7, 8, and 9. However, none of the suggested solutions seem to work for my particular situation. The issue at hand is that I have interactive content posit ...