Need assistance with CSS: The div:hover~div selector isn't functioning properly for a div that appears later in

Check out this example page

Apologies for the unclear title, let me clarify the issue here.

The webpage consists of two identical dividers. When I hover over the left divider, I want the opacity to change and simultaneously affect the second divider as well. Currently, the code does this but with the right divider, the hover effect only applies to itself and not the left divider.

I am open to exploring alternative approaches to solve this problem.

HTML:

<div class="wrap">
    <div class="bgimage" id="left"> 
        <div class="text">
            <h1>Portfolio</h1>
        </div>
    </div>

    <div class="bgimage" id="right"> 
        <div class="text">
            <h1>Photography</h1>
        </div>
    </div>
</div>

CSS:

.wrap {
    width:100%;
    height:100vh;
    background-color:#000;
}

.text {
    height:55px;
    opacity:0.9;
    text-align:center;
    vertical-align:middle;
    position:absolute; 
    top:0; 
    bottom:0;
    margin:auto; width:50%;
}

.bgimage{
    width:50%;
    height:100vh;
    opacity: 0.6;
    -webkit-transition: opacity 0.8s ease-in-out;
    -moz-transition: opacity 0.8s ease-in-out;
    -o-transition: opacity 0.8s ease-in-out;
    transition: opacity 0.8s ease-in-out;
    background-size:cover;
    background-repeat:no-repeat;
    background-position:center;
}

#left {
    float:left;
    background-image:url(left.jpg);
}

#right {
    float: right;
    background-image:url(right.jpg);
}

#left:hover~div#right, #right:hover~div#left {
    opacity: 0.3;
}

#left:hover, #right:hover {
    opacity: 1;
}

Answer №1

If you are trying to target the first div and not the second one, using CSS alone won't work as CSS doesn't allow selecting a preceding element. You can achieve this with jQuery instead. Check out the code below:

$(document).ready(function () {
    $('.bgimage').hover(function () {
        $('.bgimage').removeClass('hovered').addClass('unhovered');
        $(this).addClass('hovered').removeClass('unhovered');
    });
});

Take a look at this FIDDLE for your example with some minor CSS changes. Hopefully, this will be helpful.

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

What is the process for designing a dropdown box that showcases text details?

I am in the process of building an FAQ page and I am looking to incorporate a dropdown menu that lists all the frequently asked questions. When a user clicks on a question, I want the corresponding answer to be displayed without redirecting to a new page. ...

Unable to add a border to the thumbnail containing text

Is there a way to achieve a nice border on my thumbnail section below? I attempted using a container but it resulted in the text overflowing on md or sm screen widths. <div class="col-lg-8 col-lg-offset-2 col-md-8 col-md-offset-2 col-sm-8 col-sm-offset ...

Rendering an image on the HTML5 canvas

I really want my image to be in the perfect spot. I already have this code set up, but how can I adjust the positioning of my image? Whether it's a little to the left or more to the right, I'm talking about the exact coordinates. Where do I need ...

One potential solution to reorganizing the layout is to utilize two rows instead of just one

My current form layout is not very user-friendly, as it appears to be too long for one row. I have fields for amount, note, date, and an option list. I would like to arrange the form so that amount and note are displayed in one row, followed by date and o ...

Discover a method to prohibit the use of numbers

Can someone provide me with a Regex pattern to restrict the input in the Name field to only include a mix of text and numbers? I do not want names that consist solely of numbers like 12345678, 3241238215, or 4323438. Rather, it should contain both text and ...

What is the best way to toggle the active class on a ul list?

After clicking, the active class remains on the original "li" instead of changing as it should. I've tried modifying the code but haven't been able to find a solution. Can someone please review what I might have missed? It seems like there's ...

Following my ajax submission, the functionality of my bootstrap drop-down menu seems to have been compromised

I'm having an issue with my login page. After implementing Ajax code for the reset password feature, the dropdown menu on the login page doesn't work properly when wrong details are entered and the page reloads. I've tried using the $(' ...

Determining the pageY value of a div element with overflow-y styling

Currently, I have implemented a script that tracks the mouse's position upon hover. My goal is to integrate this script within a div that has overflow-y: scroll The script currently utilizes pageY which identifies the position relative to the windo ...

Ensure portrait orientation images have a restricted width to maintain their aspect ratio

On a responsive site, I have set up flexslider to showcase both landscape and portrait orientation images. The smoothHeight: true option is enabled to adjust the height according to the images. However, at the largest media query where the flexslider cont ...

Adaptable layout - featuring 2 cards side by side for smaller screens

I am currently designing a webpage that showcases a row of cards, each featuring an image and a title. At the moment, I am utilizing the Bootstrap grid system to display 4 cards per row on larger screens (col-md-2), but my goal is to modify this layout t ...

How to show table cell value in Angular 4 using condition-based logic

I am a beginner in Angular development. Here is the HTML code I am working with: <tr *ngFor="let item of calendarTableSelected; let idx = index"> <span *ngIf="idx === 0"> <td style="width:15%;" *ngFor="let name of item.results" ...

What is the best way to create a highlighting effect with "onmouseover" in CSS?

protected void MasterCust_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowState == DataControlRowState.Alternate) { e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#eefef0';"); ...

Is there a way to reduce the height of the border-left property in CSS?

In creating a simple address panel, I have divided it into three sections: address, phone number, and email. Each section includes an icon and a paragraph next to it. To align these sections side by side, I used the vertical-align: top and display: table p ...

Despite having unique ids, two file input forms are displayed in the same div in the image preview

Running into a minor issue once again. It may not be the most eloquent query, but I'm genuinely stuck and in need of some assistance. I am attempting to showcase image previews of selected files in a file input form. I have a jQuery script that reads ...

methods for adjusting the transparency of the background image while keeping the foreground image visible

I am using Twitter Bootstrap to create a carousel on my website. Currently, I have set the foreground image as the current image and the background image as the next one. However, I want to adjust the opacity of just the background image. Can someone pleas ...

Searching for a table row that corresponds to the content of a cell using Python and Selenium

If you need to retrieve the row element from an HTML table where the text in a specific cell matches the string 'Mathematik & Informatik'. Here is the structure of the HTML: <table class="views-table cols-4"> <thead> ...

Ways to automatically display the Nebular Accordion using NgFor

Struggling with the latest Nebular version in Angular 7, I'm encountering a problem when using the nebular accordion component. Problem: The default behavior should have only one accordion expanded at a time, but setting expanded = true expands all of ...

When the mouse reaches the far left portion of the screen, an action will be triggered

Is there a way to trigger an action when the mouse reaches the far left or right of the screen using HTML, CSS, jQuery, or any other method? I'm specifically trying to show my navbar when the mouse reaches the left end of the screen. Any assistance wo ...

Jquery and CSS behaving in an unexpected manner

Here's a piece of code that I need some help with. The issue occurs when clicking on a box to expand it and display more information. Most of the time, it works fine, but occasionally if an image is not cached, clicking again to minimize the box cause ...

Combining two functions into a single button: image changing

I'm currently facing an issue with assigning two actions to a single button. When I click on the image, it changes as expected. However, the action only triggers when clicking on the image itself and not on the button. Any thoughts on how I can modify ...