Accessing pseudo elements when their sibling is hovered can be achieved by using CSS selectors and combinators

I am struggling with the following html code:

<div class="content">
   <h2><a href="#">Hero</a></h2>
   <h2>Hero</h2>
    <div class ="tricky"></div>
 </div>

The "content" is a flex container and "tricky" is another flex container with pseudo elements before and after that I want to use for hover effects on the link. The CSS for tricky includes .tricky::before{...} and .tricky::after{..}, both set to square width with visibility: hidden.

I want these pseudo elements in "tricky" to change (ex: visibility, color, width, height) when the link is hovered over. I have tried using selectors like

.content a:hover ~ .tricky:before { ... }
but nothing seems to work.

Any assistance would be greatly appreciated. Thank you!


Here is my setup: I need to modify the styles of the "tricky" element's pseudo elements - ":before" and ":after" when the anchor tag is hovered over.

*{
  padding:0;
  margin:0;

}

.content{
  position:relative;
  top:200px;
  left:300px;
  display:flex;
  justify-content:center;
  align-items:center;
  flex-direction:column;
  width:150px;
  height:150px;
  background:transparent;
  overflow:hidden;
  border-radius:20px;

}


/* Other CSS here */

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <link href="test.css" rel="stylesheet"/>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>

    <div class="content">

      <h2><a id ="ida" href="#">Hero</a></h2>
      <h2>Hero</h2>

      <div class ="tricky">
        </div>

    </div>

</body>
</html>

Answer №1

It seems like your question could use some clarification. Is this what you're looking for? The ::after content will be displayed when any previous sibling h2 is hovered.

h2:hover~.tricky::after {
  content: "123";
}
<div class="content">
  <h2><a href="#">Hero</a></h2>
  <h2>Hero</h2>

  <div class="tricky"></div>
</div>

Answer №2

I utilized the onmouseover and onmouseleave event listeners to monitor hover and out events, dynamically adding/removing the visibleAfter class responsible for styling the content::after in the CSS

const link = document.getElementById('link');
const tricky = document.getElementById('tricky');

link.addEventListener('mouseover', ()=>{
  tricky.classList.add('visibleAfter')
})

link.addEventListener('mouseleave', ()=>{
  tricky.classList.remove('visibleAfter')
})
.tricky, .tricky::before, .tricky::after {
  width: 50px;
  aspect-ratio: 1;
  background: #777;
  position:relative;
}

.tricky::before, .tricky::after{
  content: '';
  display:block;
  position:absolute;
  display:block;
  visibility:hidden;
}

.tricky::before{
  top:.5em;
  left:.5em;
  background:red;
}

.tricky::after{
  top: 1em;
  left:1em;
  background:green;
}

.visibleAfter::after {
  visibility: unset;
}
<div class="content">
   <h2><a id='link' href="#">Hero</a></h2>
   <h2>Hero</h2>
    <div id='tricky' class="tricky"></div>
 </div>

Answer №3

Are you interested in this feature? When hovering over h2, the .tricky::after element will be displayed.

h2:hover~.tricky::after {
  content: "123";
}
<div class="content">
  <h2><a href="#">Hero</a></h2>
  <h2>Hero</h2>

  <div class="tricky"></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

Utilizing DOMStringMap data to populate CSS URLs

I am attempting to create a universal CSS modification for a webpage element that resembles: <button data-action="link" class="cardImageContainer coveredImage cardContent itemAction lazy lazy-image-fadein-fast" data-blurhash="lo ...

Discover the name of a color using its HEX code or RGB values

Is there a way to retrieve the color name from RBG or HEX code using JavaScript or JQuery? Take for instance: Color Name RGB black #000000 white #FFFFFF red #FF0000 green #008000 ...

Differences between using CSS properties and AngularJS filters:CSS properties

When working on an angularjs application, one task may be to convert a string to uppercase. There are 2 options available to achieve this, both yielding the same result. However, it is important to consider the scenarios in which one option may be preferre ...

Tracking User Visits in Codeigniter

Below is the code breakdown: (Step by step) Place counter.txt in APPPATH . 'logs/counter.txt' Set up counter_helper.php in APPPATH . 'helpers/counter_helper.php'; Autoload the newly created helper in APPPATH . 'config/autoload.p ...

