Manipulating one element's behavior on hover by means of another using CSS

I am currently experimenting with creating a unique layout on a webpage that features two columns. The goal is to have both columns shift towards the center if you hover over the left side, emphasizing the left column. Similarly, if you hover over the right side, both columns should slide to the right, highlighting the right column.

I have successfully achieved this effect for the left column, but I'm encountering some challenges with applying it to the right column. It appears that the first div can influence the position of the second div, but not vice versa. As a result, when I hover over the right side, only the right column moves towards the center while the left column remains stationary instead of sliding off to the side.

<style>
    @import url("https://use.typekit.net/poz1xqv.css");
    @import url('https://fonts.googleapis.com/css2?family=Syne:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="384f5f504c780c08081616000808">[email protected]</a>&display=swap');
    @import url('https://fonts.googleapis.com/css2?family=Work+Sans:ital,wght@0,100..900;1,100..900&display=swap')
    h2, h3 {
      .syne-font
    }
    p {
      .work-sans-light
    }
    .syne-font {
      font-family: "Syne", sans-serif;
      font-optical-sizing: auto;
      font-weight: 500;
      font-style: normal;
    }
    .work-sans-light {
      font-family: "Work Sans", sans-serif;
      font-optical-sizing: auto;
      font-weight: 300;
      font-style: normal;
    }
    .work-sans-medium {
      font-family: "Work Sans", sans-serif;
      font-optical-sizing: auto;
      font-weight: 400;
      font-style: normal;
    }
    .cta-button {
      background-color: #ffffff;
      color: #000000;
      transition: 1s ease;
    }
    .cta-button:hover {
      background-color: #000000;
      color: #ffffff;
      transition: 1s ease;
    }
    .hidden-text {
      display: flex;
      flex-direction: column;
      position: relative;
      opacity: 0;
      overflow: hidden;
      transition: 1s ease;
    }
    .hidden-text:hover {
      opacity: 100;
    }
    .feature-image:hover ~ .hidden-text,
    .hidden-text:hover {
      opacity: 100;
    }
    .sliding-section {
      /* margin-left: -120px;
      margin-right: 120px; */
      position: relative;
      margin-left: -10%;
      margin-right: 10%;
      width: 120%;
      display: flex;
      align-items: center;
      overflow: hidden;
      transform: translateX(0);
      transition: ease 1s;
    }
    .residential {
      position: relative;
      transform: translateX(0);
      transition: ease 1s;
    }
    .residential:hover {
      transform: translateX(50px);
      transition: ease 1s;
    }
    .residential:hover ~ .commercial{
      transform: translateX(50px);
      transition: ease 1s;
    }
    .commercial {
      position: relative;
      transform: translateX(0);
      transition: ease 1s;
    }
    .commercial:hover {
      transform: translateX(-50px);
      transition: ease 1s;
    }
    .commercial:hover ~ .residential{
      transform: translateX(-50px);
      transition: ease 1s;
    }
</style>
<script src="https://cdn.tailwindcss.com"></script>
<script>
    tailwind.config = {
      theme: {
    extend: {
      fontFamily: {
            'TradeGothicNext': ['"Trade Gothic Next"', 'sans-serif']
          },
          fontFamily: {
            'Syne': ['"Syne"', 'sans-serif']
          },
          fontFamily: {
            'Work Sans': ['"Work Sans"', 'sans-serif']
          },
      colors: {
        greybg: '#CECCC4',
      }
          transitionDuration: {
            '300': '300ms',
          }
    }
      }
    }
  </script>


