Ways to trigger effects on one html element from another using css

I am looking to create a connection between two sets of elements, where one set consists of text links in a navigation bar and the other set contains images with references to the same links. These images have animation effects triggered by hovering over them.

The desired behavior is to activate the hover effects on the images when hovering over the links in the navigation bar. Is it possible to achieve this without using jQuery?

Below is the styling for the animated elements:

.view-first img { 
    transition: all 0.2s linear;
}
.view-first .mask {
    opacity: 0;
    background-color:rgba(116,89,47,0.8);
    transition: all 0.4s ease-in-out;
}
.view-first h2 {
    transform: translateY(-100px);
    opacity: 0;
    transition: all 0.2s ease-in-out;
}
.view-first p { 
    transform: translateY(100px);
    opacity: 0;
    transition: all 0.2s linear;
}
.view-first a.info{
    opacity: 0;
    transition: all 0.2s ease-in-out;
}

/* Hover effects */
.view-first:hover img { 
    transform: scale(1.1);
} 
.view-first:hover .mask { 
    opacity: 1;
}
.view-first:hover h2,
.view-first:hover p,
.view-first:hover a.info {
    opacity: 1;
    transform: translateY(0px);
}
.view-first:hover p {
    transition-delay: 0.1s;
}
.view-first:hover a.info {
    transition-delay: 0.2s;
}

This section shows the markup for the navigation elements:

<nav><ul>
    <li><a href="http://users.dsic.upv.es/~margomez/">DSIC</a></li>
    <li><a href="rna/tutorial/RNA_marcos.html">RNA</a></li>
    <li><a href="ares/index.php">De Ludo Bellico</a></li>
</ul></nav>

Lastly, here is the markup for one of the images with animation effects:

<div class="view view-first">  
    <img src="images/animage.png" />  
    <div class="mask"/>  
    <div class="content"> 
        <h2>Name</h2>  
        <p>Description</p>  
        <a href="ares/index.php" class="info">Take me there!</a>  
    </div>
</div>

The goal is to trigger the animation in the associated "view" element when hovering over the navigation elements. While this behavior can typically be achieved with jQuery or JavaScript, I am curious if it is possible to accomplish the same effect using only HTML and CSS. If so, how would you go about implementing this?

To provide context, the layout of my page involves activating animations in the images below when hovering over the corresponding elements in the navigation bar highlighted in blue.

Answer №1

Definitively, the answer is no. It is impossible to trigger the animation for element y when hovering over element x. However, there are ways to achieve similar effects using pure CSS in specific scenarios:

1) If your target element is a parent of .view-first

.y:hover .view-first { ... }

2) If your target element is adjacent to .view-first

.y:hover + .view-first { ... }

3) If your target element is a general sibling of .view-first

.y:hover ~ .view-first { ... }

Interestingly, upcoming versions of CSS may introduce a "subject selector", allowing you to navigate upwards in the DOM with an exclamation mark (!) in your selectors. This addition could be beneficial in such cases, although it would still require a connection between the elements.

Edit

Mr. Alien suggests utilizing the :target pseudo-class if clicking on the element is permitted. For instance, by setting up your HTML as:

<a href="#spin">Start Spin</a>
<div id="spin" class="view-first"></div>

You can leverage the target property to initiate the spin effect in your CSS:

.view-first:target { ... }

However, this approach may have limited applicability in your case.


Edit 2019 - The subject selector (!) has been substituted with the :has() selector, yet it remains unsupported across browsers even after 5 years.

Answer №2

Give this a go:

.view-first {    opacity: 0;
/*add more styles here*/}
.myDiv:hover ~ .view-first{
    -webkit-animation: boxanimation 1.5s;
    opacity: 1;
    -webkit-transition: all 0s;
    -webkit-animation-fill-mode: initial;
/*add more styles here*/}

This code is optimized for Chrome and Safari, but you can customize it by adding -moz for Firefox and -o for Opera compatibility.

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

The ion-datetime-button within an ion-item does not have full height

I am experiencing an issue where the ion-items containing ion-select and ion-datetime-button elements do not have consistent heights. Even though they are all nested within ion-cols with identical properties. The ion-items with ion-datetime-buttons appear ...

Troubleshooting issue with image dimensions in Angular within CKEditor content

One issue I am facing is with CKEditor, where images inserted into Rich Text Fields have their height and width attributes set in a CSS style tag. For example: <img alt="" src="https://something.cloudfront.net/something.jpg" style="height:402px; ...

"Triangle Emblazoned Speech Bubble on a Clear, See-Through Back

