Is there a way to achieve a hover effect by connecting one element's behavior with the hover effect on another element?

I am working on a project using Gatsby and SASS, and I am trying to implement a hover effect where the FontAwesomeIcon element changes its CSS properties when hovering over the title h1. In pure HTML and CSS, this can be achieved using ".title:hover .arrow { transform: translateX(100px);}" or ".title:hover~.arrow { transform: translateX(100px);}". However, in SASS, the only option seems to be using ".block:hover{.title {transform: translateX(100px);}}". Is there a way to achieve this effect without having a parent element for the children (title and arrow)?

import React from "react"
import style from "./mainPromo.module.sass"
import classNames from "../../helpers/classNames"
import Slider from "react-slick"
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'
import {faArrowRight} from '@fortawesome/free-solid-svg-icons'

const MainPromo = () => {
  const settings = {
    slidesToShow: 1,
    slidesToScroll: 1,
    infinite: true,
    arrows: false,
    dotsClass: "orange_line_slick_dots",
    dots: true,
  }

  return (
    <Slider className={classNames(style.section, style.promo)} {...settings}>

      <div>
        <div
          className={classNames(
            style.section,
            style.justify_center,
            style.outcomes
          )}
        >
          <div className={classNames(style.content_1600, style.flow_row)}>
            <div className={style.outcomes_info}>
              <h1 className={style.outcomes_h1}>
                Web development
              </h1>
              <p className={style.outcomes_p}>
                Web-based solutions for small and medium enterprises and startups
              </p>
            </div>
            <div className="outcomes_arrow">
              <FontAwesomeIcon icon={faArrowRight}/>
            </div>
          </div>
        </div>
      </div>
    </Slider>
  )
}

export default MainPromo

SASS

.outcomes_arrow
  padding: 120px 0 0 40px
  font-size: 5rem
  transition: 1s

.outcomes_h1:hover~.outcomes_arrow
  transform: translateX(100px)

Answer №1

Here is a helpful solution that I found:

If you are using SASS, you have the ability to nest elements within each other. The & symbol specifically represents the parent object in which you are nested.

.results_h1:hover {
    &~ .results_arrow{
        transform: translateX(100px)
    }
} 

Answer №2

Here is the modified code to change the position of "FontAwesomeIcon" so that it becomes siblings with h1:

          <div className={classNames(style.content_1600, style.flow_row)}>
            <div className={style.outcomes_info}>
              <div className={style.outcomes_animation}>
                <h1 className={style.outcomes_h1}>
                  Web development
                </h1>
                <FontAwesomeIcon icon={faArrowRight} className={style.outcomes_arrow}/>
              </div>
              <p className={style.outcomes_p}>
                Web-based solutions for small and medium enterprises and startups
              </p>
            </div>
          </div>

SASS

.outcomes_arrow
  padding: 130px 0 0 40px
  font-size: 5rem
  transition: 1s

.outcomes_h1:hover~.outcomes_arrow
    transform: translateX(100px)

Answer №3

To ensure proper styling, you can make use of Javascript or adjust your HTML layout so that the outcomes_arrow element becomes a sibling to both the h1 and p elements.

<div className={style.outcomes_info}>
     <h1 className={style.outcomes_h1}>Web development</h1>
     <p className={style.outcomes_p}> 
       Web-based solutions for small and medium enterprises and startups
     </p>
     <div className="outcomes_arrow">
         <FontAwesomeIcon icon={faArrowRight}/>
     </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

Are your file uploaders malfunctioning by saving empty image files?

I am currently working on a file uploader using JavaScript and Classic ASP. The process involves importing an image into a canvas, converting it to a base64 URL, and then sending that URL to the ASP script for decoding and downloading. Although my AJAX re ...

Add javascript and jquery together into a single function

