Can elements be styled differently using the CSS :hover selector even if the hovered element and affected element are not related?

Within this demonstration fiddle, hovering over the element a triggers a change in background color for the First paragraph to red.

  • Since a and #p1 are siblings,

  • By altering the selector between them, this method (changing the relationship between elements) will also apply if there is a parent-child connection (direct or nested) between the two elements.

    However, my question is:
    Is it possible to modify the background color of the Second paragraph when the mouse hovers over a?


Please Note: I understand that achieving this with JavaScript/jQuery is straightforward, but I am seeking a solution utilizing CSS.


Answer №1

CSS4 is set to bring in this feature. Unfortunately, it has not been finalized as a specification yet and no browser has implemented it. (Even when browsers do implement it, it will still take time for it to be widely used in everyday CSS).

Currently, your best bet is using Javascript. (Implementing this with jQuery is a piece of cake)

Answer №2

UPDATED: Still With Some Really Unique Tweaking!

Surprisingly, this piece of code functions in FF, Chrome, IE9 and 10 to achieve the desired outcome (check out the example). Yes, it's a bit of a "hack" as noted by Pumbaa80. However, I enjoy creating working hacks for challenges that may initially seem impossible. Admittedly, due to its unconventional nature, it may not work in all scenarios (especially more complex real-world situations).

What was Accomplished with the Update

  1. I believe I managed to fix a small bug in FF and Chrome where it would occasionally still display when hovering not directly over the <a> element.
  2. I discovered a workaround to make IE cooperate by inserting the body:before pseudo-element at z-index: -1 between the #p1 (z-index: -2) and the parent #out div. For this to function properly, the :before must have a set background-color and then be given a opacity: 0 (without this, it seems like it doesn't even exist for blocking purposes). It is important to note that there are at least two side effects (perhaps more) from this blocking technique: (a) Links within the #p1 won't be clickable, and (b) as mentioned in a comment by the OP, text within #p1 won't be selectable.

Below is the revised code primarily for the #sec hover effect. Additionally, I've included the combined code here for the original #p1 hover effect now triggered by #out:hover. Nonetheless, the code has been adjusted so only the <a> element inside #out activates the #out:hover:

body:before { /* used for blocking hover of #p1 triggering */
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: -1;
  background-color: cyan;
  opacity: 0;
}

#out {
  height: 0; /* no inherent height; fixed the occasional FF/Chrome hover bug */
}

#out:hover #p1, /* revised #p1 hover code */
#out:hover + div p { /* hover of #out triggers for #sec */
  background-color: red;
}

#out a { 
  float: left; /* ensures #out only triggers on anchor */
}

#p1 {
  float: left; /* clears the anchor */
  width: 100%; /* maintains the normal div width of 100% */
  position: relative; /* necessary for z-index */
  z-index: -2;  /* enables #out to trigger exclusively on anchor rather than #p1 hover */
}

#sec {
  clear: left; /* clears above floats to preserve layout */
}

The primary challenge with a pure CSS solution lies in elements that are "not in relationship." My aim with this endeavor (largely successful for major browsers...though uncertain about mobile browsers, etc.) is to leverage the parent element relationship in a manner that allows only the hover of the <a> (within that initial parent element) to trigger the :hover on the parent element itself, thereby enabling me to implement hover effects for all <p> elements.

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

Unable to alter the background color of the text box

I am currently struggling with my code where I am trying to overlay a text box on an image. Even after setting the background color to white, it still shows up as transparent. I have successfully done this in the past, but for some reason, it is not work ...

When hovering over it, an element within an li tag appears larger than its parent element

I'm currently working on a project for my school classes and running into an issue with CSS. I'm trying to enclose everything in boxes, but it's proving to be quite challenging. The main problem I'm facing is that when I hover over the ...

Need a jQuery function that updates the 'id' after clicking on a specific div? Here's how to do it!

I need help simplifying my current situation. Step 1: I want to increment the variable "count" by 1 every time a specific div is clicked. Step 2: After updating "count", I want to utilize it in another function. This function involves copying the value f ...

Styling elements using flexbox and various other divs

Looking to style a webpage with CSS to achieve this particular layout: https://i.sstatic.net/K3x0A.png The objective is to make the page responsive where the red and blue columns (left and right) have fixed widths. The center column should be collapsible ...

