0

After extensive searching I have not found a answer. Everything I find has to do with child or siblings. I found using '+', '~', ' ', '>' have no effect other then on children or siblings.

Using the code in my Codepen(and below), I am trying to get the second image that is behind the first to move to the right when 'info' in the side menu is hovered over. How can this be done?

Basically, What I want is to have the side menu items effecting other elements in the html. These other elements are not siblings or children to the menu items.

html

<div class="container">
  <div class="side_nav">
    <ul>
      <li class="info">Info</li>
      <li class="stuff">Stuff</li>
      <li class="other">Other</li>
    </ul>
  </div>
  <div class="main_content">
    <img class="img1" src="https://images.freeimages.com/images/large-previews/389/mitze-1380778.jpg">
    <img class="img2" src="https://images.freeimages.com/images/large-previews/45f/garter-snake-1401165.jpg">
  </div>
</div>

CSS

.container {
  display: grid;
  grid-template-columns: 100px auto;
}

.side_nav{
  grid-column: 1;
  background-color: #aaa;
}

.main_content {
  grid-column: 2;
  background-color: #eee;
}
.img1 {
  position: relative;
  z-index: 5;
  width: 200px;
}
.img2 {
  position: absolute; 
  z-index: 3;
  width: 200px;
  left: 109px;
}

.info:hover ~ .img2 {
  left: 300px;
}
Codarth
  • 13
  • 2
  • Css doesnt let you do that. But you can always use javascript, jquery – Uğur Eren Oct 20 '18 at 20:14
  • you cannot go up, there is no parent selector – Temani Afif Oct 20 '18 at 20:28
  • @TemaniAfif I was not looking for the parent. However, I understand why this is the same question. To get to another element in the html I would have to get back to the common parent of the two elements then work my way down to the element I want. – Codarth Oct 20 '18 at 21:20
  • @TrueTiem Any suggestions on javascript code? I am still working on my javascript skills. – Codarth Oct 20 '18 at 21:22
  • exactly and to do this you need a *parent selector* which is the missing part in your logic and this part is impossible using CSS ... the duplicate you show you some workaround and JS code if you want – Temani Afif Oct 20 '18 at 21:34
  • @Codarth `
  • Info
  • ` would work. Sorry for the late response – Uğur Eren Oct 20 '18 at 22:31