What could be the reason for my selector ( a:hover ~ a ) not functioning correctly?

My goal is to create a hover effect where hovering over a link will reduce the opacity of other links while keeping the color of the hovered link unchanged.

I attempted using a:hover ~ a, but it did not work as expected. I set up a new jsfiddle with a basic <a href=""> and that worked fine, so I'm confused why it's not working here.

http://codepen.io/jzhang172/pen/OMRqpK

    @import url("http://fonts.googleapis.com/css?family=Ubuntu+Condensed");

    *{
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
    }

    html{
     background: url('img/congruent_pentagon.png');
    }

    body{

    margin:0px;
    }
    #wrapper{
        height: auto;
        width: 100%;
        max-width: 300px;
        margin-left: auto;
        margin-right: auto;
        background-color: #fff;
        margin-top: 2em;
        margin-bottom: 2em;
        height: 700px;
        background: transparent;
        font: 25px/24px normal 'Ubuntu Condensed', sans-serif;
    }

    a{
        text-decoration: none;
    }

    .social{
        display: inline-block;
        position: relative;
        width: 100%;
        padding: 16px;
        margin-bottom: 16px;
        margin-right: 16px;
        background-color: rgba(231, 231, 231, 0.6);
        box-shadow: 8px 8px 0 0px rgba(0, 0, 0, 0.25);
    }

    nav{
     padding-top: 8px;
     padding-left: 8px;
     padding-right: 8px;
 }

 nav ul{
    margin: 0;
    padding: 0;
    list-style: none;
}

nav ul li{
    display: inline;
}

nav ul li a:hover{
    opacity: 0.7;
}

nav ul li a.email{
 border-left: 50px solid #c9182c;
 color: #c9182c;
}

nav ul li a.twitter{
    border-left: 50px solid #00a0d1;
    color: #00a0d1;
}

nav ul li a.facebook{
    border-left: 50px solid #365998;
    color: #365998;
}

nav ul li a.github{
    border-left: 50px solid #4183c4;
    color: #4183c4;
}

nav ul li a.google{
    border-left: 50px solid #db4a39;
    color: #db4a39;
}

nav ul li a.instagram{
    border-left: 50px solid #634d40;
    color: #634d40;
}

nav ul li a.tumblr{
 border-left: 50px solid #34526f;
 color: #34526f;
}

nav ul li a.scriptogram{
 border-left: 50px solid #0088cc;
 color: #0088cc;
}
nav ul li a.linkedin{
    border-left: 50px solid #0e76a8;
    color: #0e76a8;
}

.container{
    width:100%;
    
    text-align:center;
    padding-top:20px;
     padding-bottom: 20px;

    
}
.container h1{
    font-family: 'EB Garamond', serif;
    font-size:55px;
    width:500px;
    margin:0 auto;
    border-top:1px solid black;
    border-bottom:1px solid black;
    font-weight:0px;

}
.container span{
    width:100px;
    height:300px;
}
h2{
    font-family: 'Lora', serif;
    text-align:center;
    font-size:25px;f
    font-weight:normal;
    color:#0B437D;

}


a.tumblr:hover ~ a.social{
    opacity:.1;
}
<div id="wrapper">
  <nav>
    <ul>
      <li><a class="social tumblr" href="#" target="_blank"><img src="img/svg/home.svg">Home</a></li>
      <li><a class="social google" href="#" target="_blank"><img src="img/svg/prod.svg">Products</a></li>
      <li><a class="social google" href="#" target="_blank"><img src="img/svg/about.svg">About</a></li>
      <li><a class="social facebook" href="#" target="_blank"><img src="img/svg/fb.svg">Facebook</a></li>
      <li><a class="social twitter" href="#" target="_blank"><img src="img/svg/twitter1.svg">Twitter</a></li>
      <li><a class="social instagram" href="#" target="_blank"><img src="img/svg/insta.svg">Instagram</a></li>
      
    </ul>
  </nav>
</div>

Answer №1

It has been noted in previous responses that using the general sibling combinator (~) will not produce the desired effect.

Nevertheless, I believe achieving this effect is still possible with pure CSS. Consider giving this a try:

ul:hover li:not(:hover) { opacity: .1; }

Updated Demo


Explanation

The selector li:not(:hover) targets all list items not being hovered over. However, using this alone would result in all list items having opacity: .1 as default.

To prevent this, we start the selector with ul:hover, forming a descendant selector that specifies: do not apply li:not(:hover) unless the ul element is first hovered over.

Answer №2

The general sibling operator (~) is designed to match elements that are siblings of each other, but in the case of your links, they are not direct siblings.

Instead, the list items that contain these links can be considered siblings, making the links themselves more like cousins.

Unfortunately, CSS does not have a parent combinator to directly describe the relationship between these links.

One workaround would be to use something like li:hover ~ li > a to target the links based on their position relative to each other.

Answer №3

I attempted using a:hover ~ a, but it's not producing the desired result. I set up a new jsfiddle with a basic example and it works perfectly fine there, so I'm confused why it's not working here.

The selector ~ for general sibling combinator selects elements that have the same parent element.

8.3.2. General sibling combinator

The general sibling combinator consists of the "tilde" (U+007E, ~) character that separates two sequences of simple selectors. The elements represented by the two sequences share the same parent in the document tree, and the element represented by the first sequence precedes (not necessarily immediately) the element represented by the second one.

In the code snippet:

a.tumblr:hover ~ a.social {
    opacity:.1;
}

