How can the style of a div's :after pseudo-element be modified when hovering over its :before pseudo-element?

Here is some CSS code:

.myDiv:before{
    content:'';
    width:15px;
    height:15px;
    display:block;
}

.myDiv:after{
   ...
   ...
   display:none;
}

And here is the corresponding HTML code:

<div class='myDiv'></div>

I am wondering if there is a way to display the .myDiv:after pseudo-element when hovering over the :before element. I know I can use the hover selector as .myDiv:hover:before, but I'm not sure how to target the :after pseudo-element within that selector.

Answer №1

If you want to enhance the appearance of your content, consider incorporating a new style into the :after css class despite it not being the most recommended approach.

.myDiv:hover:after {
    display:block;
}

http://jsfiddle.net/9LX1m/3/

Answer №2

The best way to achieve this effect is by using a hover effect on the entire element as shown below:

.myContainer {
    height: 200px;
    background: red;
}
.myContainer:before {
    content: '';
    display: block;
    height: 20px;
    width: 100%;
    background: blue;
}
.myContainer:hover:after {
    content: '';
    display: block;
    height: 20px;
    width: 100%;
    background: blue;
}

http://jsfiddle.net/CodePen/A67b2/

Answer №3

If you are struggling to trigger the :hover::after while executing a :hover::before, there is a workaround using JavaScript. To maintain cleaner markup, consider implementing a loop within your code and adding placeholders dynamically behind the pseudo elements. This will allow you to interact with them instead of attempting to access non-existent pseudo elements within the DOM.

$("head").append("<style id='pseudoElementStyle'></style>");

var setPseudoElementDisplay = function (hover) {
    var content = hover ? ' after' : '';
    var cssRule = '.text::after { content: "' + content + '"; }';
    $('#pseudoElementStyle').text(cssRule);
};

$('.text').prepend('<span class="place-holder before">before </span>');
$('.text').append('<span class="place-holder after"> after</span>');

$('.place-holder').each(function () {
    var direction;
    var width = $(this).css('width');
    direction = $(this).hasClass('before') ? 'left' : 'right';
    $(this).css('margin-' + direction, '-' + width);
});

$('.before').mouseover(function () {
    setPseudoElementDisplay(true);
}).mouseout(function () {
    setPseudoElementDisplay(false);
});

http://jsfiddle.net/jasonjohnson115/2d96N/

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

Radio buttons have been concealed and are not visible

Tried the solutions recommended in a previous question about radio buttons not showing in Safari and Chrome, but unfortunately, it did not solve my problem. It seems like this issue is different from the one discussed in that question. The WordPress them ...

The Toggle Functionality necessitates a double-click action

I'm currently working on implementing a menu that appears when scrolling. The menu consists of two <li> elements and has toggle functionality. Everything is functioning properly, except for the fact that the toggle requires two taps to activate ...

What is the best way to stack div elements one after the other?

I am a complete beginner in HTML and I have a project for my web design class about a movie. http://jsfiddle.net/Lbo1ftu9/ <div id="footer"> <a href="D:/MOVIE REPORT/akira.htm" title="GO BACK?"><img src="D:/IMAGES/goback.gif"> My code ...

Issue: $injector:unpr Unrecognized Provider: itemslistProvider <-

I've spent several days debugging the code, but I can't seem to find a solution. I've gone through the AngularJS documentation and numerous Stack Overflow questions related to the error, yet I'm still unable to identify what's caus ...

Unable to retrieve data from Flask for AJAX request

When making an AJAX call to a Flask function to retrieve data using a token, the code looks like this: @app.route("/questgen/results", methods = ['POST', 'GET']) def fetch_results(): token = request.form.get('token&a ...

Tips for personalizing the FileField in wtforms to function as an image button

I'm attempting to display an image from my project's directory as an icon instead of the standard "choose file" button. Unfortunately, my attempts thus far have been unsuccessful. Not only is the image not loading, but the original button is only ...

What HTML element identifies the beginning of a particular section?

It has been suggested that Google frowns upon using identical content across multiple websites. Is there a method to mark or label a section of content with its "source"? For example, an attribute might look like this: <div original-content="http://s ...

Blending HTML template and WordPress PHP file

I am facing a challenge while attempting to implement a Wordpress template on one of the pages of my website, specifically at http://www.windowblindshadeshutters.com/blog/. The blog has been successfully created, however, I am having trouble preserving our ...

What is the best way to implement a new search input for my datatable while also enabling the searchHighlight feature?

I'm attempting to implement a search bar for my datatables. I need to hide the default search engine that comes with datatables, so I added a script that I found in a forum. However, when I try to run it in my code, it doesn't work and displays a ...

Removing a faded out div with Vanilla JavaScript

I am struggling with a JS transition issue. My goal is to have the div automatically removed once it reaches opacity 0. However, currently I need to move my mouse out of the div area for it to be removed. This is because of a mouseleave event listener that ...

Cells within a table are equivalent

I am attempting to ensure that all table cells match the size of the image, but for some reason, they are not aligning properly. Visit this link for reference. Here is a condensed version of the HTML code: <table> <tr> <td> ...

Setting the width of an SVG element to match the width of the <text> element inside it

Within this JSFiddle project - https://jsfiddle.net/xtxd6r8t/ - there is an <svg> element containing text inside a <text> tag. Upon inspection of the <svg> and delving into the <text> element, it's noticeable that the width of ...

The buttons mysteriously vanish when hovered over in Internet Explorer 11

I've encountered a strange issue with my website www.webfounded.com when viewed in Internet Explorer - all of my bootstrap buttons disappear on hover! Despite adding CSS classes for multiple browser compatibility and even attempting to remove the hove ...

Creating a drop-down menu that aligns perfectly under the bar in Material-UI: What you need to know

While working with Material-UI, I encountered a problem with my drop-down menu. Every time I click on it, it covers the bar instead of appearing below it (see image links below). https://i.stack.imgur.com/1Y8CL.jpg https://i.stack.imgur.com/emf87.jpg Is ...

Elements separated by empty space on a Complex Grid, appearing distant despite the available space

Struggling with creating a complex layout using minimal relative and absolute positioning. Here's the issue I'm facing: All my elements are aligned side by side with only one problem: the border image needs to bleed into the row below. How can t ...

Update the query string in the src attribute of the image tag

I'm facing a challenge with our products loading at specific sizes and quality that I need to address. I am looking to update a portion of the img src code: &wid=128&hei=144&qlt=80 to: &wid=256&hei=288&qlt=100 I have attemp ...

Get the number of elements that match your XPath expression

Currently engaged in an XML and XSLT project with a challenging task; the query requires me to count the number of open ports with a specific port number using the following path: liste_hotes --) hote --) liste_ports --) port num_p ...

Unable to fetch the data from HTML input field using autocomplete feature of jQuery UI

I am facing an issue with my html autocomplete textbox. Whenever I try to enter an address like "New York" and click on the submit button, it does not retrieve the input value and returns no value. Test.aspx <!--Address--> <div class="form-g ...

What location is most ideal for incorporating JavaScript/Ajax within a document?

Sorry for the seemingly simple question, but I would appreciate some clarification from the experts. When it comes to the three options of placing JavaScript - head, $(document).ready, or body, which would be the optimal location for ajax that heavily rel ...

The negative z-index is causing issues with my ability to select classes using jQuery

By setting a z-index of -3 for several divs in my background, I thought they wouldn't affect the formatting of elements in the foreground. But now I'm facing an issue where I can't click on those background divs when trying to target them wi ...