Is it possible to globally modify the component reference <dropdown-component> name in Angular during runtime in a dynamic manner?

I am currently working on an application that utilizes a component called "dropdown-component" throughout its pages. However, due to specific business requirements, I have been tasked with replacing "dropdown-component" with "custom-dropdown-component". Un ...

Update each initial .not('class') using Jquery

My attempt at creating a live search with an effect in jQuery is proving to be challenging as I am struggling to capture the first word in the text. I have attempted to wrap each word in a span like this: <span class="word word-this" id="word-#" aria-h ...

Pictures do not adhere to the maximum width set on their parent elements

When the max-width CSS style is set on the body tag and an image within the body surpasses this maximum width, the image does not adhere to the set limit. Instead of resizing, it simply overflows. Why does this happen? So, what is the solution to this pro ...

Problem with Bootstrap 3 navbar on mobile devices - not tappable or responsive

After years of using Bootstrap, I've come across a new issue with my implementation of a Bootstrap 3 Nav. While testing on a desktop browser with device emulation, the nav collapses and functions properly. However, when tapping on the header on an ac ...

What could be causing Html.checkboxfor to fail in updating the boolean value of the model?

I am currently working on a project where customers can select multiple users to remove them from the database. The issue I'm facing is that the checkboxes are not changing or sending the isSelected variable, which tells the server which users should ...

The code inside the if statement is somehow executing even when the if statement is not true

Similar Question: Issue with jQuery function running at inappropriate times I've spent several hours trying to figure out why my function isn't working properly. I have a function inside an if ($window.width() < 1000) statement, but it se ...

Creating a user-friendly HTML form to convert multiple objects into JSON format

Edited content: $.fn.serializeObject = function() { var obj = {}; var arr = this.serializeArray(); $.each(arr, function() { var value = this.value || ''; if (/^\d+$/.test(value)) value = +value; if (obj[this.name] !== u ...

Best practices for organizing your Obelisk project include creating a specific folder for CSS files within

Trying to align two divs side by side using Obelisk, I referred to this post for guidance on how to do it: How to place div side by side. The solution required declaring classes in CSS. Utilizing the instructions from a tutorial (https://github.com/hansrol ...

Make sure all the table rows have the same height to completely fill the table regardless of the number of rows

Looking for a solution to create a table with a fixed height of 280px, whether it has 3 or 4 rows. Is there a method to adjust the row heights based on the number of rows in the table, so that they fill up the table's height? To clarify, when the ta ...

What could be causing the Material AngularJS (CSS) on my website to not function properly, unlike the demo that was

I am completely new to AngularJS and I want to incorporate the material design version of AngularJS for developing a form. I followed the beginner's guide and attempted to create something, but it didn't turn out like the one on the website. So, ...

What is the best way to align three tables columns in a single row?

I am facing an issue with aligning the columns of three different tables in one line. I have tried applying CSS styling, but the columns still appear misaligned. How can I ensure that the columns are properly arranged in a single line? Below are my HTML/C ...

Deactivate the form element when a user selects another button

I am facing an issue with two buttons that are linked to the same modal form. Here is the code snippet for the form: <form name="addUser" ng-submit="(addUser.$valid) ? add() : '';"> <div class="form-group has-feedback" ng-class="ad ...

A reliable approach for dynamically altering SVG graphics

It seems like IE10 does not support CSS transformations for SVGs, only attribute transformations. Here is an example: <svg><rect id="myrect" width="200" height="200"></rect></svg> setTimeout(function() { var r = document.getE ...

Creating a responsive design for my website that will adapt to any device, whether it be a phone

As a beginner in HTML and CSS, I am currently working on creating a website for my project. However, one of the requirements is that it should be responsive. I have encountered an issue where my yellow box appears to be getting smaller while the red and ...

A <mat-card> does not have a raised appearance because it lacks a border

Looking for help with my HTML code. Here's the setup: <mat-card> <mat-card-content> <code>{{file}}</code> </mat-card-content> </mat-card> But when the mat-card element is rendered, it's not ra ...

The popover seems to be failing to appear

I am struggling to get a popover to appear when clicking on an appended href element dynamically. Here are the code snippets I have tried: <div id="myPopoverContent"> ...stuff... </div> <div id="div3" style= "width:200px;height:200px;" ...