Is there a way to make my siblings' elements turn black when hovering over an element?

Currently, I am working on CSS within Reactjs. In my project, I have two sibling elements: a logo and a text placed beside it. My goal is to have the text turn black when I hover over the logo, but so far, I have been unsuccessful in achieving this effect.

I attempted to share a className between the elements, but this approach did not work as expected.

Below is a snippet from my file.js:

export default () => {
  return (
    <div className={style.logo_flex}>
      <div>
        <Link to="/"> 
          <img className={style.logo} src={Logo} alt=""/>
        </Link>
      </div>
      <Link to="/"> 
        <p className={style.brandName}> Coding </p>  
      </Link>
    </div>
  )
}

Here is the portion of my file.css responsible for styling:

/* Logo */

a { 
    text-decoration: none;
    color: grey;
}
a:active { 
    border: none; 
}

a:visited { 
    color: grey;
}


.logo_flex { 
    height: 100%;
    display: flex; 
    flex-direction: row;
    align-items: center; 
}
.logo_flex:hover {
    color: black;
}
.logo {      
    left: 7%;
    top: 0.2em;
    height: 2.2em;  
}


p, div { 
    display: inline-block;  
}

.brandName { 
    margin-left: 1.2em;  
    align-self: center;
}

.brandName:hover, .logo:hover { 
    color: black; 
}

Answer №1

  • Try this alternative approach instead of using Pseudo-classes.
import React, { Component } from 'react';

export default class App extends Component {
    constructor (props) {
        super(props);

        this.state = {
            logoClass: 'logo',
            brandClass: 'brandName',
        };
    }


    handleHover = e => this.setState({ brandClass: 'brandNameHover' });

    render = () => {
        const { logoClass, brandClass } = this.state;

        return (
            <div className='App'>
                <img className={ logoClass } src={ 'test' } onMouseOver={ this.handleHover } />

                <p className={ brandClass }>Coding</p>
            </div>
        );
    }
}

Answer №2

In order to update the appearance of logo_flex:hover, you must modify the background-color. A simplified version of the corrected code is shown below.

import React from "react";
import ReactDOM from "react-dom";

import "./styles.css";