<div class="no-margin sliding-section w-screen z-3">
  <div class="w-full grid grid-cols-2 gap-10">
    <div class="residential feature-image">
      <img class="feature-image object-cover overflow-x-visible" src="https://danverstg.wpengine.com/wp-content/uploads/2024/04/img4.jpg" alt="Residential" />
      <h3 class="feature-image text-right syne-font pt-4">Residential</h3>
      <div class="grid grid-cols-2">
        <div></div>
        <div class="hid-text2 hidden-text">
          <p class="hid-text2 text-right work-sans-light pt-4">See our wide variety of cabinet types and styles and how they can accommodate all of your outdoor kitchen needs</p>
        </div>
      </div>
    </div>
    <div class="commercial">
      <img class="object-cover overflow-x-visible" src="https://danverstg.wpengine.com/wp-content/uploads/2024/04/img5.jpg" alt="Commercial" />
      <h3 class="text-left syne-font pt-4">Hospitality & Multi-Family</h3>
      <div class="grid grid-cols-2">
        <div class="hid-text2 hidden-text"><p class="hid-text2 text-left work-sans-light pt-4">See our wide variety of cabinet types and styles and how they can accommodate all of your outdoor kitchen needs</p></div>
        <div></div>
      </div>
    </div>
  </div>
</div>

This current project showcases an interesting scenario where one child element within a parent container has the ability to affect another child element's positioning, whereas the reverse is not true. How can the relationship between these divs or elements be better understood in such cases of interaction?

Thank you for any insights and assistance!

Answer №1

If you want to determine if the .commercial element is being hovered over and then adjust the CSS of the .residential element, you can use the :has() selector.

.residential:has(~.commercial:hover) {}

console.clear();
.sliding-section {
  position: relative;
  margin-left: -10%;
  margin-right: 10%;
  width: 120%;
  display: flex;
  align-items: center;
  overflow: hidden;
  transform: translateX(0);
  transition: ease 1s;
}

.residential {
  position: relative;
  transform: translateX(0);
  transition: ease 1s;
}

.residential:hover {
  transform: translateX(50px);
  transition: ease 1s;
}

.residential:hover~.commercial {
  transform: translateX(50px);
  transition: ease 1s;
}

.commercial {
  position: relative;
  transform: translateX(0);
  transition: ease 1s;
}

.commercial:hover {
  transform: translateX(-50px);
  transition: ease 1s;
}

.residential:has(~.commercial:hover) {
  transform: translateX(-50px);
  transition: ease 1s;
}
<script src="https://cdn.tailwindcss.com"></script>

<div class="no-margin sliding-section w-screen">
  <div class="w-full grid grid-cols-2 gap-10">
    <div class="residential">
      <img class="object-cover overflow-x-visible" src="image.jpg" alt="Residential" />
      <h3 class=" text-right syne-font pt-4">Residential</h3>
      <div class="grid grid-cols-2">
        <div></div>
        <div class="">
          <p class="text-right work-sans-light pt-4">lorem ipsum</p>
        </div>
      </div>
    </div>
    <div class="commercial">
      <img class="object-cover overflow-x-visible" src="image.jpg" alt="Commercial" />
      <h3 class="text-left syne-font pt-4">Hospitality & Multi-Family</h3>
      <div class="grid grid-cols-2">
        <div class="">
          <p class="text-left work-sans-light pt-4">lorem ipsum</p>
        </div>
        <div></div>
      </div>
    </div>
  </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

Django: The page you are looking for cannot be found, but the path is

I've recently delved into learning Django, and I've hit a roadblock trying to understand why my services.html page isn't rendering. I followed the same steps I used for my home page, which renders perfectly fine. However, I keep encountering ...

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 ...

Sending a base64 image of a canvas to a servlet