I'm currently working on creating a semi-transparent speech bubble with a border. I'm using a ::before element for a larger triangle shape and a ::after element for a smaller triangle shape that sits on top to create the illusion of a border. How ...

Having Trouble with $.ajax Function in my Program

After spending three days experimenting with various methods, I'm still unable to successfully use the Javascript ajax command to send form values to a php script. Despite no errors being displayed and the scripts running smoothly, nothing is getting ...

Tips for creating a div element that closes on the second click:

On the PC version, I have three blocks that open and close perfectly when clicked. However, on the mobile version, when I click on one block, it opens but does not close unless I click on another block. Additionally, if I click again on the same block th ...

The attribute 'select' is not found within the type '{}'

I am a beginner in Angular and currently working on an ecommerce project where I need to filter products based on the query parameters present in the URL. Let's take an example URL: http://localhost:4200/?category=Fruits This URL should filter all ...

What steps can I take to restore my text_field to its original dimensions?

I recently added the token-input-mac.css file to my project and noticed that my text_area has become extremely small, resembling a text input field. How can I maintain the new css styling while only adjusting the height of the text_field? Here is the toke ...

"Create a dynamic HTML newsletter utilizing tables similar to the responsive design principles

Looking to create a mobile-responsive newsletter using my style.css <table cellpadding="0" cellspacing="0" border="0" align="center" width="600" bgcolor="#fff"> <tbody> <tr> <td style="padding-bottom:5px" valign="top"&g ...

Is it possible to link an HTML select element to a changing array in JavaScript?

Let's say I have an empty HTML select element. <select id="options"> </select> Can I link a JavaScript array to this select so that when the array is modified, the select options update accordingly? Alternatively, do I need to resort to ...

Issue with background in list items

I am struggling with aligning the background of my vertical menu to the right and center from the top. Here is the code I have tried: ul.nav { margin:0; background-position:center; background-image: url(../images/nav_bg.gif);font-family: "Century Go ...

Coding in HTML for the Font Awesome Plus/Minus Icon Toggle

In my role as a knowledge base manager, I primarily use Adobe RoboHelp and rarely need to utilize HTML outside of the standard editor. However, I am currently working on implementing a dropdown feature that involves changing the font awesome icon from fa-p ...

Using jQuery to update elements with the same class in HTML

This morning, I stumbled upon a rather perplexing issue. Despite my attempts to find a solution on SO, all I came across were methods involving data loading into HTML or a complete page refresh. My aim is to utilize jQuery's $.ajax function to update ...

Concealed Button in Javascript

I've got a JavaScript function that effectively hides a link or button for a specific period of time. JavaScript: <script type="text/javascript"> function showNextButton() { document.getElementById("nextbutton").style.visibility = " ...

Issue: ENOENT - The specified file or directory, './views/s.ejs', does not exist in Node.js Express

Encountering an error when attempting to render a file from the 'views' directory in the 'routes'. The specific error message is as follows: Error: Valid Login { [Error: ENOENT: no such file or directory, open './views/s ...

What is the process of making circle or square shapes with text inside using React, without the use of SVG images?

Is there a way to create circle and square shapes in React with custom text inside, without relying on SVG images? Check out this example: https://i.sstatic.net/iHZgy.png I attempted the code below but it did not render any shapes: import React from &apos ...

What could be the reason for the percentage not functioning properly in margin-top?

Attempting to apply a top margin in percentage (%) to the form element but encountering issues with the property not being accepted. The goal is to create a margin from the top for the form. Below is the code snippet: http://plnkr.co/edit/byQLFlLh4a2qaIEZ ...

Angular's getter value triggers the ExpressionChangedAfterItHasBeenCheckedError

I'm encountering the ExpressionChangedAfterItHasBeenCheckedError due to my getter function, selectedRows, in my component. public get selectedRows() { if (this.gridApi) { return this.gridApi.getSelectedRows(); } else { return null; } } ...

What is the best way to include a dialog div within a form tag?

I am facing an issue with a JQuery dialog on my webpage. The data entered into the dialog seems to get lost because the dialog is placed outside the form tag. Here is how it appears: https://i.stack.imgur.com/fnb07.png So far, I have attempted this soluti ...

Mastering the art of efficient navigation between various pages

In the create_session.php page, I have included a simple form below. The form action method is set to navigate the user to the "QandATable.php" page upon form submission. However, I would like to implement a conditional logic - if the user enters '1&a ...

"Fixing the Vertical Order of Divs with Jquery: A Step-by-

Before I completely lose my mind, I need some help with a coding issue. Right now, I have two initial divs displayed. The user should be able to add these divs below the original ones as many times as they want, or remove them if needed. I've been att ...