function App() {
  return (
    <div className="logo_flex">
      <div>
        <img
          className="logo"
          src=""
        />
      </div>
      <p className="brandName"> Coding </p>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Style:

a{ 
    text-decoration: none;
    color: grey;
}
a:active{ 
    border:none; 
}

a:visited{ 
    color:grey;
}


.logo_flex{ 
    height: 100%;
    display: flex; 
    flex-direction: row;
    align-items: center; 
}
.logo_flex:hover{
  background-color: black;
}
.logo{      
    left: 7%;
    top: 0.2em;
    height: 2.2em;  
}


p, div { 
    display: inline-block;  
}

.brandName{ 
    margin-left: 1.2em;  
    align-self: center;
}

.brandName:hover, .logo:hover{ 
    color:black; 
}

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

What is the best way to extract the href value or URL from a JavaScript function and then utilize it for redirection

I need to redirect to a specific hostname from the request by adding "https://" in front of it. <a target="_blank" href="javascript:createDynamicPubUrl();" > Here is the function that generates the URL by combining the hostname with the protocol (h ...

Determine the full location information with the help of Google Maps SDK without the need

My current challenge involves dealing with a list of unformatted and incorrectly written addresses. I am seeking a way to iterate through these flawed strings and generate more organized and accurate addresses using one of the many Google Maps SDKs availa ...

When starting a new project with Angular 7, the option to set up routing is automatically included without asking for confirmation

After switching from Angular 6 to version 7, I encountered an issue while creating a new project in CLI using ng new [app-name]. It simply starts without giving me the option to include routing or styling in my project. Just a heads up, I am currently run ...

Tips for triggering a method upon the initial passing of props to a Vue.js component

<script> import _ from "lodash"; export default { name: "QuestionBottom", props: { currentQuestion: Object, nextQuestion: Function, increment: Function, }, ...

To grab a specific CSS attribute from a stylesheet using JavaScript

I am looking for a way to extract the value from a CSS file using a JavaScript function. Take a look at my code snippet below: HTML file -> <link rel="stylesheet" type="text/css" href="test_css.css" /> <script type="text/javascript ...

Tips for querying orchestrate.io

Recently, I found myself in need of a user-friendly database for a small highscore system in my game development projects using JavaScript. Through the Github student developer pack, I came across Orchestrate.io. After discovering a suitable driver module ...

Activate the drop-down menu in Angular 6 by hovering over it with your mouse

I am just beginning my journey with Angular 6 and Bootstrap. Currently, I am working on a Navigation bar that consists of 3 navigation items. One of the nav items is called "Store", and when a user hovers their mouse over it, I want to display a mega menu ...

Is my JavaScript coding accurate?

I'm working on extracting JSON data from my Rest API to display in my application. I'm wondering if the function renderUserState is being called, as the rest of my code seems to be functioning correctly. function testSubmit() { var card = ge ...

Extracting data from the response object and injecting it into the request

Once the file has been successfully uploaded, the 'uploadSuccess' callback event is triggered, providing information about the newly created media. The 'hashed_id' value within this object is crucial for my PUT request. I attempted to ...

Guidance on integrating a pig latin translation npm module into a React application

My task is to develop a React/Redux application that translates English sentences into Pig Latin. After discovering existing npm packages designed for this purpose, I decided to leverage them in my project. To begin, I created a straightforward functional ...

Generate a PDF file using a byte array and save it for download

I am working on an AJAX function that calls a server-side method which in turn calls an API and returns a byte array. My goal is to convert this byte array into a PDF file and then download the file. Here is the code I have: AJAX function: function Obtai ...

`The styling of a Select component is getting lost upon page refresh.`

Currently, I am in the process of developing a mini website for my company's break room. The website is built using React and Gatsby to convert the app into static content. An issue that I have encountered is that upon refreshing the page where the M ...

Creating packing features specifically designed for resolution within a reusable module

I've decided to revamp my Angular application based on John Papa's style guide (well, mostly) and my main focus is on improving modularity. The stumbling block I've encountered is with route resolves. So far, I've been using a global ap ...

The method for connecting the width of a panel to the height of the viewport in Ext Js

I'm looking to automate the adjustment of a panel's height based on the viewport's height using Ext js. Although achievable using listeners, I believe utilizing Ext js variable binding would be more efficient. However, I've encountered ...

Displayed unfamiliar Obj characters on Google Chrome

Hey there, I'm new to Vue JS and Vuetify and could really use some help! I've been trying to display some HTML content from my data using v-html in Vue JS, but for some reason, strange OBJ symbols keep showing up. Here is a screenshot of what I ...

The javascript function "Scroll to the element" is not functioning properly on my website. Perhaps using JQuery will resolve the issue

I used this bootstrap template to create my website. Everything worked perfectly except for the "scroll to the element script". I kept encountering the following error: Uncaught Error: Syntax error, unrecognized expression: [href=#],[data-toggle],[data-tar ...

Troubleshooting ECONNREFUSED in NextJS API: App successfully runs in both deploy and development mode, but encounters issues when next build is executed

Detailing the Glitch I've developed an application using NextJS that incorporates an internal API, utilizing the getStaticProps methods to interact with the API. You can access the live app at this link: Additionally, you can find the project' ...

The entry description function is not functioning properly when used with jGFeed

I have implemented jGFeed to extract data from an RSS Feed. Below is a snippet of the code I am using: $.jGFeed('http://feeds.bbci.co.uk/news/rss.xml', function(feeds){ // Check for errors if(!feeds){ ...

Challenges with Incorporating Stripe

I am currently working on integrating Stripe into a school project. Despite following the documentation and instructions provided, I keep encountering a POST 500 (INTERNAL SERVER ERROR) along with an error message stating "Error fetching payment intent: Er ...

Error in Gatby development: Process Undefined

After successfully upgrading the dependencies and running the development server in my Gatsby application, I encountered an abnormal app crash with the following error: Here is a screenshot of the error from the developer console: https://i.sstatic.net/Cl ...