When you hover over one div, the opacity of another div shifts

Need some help with CSS on my WordPress site. I want to change the opacity of a div column when hovering over another one. Specifically, I would like the cat box to become semi-transparent when hovering over the dog box.

Here is the CSS code I am using:

.dog_box {
    height: 200px;
        width: 200px;
    background-color: #000000;
    float:left;
}

.cat_box {
    height: 200px;
    width: 200px;
    background-color: red;
    float:left;
}

.dog_box:hover .cat_box{
    opacity:0.5;
}

You can see the HTML structure below:

<div class="dog_box"></div><div class="cat_box"></div>

I seem to be encountering an issue with this setup and would appreciate any insights or suggestions. Here is the link to my site for reference: Parapet Care Site

Answer №1

If you're looking for the CSS selector to target the previous sibling element, here is a solution that has worked for some users (although it may not be supported in Firefox):

.dog_box:hover + .cat_box {
    opacity: 0.7;
}


.dog_box:has(~ .cat_box:hover){
    opacity: 0.7;
}


.dog_box {
    height: 200px;
        width: 200px;
    background-color: #000000;
    float:left;
}

.cat_box {
    height: 200px;
    width: 200px;
    background-color: red;
    float:left;
}
<div class="dog_box"></div><div class="cat_box"></div>

Answer №2

Like I mentioned in my previous comment, your selector is specifically targeting a descendant. Instead, you should consider using a sibling selector such as ~ (general) or + (adjacent)

.dog_box {
    height: 200px;
        width: 200px;
    background-color: #000000;
    float:left;
}

.cat_box {
    height: 200px;
    width: 200px;
    background-color: red;
    float:left;
}

.dog_box:hover + .cat_box{
    opacity:0.5;
}
<div class="dog_box"></div><div class="cat_box"></div>

Answer №3

The current implementation modifies the transparency of every cat_box component within the container called dog_box.

In this scenario, you can utilize the subsequent-sibling combinator (~) to adjust the styles of all cat_box elements that are on the same level as the dog_box, but show up after it in the document.

.dog_box:hover ~.cat_box{
    opacity: .5;
}

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

Resizing images next to lengthy text on mobile screens

My text and image keep behaving oddly when viewed in a smaller window or on mobile. The image resizes unpredictably based on the length of the text, as shown in my preview. I attempted to use flex wrap to contain the longer text, but it only pushed the ima ...

Mastering Bootstrap 4 flexbox for optimal content filling: A comprehensive guide

I am currently working on an angular app integrated with bootstrap 4. The challenge I am facing is related to the layout design. I have a fixed navbar at the top and a content area below it. Within the content area, I have a div that includes header and fo ...

Content width exceeds body width

Having an issue with body width that seems strange. Check out this fiddle link to see for yourself. <div class="topbar" style="width:100%; background:#000; color:#fff"> <div class="container" style="width:970px; margin:0 auto;">Lorem ipsum ...

Bootstrap 4 - The Mystery of 'col-sm-auto' Taking Precedence Over 'col-md-4'

I am currently learning Bootstrap and have encountered a challenge that I can't seem to figure out. Let's take a look at a snippet of the code: <button className=" col-md-4 col-sm-auto btn btn-info ...

Unlock the secret: Using Javascript and Protractor to uncover the elusive "hidden" style attribute

My website has a search feature that displays a warning message when invalid data, such as special characters, is used in the search. Upon loading the page, the CSS initially loads like this: <div class="searchError" id="isearchError" style="display: ...

Triggering Leaflet's Mouseout function during a MouseOver event

I am working on a leaflet map project where I am dynamically adding markers. One of my goals is to have the marker's popup appear not only when clicked, but also when the user hovers over it. This is the code snippet I have been using: function mak ...

Changing color based on AngularJS condition using CSS

My HTML code looks like this: <div> <i class="glyphicon glyphicon-envelope" style="margin-top: -50px; cursor:pointer; color: #1F45FC" ng-click="createComment(set_id)"> </i> Route <center> ...

Prevent the float:left property from causing my div to overlap with another div

Is there a way to style "a" so that it can take up space and float to the left simultaneously? <div style="background:#111"> <div id="a" style="float:left; height:30px"></div> </div> ...

Changing the position from fixed to static

It's strange, but for some reason when I attempt to apply position:fixed using jQuery: $('#footer').css('cssText', 'position:fixed !important;'); to my footer, the result on the page is different: <div id="footer" ...

Tips for defining a relative path in the base URL with AngularJS

Encountering an issue with AngularJS routes related to file paths. The folder structure is as follows: js -> Angular -> css -> Bootstrap -> index.html Routes work fine when hosted on a server like IIS. However, copying and pasting the direct ...

Arrange elements in bootstrap to stack on top and bottom for mobile devices, and have a sidebar for

My layout consists of three elements: top bar, content, and bottom bar. I'm using Twitter Bootstrap 4. For mobile view, I want the elements to stack in this order: top bar content bottom bar For desktop view, I want them to align like this: conte ...

Hidden beneath the content div lies the secretive footer div

I am experiencing an issue with a webpage that is working in IE7 but not in IE8; Here is the HTML code: <div class="content"> <div class="inner_content"> <div class="column"> <div class="widget"> ...

Why isn't my CSS styling showing up on the page?

I am struggling to create a thin border using my code. Despite all my efforts, the border simply won't render on the page. To ensure maximum specificity, I have temporarily placed it within a page element: <div style="max-width: 100%; position: re ...

setTimeout in CSS animation causing issues

I recently created an animation using CSS3. The source code is quite simple. It involves displaying text and then making it disappear after 4 seconds. Below you can find the current source code: const intro1 = document.getElementsByClassName('intro ...

Guide to Repairing Uncaught TypeError: players.setAttribute is not a recognized method?

I'm a beginner and encountered an error saying Uncaught TypeError: players.setAttribute is not a function when submitting an action. How can I solve this issue? Please share your insights. ''' //selecting all necessary elements const s ...

What is the best way to evenly divide divs vertically on a Bootstrap website?

I am currently working on a responsive page design and have come across this useful resource: http://www.bootply.com/2sfiCehqac My main objective is to ensure that when the screen size is medium or large: 1) div_left and div_right (orange and blue) adjus ...

Safari browser is experiencing issues with the custom file upload script

My custom upload script allows users to easily select images for upload by clicking or dragging them into the designated box. A preview of the chosen image should appear, and this functionality works smoothly in Firefox and Chrome. However, I've encou ...

Horizontal alignment of navigation bar items using <ul>

I'm trying to center the bar within the navigation bar vertically, but when I use align-items: center for the bar, it only centers the sign-in button, leaving it slightly above the middle position. nav a{ color:white; } nav{ justify-content: sp ...

Find the final element that does not include a particular class

Looking for the best way to identify the last item in a list without a specific class? Learn how to write code that can check for the absence of a class name. $('ul li:last') $('ul li:not(.special)') ...

Obtain the in-flow position of a DOM element

alert("Incorrect (red): " + document.getElementById("target").getBoundingClientRect().top); alert("Proper (blue): " + document.getElementById("wrapper").getBoundingClientRect().top); #target { transform: translate(20px, -20px) rotateZ(20deg); backgroun ...