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

Moving smoothly while retaining absolute positioning

I have a question about a particular scenario. I have a div element that is styled like a ball and I want it to transition diagonally across the page to the bottom right corner instead of moving suddenly. However, this is not happening as expected. Can you ...

Transform your current website layout from a fixed 960-grid system with 12 columns into a fluid and

As a newcomer to web development, I have successfully created my portfolio website. I initially set the body width to 1600px because that matched the size of my banner background in Photoshop. I'm using a 960gs 12-column grid system, but when I view t ...

Update to the viewport meta tag in iOS 7

Anyone experiencing problems with the viewport tag after updating to iOS7? I've noticed a white margin on the right side of some sites. Adjusting the initial scale to 0.1 fixed it for iPhone but made it tiny on iPad 3, which is expected due to the low ...

Is it possible to add padding to an HTML element without affecting its overall dimensions?

Is it possible to apply padding to a div without increasing its size? <div id="someDiv"> someContent </div> #someDiv{ padding: 1em; } One potential solution is to add another nested div within #someDiv and apply margin to that, rathe ...

Creating a personalized layout for your WordPress posts with custom card designs

As a new developer diving into the world of WordPress, I am aiming to create a sleek and minimalistic grid/card layout for my website. Struggling to achieve this, I seek guidance on how to replicate the design seen in the following link: desired result H ...

Troubleshooting Flexbox: Content within a Flex Item is not displaying properly within a scrollable container

I recently embarked on converting one of my websites to flexbox and encountered an issue with vertical scrolling within a flex container. It appears that Firefox displays it correctly, while Chrome and Safari do not. You can check out the code here. . ...

Is there a way to make a button on a single div only affect that specific div

I have a PHP query that echoes a div for each row in the table. I want the div to slide up and then, when the user clicks the "read more" button, the div slides down. However, since it is echoed in a loop, all the divs have the same IDs and classes. I wo ...

Revise for embedded frame contents

Is it possible to modify the HTML content of an iframe within a webpage? I currently have this code snippet: <iframe src="sample.html"></iframe> I am looking for a way to edit the contents of sample.html without directly altering the HTML co ...

html inserting line break using label

I am attempting to dynamically set text to a label using jQuery. However, I am having trouble getting the <br> or \n to create new lines. Instead, all the text displays on the same line and wraps around. If you want to see my code in action, ch ...

What are the reasons for not utilizing JSS to load Roboto font in material-ui library?

Why does Material-UI rely on the "typeface-roboto" CSS module to load the Roboto font, even though they typically use JSS for styling components? Does this mix of JSS and CSS contradict their usual approach? ...

"Despite using the same CSS and HTML code, the fonts appear distinct from each

Feeling lost here, not understanding what's going on. I made changes in Elementor on my first PC and later noticed that the font looked a bit different. I tried to revert the changes but had no luck. I also work on my second PC where the browser showe ...

Tips for verifying that input is provided in a text field when the checkbox is marked

Can someone help me with validating a form where the user must enter data in a text field if they check a checkbox? I have JavaScript code for checkbox validation, but need assistance with text field validation. Thank you! $(document).ready(function () ...

Passing string values to an onClick event listener in Javascript is similar to how it is done in an onclick event listener in HTML

render() { return ( <div> <div onClick={() => "FSR.launchFeedback('ax9sgJjdSncZWoES6pew6wMIyCgSXpC')" } /> </div> ); } This piece of code initially appear ...

What is the method for inserting a new <p> tag whenever a user sends a new message in ReactJS?

Whenever I press the Enter key, the content is updated in the same tag. First I sent Hello World! The second time I tried to send Hello as a new message, it was updated within the same tag. I have shared code snippets related to the messaging function ...

Double Row Navbar Struggles in Bootstrap 4

I have been experimenting with creating two bootstrap navbars. The first one features the website's domain name in the center, accompanied by a dropdown menu to navigate to other sections. Each of these sections has its own subbar containing specific ...

Creating PDFs using Puppeteer in the presence of `link` tags

On my website, students have the ability to create their own notes using a RichText editor where the content can be quite complex. I want to provide them with an option to download these notes as a PDF file. To achieve this, I am planning to use the "puppe ...

The functionality for determining whether the value isset as 'Yes' or 'No' is not functioning properly in my PHP form

I'm currently working on creating a combined 'Order Calculator / Order Form' using one php form. The calculator part of the form is functioning perfectly and accurately calculates the total for all 5 order options both on the live page and i ...

Utilizing jQuery to target a select element's two specific children

I am trying to target the parent element by matching two specific children elements. Here is my code: $('span:contains("11:00am"), span.name:contains("Tom")').parents("a").css("background-color","rgb(255, 255, 255)"); <script src="https://c ...

Using Jquery's prependTo method will add a display:block property to

Can't find a solution to this issue. This is the code snippet in question: var new_img = '<img id="' + drag_id + '" rel="' + drop_id + '" class="' + gallery_link + ' drop_out" src="' + drag_src + '" /& ...

Angular js encountered an unexpected error: [$injector:modulerr] ngRoute

https://i.sstatic.net/PATMA.png In my Angular code, I encountered the error "angular is not defined". This code was written to test the routing concept in AngularJS. var app=angular.module('myApp',['ngRoute']) .config(function ($routeP ...