I have a JavaScript function that uploads HTML5 canvas base64 image data to a servlet. Here is the code: function saveDataURL(a) { var postData = "canvasData="+a; var ajax = new XMLHttpRequest(); ajax.open("POST",'uploadPhoto.cgi',tr ...

The toggle function for the hamburger icon is currently malfunctioning

I am facing an issue with the hamburger icon on my website. The toggle functionality associated with it is not working properly. Even though I have a class named change-1 that should be toggled upon clicking the icon, it is not happening. There are no erro ...

How can I adjust iframe content to fit properly on a mobile device screen?

I am facing a challenge where I need to embed an iframe into an external website over which I have no control. The only thing I can specify is the size of the iframe on my own website. My goal is to ensure that the content within the iframe adjusts to fit ...

Guide on receiving a date input in a datetime format upon clicking a submit button

Currently, I am developing a daily report feature for my system. One of the requirements is to have an input field where users can select a date using a datepicker. When the user clicks the submit button, the system needs to retrieve the date from the data ...

Is it possible to export a table to a CSV file using jQuery/JavaScript code, no matter how large the table is? Check out the jsfiddle link for

Check out this link to view the table and code: http://jsfiddle.net/KPEGU/485/ I am experiencing an issue with exporting the table to Excel CSV. When I click the export button, a blank CSV file is generated without any data. // The following JavaScript ...

Misaligned Div Content

Check out my jsfiddle here The text inside the <div> is shifting upwards Could someone explain why this is occurring? And suggest a solution? I would truly appreciate any assistance. Thank you! ...

When attempting to execute a function when a hidden div is not displayed, JQuery encounters an

I'm currently attempting to modify the text color of the h2 title with the id ticketSearchTitle when the ticketSearch div is not visible. However, the code I have been using to achieve this seems to cause issues (such as the absence of a pointer icon ...

Send information without refreshing the page

Even though there are countless questions similar to this one, I am still struggling to make this work correctly. Any advice on what steps I should take to fix this would be greatly appreciated, as my understanding of jQuery is about 80%. <form action= ...

Utilizing Django's For Loop to Transfer Dropdown Selections to a Modal

I have a dropdown menu with different choices. When a user selects an option, I want a modal to appear with a message specific to the choice made (e.g. "You selected Choice A"). I attempted to use data-target="#myModal{{choice}}" and id="my ...

How can I incorporate a custom color into a preset navbar on an HTML webpage?

<div class="navbar-header"> <button aria-controls="navbar" aria-expanded="false" data-target="#navbar" data-toggle="collapse" style="color: blue;" class="navbar-toggle collapsed" type="button"> <i class="fa fa-reo ...

Designing a JSON array

How can I format my JSON list into Material Cards? Here is my JSON/JavaScript code: $(document).ready(function(){ var url="getjson.php"; $.getJSON(url,function(data){ console.log(data); $.each(data.bananas, function(i,post){ ...

Modify the href value by matching it with the parent div attribute

I am working on an HTML project <div class="1" style="" title="NeedthisText TextIDontneed"> <div class="2> <div class="3"> <a target="_blank" href="/NeedToChange/DispForm.aspx?ID=1"></a> </div> </div> &l ...

What is the best way to transfer information using PHP?

I'm struggling to pass the player id to the delete and update page, but it's not working. Below is my table body: <tr> <td><img src="<?php echo $readRow['Photo']; ?>" width='64px'></t ...

Tips for creating a clickable A href link in the menu bar that triggers an accordion to open in the body when clicked - html

Is there a way to open the first accordion when clicking on the "open 1st accordion" link, and do the same for the second link? The accordions themselves work perfectly fine, I just need a way to trigger them from outside their scope by clicking on links i ...

Display HTML using JavaScript/jQuery

I am trying to figure out how to print a document by passing custom HTML code. Below is the code I have tried, but unfortunately it's not working: function Clickheretoprint() { var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes, ...

Extra horizontal spacing in CSS

Struggling to eliminate the excess horizontal space on my HTML/CSS page. Any thoughts on what might be causing this? Appreciate any help! ...

What is the most effective way to add HTML from a dynamically loaded document using AJAX?

I am attempting to attach the information from a .html file to the body of my main webpage. Essentially, I am striving to create a reusable section of html that can be loaded into any page with a basic JavaScript function. Below is the content of my navig ...

Issue with Bootstrap navbar not highlighting section items when scrolled to

I have recently developed a webpage using html5, css3, and bootstrap. However, I am facing an issue with the navbar menu not highlighting the items that are pointing to specific sections on the page. I would like the navbar menu to automatically highlight ...