The contents of the multi-level navigation are automatically shown as the page loads

I've implemented a multilevel dropdown menu on my website using a plugin, and it works as expected. However, there's an issue where the contents of the dropdown menu display for a brief moment while the page is loading. I tried adjusting the posi ...

Scrolling through menus horizontally

I am faced with a challenge where I have Menu items from Menu1 to Menu10, but the limited space on the web page prevents me from displaying all the menu items at once. Currently, I can only show 4 menu items - Menu4 to Menu7. To access the remaining menu i ...

Having trouble installing node-sass through npm

Currently, I am attempting to install the SASS compiler node-sass in order to compile SCSS code into CSS code. Despite following an online tutorial and replicating the same steps, I keep encountering errors. npm init --yes : this will generate a json file ...

Utilize custom scrollbar design exclusively for Windows operating system

My goal is to customize the scrollbar style of a div specifically for Windows systems. While most mobile devices have invisible scrollbars that I want to keep hidden, the aesthetics of Mac OS scrollbars are acceptable to me. What I really need is to make t ...

Creating a custom button in a Material UI table using React

I am looking for a solution specific to customizing the MaterialTable Actions column by adding an icon for viewing details. Although I have reviewed the documentation and API, I have not found an efficient way to achieve this customization. My project inv ...

Issue with Flat-UI: Navigation bar is not collapsing correctly. Need help to resolve this problem

I am currently utilizing the most recent Twitter Bootstrap along with Flat UI. I have been trying to create a basic navbar that collapses when the screen size is reduced. How can I resolve this issue? This is how it currently appears: My navigation items ...

The CSS property col visibility:collapse is ineffective on the Chrome browser

I am attempting to conceal some columns in my HTML code by using MDN colgroup and col elements, and manipulating the styles of the columns. The <td> containing the text 'visible' is visible in all browsers (which is good), but the one with ...

Dynamic backdrop featuring engaging content

I am currently working on a WordPress website that features a dynamic background. While I have successfully implemented movement to the background, the content on the site remains static and unaffected by scrolling. The background does not move alongside t ...

Techniques for adding multiple elements in a grid system while ensuring responsiveness

Looking for guidance on designing a responsive screen using the Bootstrap-4 grid system for web development. Here's an image of what I'm aiming to create using rows and columns: View the grid picture here Code snippet: <div className="row"& ...

CSS specificity in Angular components utilizing the /deep/ selector to target a class outside of the component's scope

I have developed a button component for an Angular Component Library that I am currently working on. The button functions correctly, and its implementation is as follows: <sio-button [buttonLabel]="'Button Text'" [buttonFormat]="&apos ...

Tips for implementing a hover effect on the background color of a Bootstrap navbar

I am having trouble changing the background color of links and social media icons when hovering over them. I've tried multiple solutions found through searching, but none seem to work for me. Could someone please assist me with this issue? Thank you! ...

Obtaining data from an external ng-repeat element

My list contains items that are being repeated using ng-repeat details. I want to be able to hover over one of the li elements and have the background of a div called background (which is outside of the ng-repeat) change to the url of the corresponding d ...

Having trouble with implementing a lightbox form data onto an HTML page using the load() function? Let me help

In my project, I have set up a lightbox containing a form where user inputs are used to populate an HTML file loaded and appended to the main HTML page with the load() function. While I can successfully load the data onto the page, I am facing challenges i ...

The CSS border-radius feature shapes the corners without affecting the background

When I apply a 15px rounded border to a div, the border appears rounded on the outside but the corners where the border meets the background remain square. Here is an example: http://jsfiddle.net/BQT5F/1/ I tried using the background-clip property to add ...

Establish a primary background color for the React application, with the majority of the display content

I have been working on styling a React project where App.js mainly handles routing and navigation to its components: App.js import {useState, useEffect} from 'react'; import Models from './pages/Models'; import Loadingpage from ' ...

Arranging adjacent div blocks to maintain alignment in a stylish way

I am facing an issue with divs that are placed side by side inside a table. These divs have been styled to appear as colored blocks. Everything seems fine when the text within each div fits on two lines, however, if it overflows onto another line, the en ...