$(document).ready(function() { $("#btn1").click(function() { $("p").append(" <b class='phs' id='btn1r' onclick='$(this).remove();$(\"#btn1\").show()'>Auto</b>"); $("#btn1").hide(); }); $("#b ...

The ng-isolate-scope is not properly connected to the specified templateUrl

I am encountering difficulties when trying to implement isolated scope with templateUrl. Here is my directive test: beforeEach(ngModule('app.directives')); var scope, compile beforeEach(inject(function($rootScope, $compile){ scope = $ro ...

Integrating the feature of pasting Excel data directly into a Django form

My current Django form consists of a table with N rows and 12 columns of textboxes, allowing users to fill out the form one textbox at a time: [________][________][________][________][________][________][________][________][________][________][________][_ ...

The Mat-Table does not display any content despite the fact that the data is being recorded in the console

I have been trying to implement a mat-table in my Angular application to display a list of users fetched from a Spring RestAPI database. However, despite following several tutorials, I am facing an issue where only the table headers (column names) are bein ...

Personalize the Hover color on your flexdashboard with a touch of custom CSS

I am currently working on building a dashboard with Flexdashboard and I want to customize the hover and active colors of the Flexdashboard pages named Capture and Results with specific colors that are unique to the client. I prefer not to use the default F ...

AngularJs: Issue encountered while compiling an ng-switch statement

Programming <input type="text" ng-model="user.name" /> <div ng-switch on="user.name"></div> <p ng-switch-default>The winner is</p> <h1 ng-switch-when="John">{{ user.name }}</h1> Can anyone help me? I encountered ...

Discover the steps to transform the elements within an array to a particular structure

I am working with an API that has the following structure: [ { "id": 4, "code": "IN", "name": "India", "recordVersion": "6CB5C760-E145-4FFA-AC63-FAF5F36B6C80", "updatedBy": "Sini " }, { "id": 59, "code": ...

Script malfunctioning following an AJAX request

My page consists of a header where all my javascript is located. Within the body of the page, there is a link that triggers an ajax http request using vanilla JavaScript (not jQuery). This request retrieves a PHP form and injects it onto my page. The PHP ...

Is there a way to create a navigation menu that highlights as we scroll down the page?

Check out this example of what I am looking for. https://i.stack.imgur.com/fdzvZ.png If you scroll, you will notice that the left menu highlights. Sub-menus will extend when the corresponding menu is highlighted and collapse when other menus are highlig ...

How do I locate the specific page in angular.js where I need to make changes?

I have recently finished a project and now I am facing an issue that needs to be solved. In this project, there are 3 select boxes, each with different names. The first select box is named nameSelectBox162572640796915, the second one is named nameSelectBox ...

Issues arise when trying to use JQuery in conjunction with JSON data

I've been working on a project that involves creating a dynamic dropdown list, but I've run into an issue with jQuery and JSON. This is my first time working with JSON, so I'm still getting the hang of it. Despite googling for solutions, I h ...

Using AJAX within the $.when function to extract the first character from the returned string

Currently, I am utilizing an AJAX call by using $.when to wait for the completion of that specific ajax request and then continue with processing the subsequent ajax request inside. The code block below demonstrates where the $.when method is being invoke ...

Arranging a Vertical Element Within a Rotated Holder

Who would have thought that geometry would come into play when working with CSS? I need help figuring out how to perfectly cover a rotated container with an upright background image. The goal is to hide the rotation on the sides and maintain dynamic contro ...

What is the best way to ensure jQuery loads before CSS code on a website

I am currently working on a blog project that allows visitors to customize the background color by simply clicking on one of five available buttons. $(function(){ //Checking if a color scheme has already been selected var chosenColor = ...

Filtering a table in Angular2 based on two inputs with a custom pipe

Within my table, I have two columns that display dates as strings. My objective is to filter this table based on input from two ngb-datepickers (with a default value of null). Despite importing a custom pipe into declarations in app.module.ts, I keep encou ...

Exploring Elements with JQuery through a Dropdown Selection

When a selection is made in the dropdown list other than "Condition", I want it to search for and display all matching elements. Here is my jQuery code: $('#searchbtn').click(function(){ var e = document.getElementById("condition"); var str ...

Error message: The function s.t.match is not defined in the context of react-export-excel

I have integrated Excel Export with custom cell styles from the react-export-excel examples in my frontend application. However, I encountered the following error whenever I attempt to click on the Export button. Uncaught TypeError: s.t.match is not a func ...

Is the format of this HTML tag correct?

I was provided with a code from our design department to add to a page, and since I am relatively new to the design aspect of things, I'm seeking clarification. Is this a valid tag? Also, what does <--if subcontent add rel="test1"--> mean? The c ...

Ways to Achieve the Following with JavaScript, Cascading Style Sheets, and Hypertext

Can someone help me convert this image into HTML/CSS code? I'm completely lost on how to do this and don't even know where to start searching for answers. Any assistance would be greatly appreciated. Thank you in advance. ...