The a.social elements do not have the same parent li as the a.tumbler element.

Answer №4

It looks like you're attempting to achieve a similar effect as shown below:

a:hover, li>a.social{
opacity: .1;
}

The goal is to have all the "a elements" initially visible, and when one is hovered over, it causes the others to become transparent.

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

Ways to turn off the touchable opacity功能

Is there a way to temporarily disable a touchable opacity for a minute after it is pressed? let enable = true; <TouchableOpacity style={styles.btnSolid} onPress={() => { resetUserPswd(auth, Email, navigation); enable = false ...

Transform the string into a file format, followed by encoding it into base64

Is there a way to take the string const content="<div>How do I <b>convert </b> this string to file?</div>"; and convert it into an HTML file, then ultimately encode it as a base64 string? The method Buffer.from(content).toString(&ap ...

Exploring innovative designs for asynchronous JavaScript programming

Imagine you have an Express app and you need to retrieve data from a database to display on the frontend. There's a function in your code that looks like this (using node-mysql for handling database queries) exports.getData = function() { ...

The functionality of Ajax is not compatible with Internet Explorer

I am currently managing a project on an e-commerce website that involves allowing visitors to rate different products using a star rating script. Everything is functioning correctly, but I need to ensure that each product can only be rated once per visito ...

Contrast between employing element selector for concealment and activation tasks

I can't figure out why $('#mdiv input')[1].hide(); isn't working while $('#mdiv input')[1].click(); works perfectly fine. Firstly, I'm curious to understand why. Secondly, how can I get it to work without knowing the id ...

Creating a Boolean in Reactjs based on a successful axios POST request - is it possible?

Currently, my dispatch is triggered whenever there is any input in the Login fields, even if the credentials are incorrect. What I want is to only trigger the dispatch once the user has entered the correct username and password. Here's the code snippe ...

Place two divs within a parent div, with one div aligned at the top and the other at the bottom

Here is the code snippet I am working with: <div id="container"> <div id="1"> div 1</div> <div id="2"> div 2</div> </div> I am looking to rearrange the order so that div 1 is positioned at the top and div 2 is ...

Stack the text on top of each other beside an image

Here is some HTML code that I am working with: <div class="row"> <div col-md-9> <div> <img class="img-valign" src="an image"> <span class="text1" Style="font-size: 8 ...

Create a div element containing a captivating background image that automatically scales to fit any browser window

Currently, I am facing a challenge with scaling background images within a shared div (framework) so that they can resize along with the browser. I have searched for solutions involving Javascript, but none seem to address my specific case. Any assistance ...

Struggling to locate the origin of this mysterious border

After thoroughly inspecting and manipulating CSS, I am still unable to determine the source of this mysterious border... Mysterious Border Image - The Origin Unveiled Could someone take a look and provide some insights? It seems like the border is encom ...

Unable to engage with any items on the display when the Material UI (MUI) Select Menu is active

In my web application, I am utilizing Material UI's Select component. Upon using it, I encountered a restriction where the user is unable to interact with anything else on the screen while the Select menu is open. This includes the inability to click ...

A React child error has occurred in Next.js due to invalid objects being used

Within my project, I have integrated the latest version of next.js and encountered an issue where objects are not valid as a React.js child. https://i.stack.imgur.com/MCO7z.png The problem arises after importing the Head component from Next.js and implem ...

Issue with jQuery datepicker not triggering onChangeMonthYear event

Recently, I've been working on creating an app using jQuery date picker. To see my progress so far, feel free to check out this fiddle: http://jsfiddle.net/Lf6sD/2/. In the options, there's a mention of an onChangeMonthYear event that should trig ...

Errors in Animation in Bootstrap Carousel Implementation - slides fail to smoothly transition on and off the screen

It appears that when the slideshow transitions to the next slide or image, it loads partway across the screen instead of smoothly displacing the previous slide. I have attempted the following troubleshooting steps: Eliminating any special classes applied ...

Encountering a problem with my Solidity map(int,string) function when attempting to use JavaScript to pass values back to HTML

I'm struggling to update my labels using a JavaScript function call to my Solidity smart contract. My goal is to create a map with integer and string values in Solidity, then display these values in HTML upon clicking a button. But for some reason, th ...

Increase the size of the logo in the navbar without causing any distortion to the other elements

I am currently facing an issue with my logo placement in the header section of my website. The header consists of a background image with a navbar and logo embedded within it. HTML <div class="header"> <div class="container"> < ...

React has been reported to display a Minified React error even in its development mode

Currently, I am utilizing browserify and babel for transpiling and bundling my script. However, a challenge arises when React 16 is incorporated as it presents the following error message: Uncaught Error: Minified React error #200; for more details, kin ...

The Vue EventBus does not support passing object attributes

Looking for assistance with integrating two Vue single page components. The 'Searchbar' component features a dialog box where users input data. This data is required in the 'ResultList' component for further processing. To achieve this ...

Discovering the screen dimensions or viewport using Javascript in Bootstrap 4

Is there a method to identify the view port/screen size being utilized by Bootstrap 4 using Javascript? I've been struggling to find a dependable way to determine the current view port after a user resizes the browser. I attempted to use the Bootstra ...

Tips for assigning focus properties inside VueJS components

Within my component child Input: <template> <div class="basic-input-outer" :style="styles"> <p class="paragraph-small">{{ title }}</p> <input ref="name" :type="type" cla ...