Issues with nested onHover selector functionality in styled-components

When trying to change the style of a styled-components element upon hovering over another element, I'm running into an issue where the changes are not being applied.

The relevant code snippet is as follows:

const Logo = styled.div`
  . . .
  &:hover {
    color: red;

    ${TextStyled} {
      display: flex;
    }
  }
`;

Although the color of the Logo component successfully updates to red on hover, the display property of the TextStyled component does not seem to be affected.

I have attempted using a tilde before ${TextStyled}, but that selector did not work either.

In addition, applying the style through the parent Main component also proved to be unsuccessful:

const Main = styled.div`
  height: 100vh;
  width: 100vw;
  background: #000;

  ${Logo} {
    &:hover {
      color: red;

      ${TextStyled} {
        display: flex;
      }
    }
  }
`;

https://codesandbox.io/s/cool-grass-2x1ob?file=/src/Component.js

Answer №1

It seems like the issue is that it's not a direct child of the element being hovered over. Here's a possible solution:

&:hover + ${TextStyled}   {
    color: red;
    display: block;
    
  }

Alternatively, you could nest TextStyled within Logo like this:

<Logo>Logo<TextStyled /></Logo>

In the Logo component:

&:hover {
    color: red;

    ${TextStyled} {
      display: flex;
    }

Following your comment, here's another approach: create a separate div to contain both logo and textstyled elements.

<Cont>
      <Logo>Logo</Logo>
      <TextStyled />
    </Cont>

Then define styles for Cont:

const Cont = styled.div`
  width: 100%; // example
  &:hover {
    ${TextStyled} {
      display: block;
    }
  }
`;

And for Logo:

const Logo = styled.div`
  font-weight: 700;
  color: #ffffff;
  font-size: 4em;
  letter-spacing: 5px;
  transition: 0.25s;
  z-index: 200;

  &:hover {
    color: red;
    filter: blur(10px);
  }
`;

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

Troubleshoot visual studio html/css conflict

I've been encountering some issues with Visual Studio. When I try to run the site, certain changes I make to the CSS don't seem to reflect, and elements in the HTML that I move around do not display correctly. I'm using jQuery to show and hi ...

Material UI autocomplete displaying differently than anticipated

My autocomplete feature is connected to an API that provides a list of books. I am displaying these books as options in the Autocomplete component, and also showing them in a list at the bottom of the page for debugging purposes. Additionally, I am outputt ...

Achieving precise alignment of div content through Css techniques

body { margin: 0; font-family: Arial, Helvetica, sans-serif; } .topnav { overflow: hidden; background-color: #333; } .topnav a { float: left; display: block; color: #f2f2f2; text-align: center; padding: 14px 16px; text-decoration: ...

The Bootstrap Navbar is occupying an excessive amount of room on compact screens

Currently, I am developing a responsive website using bootstrap 3. However, I am encountering an issue with the collapsed navbar. Instead of the normal height (i.e. height of one nav link item), it is taking up the total height of all the nav link items. Y ...

Is Bootstrap failing to function properly in my Angular project?

During my work on an angular project, I decided to implement bootstrap CSS and js for styling. However, after completing the project, I noticed that the bootstrap was not functioning correctly. I found that when I used the CDN link in the project, the bo ...

getDerivedStateFromProps will be triggered every time a field is updated in React

I am currently working with a form that contains 8 fields. In order to populate these fields with data retrieved from an API, I am utilizing the getDerivedStateFromProps method to set the initial state. static getDerivedStateFromProps(nextProps, prevState ...

Encountered an error while using webpack-dev-server on Windows_NT 7601 system,

Encountered an error while trying to run the following command: $ npm run dev I have used nodejs cmd, git, and have administrative privileges with the cmd. The error message is as follows: D:\IIT Samester\KAZ\ReactJSProjects\Git ...

What is the best way to switch from a React app on port 5173 to another React app on port 5174 while in the development phase?

In my current project setup, there is a backend server along with two separate React applications - a frontend website and a dashboard, each running on different ports. After the user logs in on the website, they have the option to access the dashboard. Ho ...

I always attempt to utilize the const useStyles method within my class components

Currently, I am working on a project using the React Framework and the Material-UI library. I found my templates at https://material-ui.com/getting-started/templates/ --> Sign In I switched from a function component to a class component in order to u ...

NextJS leading me to a fresh page packed with clickable links

Currently, I am attempting to create a functionality where clicking on the text in the navigation bar will smoothly scroll down to specific sections in my Next.js application. The code I currently have is as follows: <Link href="#About">Ab ...

The Angular tutorial for the "Tour of Heroes" is experiencing issues with aligning the heroes' list properly

I am currently working on the Angular tour of heroes tutorial. However, I am facing an issue when trying to display the list of heroes as it appears like this: https://i.sstatic.net/AGnzJ.png It is strange because even though the CSS/HTML/TS code from the ...

Issue with missing CORS allow origin in ReactJS

Currently, I am delving into a project utilizing react js, node, and axios for handling API requests. Unfortunately, I hit a roadblock with one of the API requests due to an error message stating "CORS missing Allow-Origin". This indicates that the cross ...

Having trouble with Cors Access-Control-Allow-Credentials failing even though it's set to true? Looking for solutions?

Hello everyone, I am currently working on a school project and facing an issue with CORS. I have tried using the loopback address and localhost, set headers, and installed cors. The frontend is built on React running on port 3000 while the backend is devel ...

Hover over me to see the CSS animation translate upwards until it reaches a specific point

I am new to CSS animation and I am trying to create a circle that moves from one end to the other on hover. However, I am facing an issue where the circle goes off-screen when the browser size is changed. How can I make it stop at a certain point within th ...

Is it possible to import npm modules conditionally?

Here is the structure of my project: - workspace - customPackage - customIndex.js - myProject - index.js - myProject2 - index.js During development, I need to import the package from my local workspace like this: //index.js import some ...

What could be the reason for the lack of updating the value of my page for this specific URL?

import { useState, useEffect } from "react"; import MovieCard from "./MovieCard"; const PopularMovies = () => { const [movies, setMovies] = useState([]); const [loading, setLoading] = useState(false); const [ ...

What causes the behavior of <body> backgrounds to differ from that of other HTML elements?

After examining the HTML code provided: <p style="margin:100px;border:1px solid red;background-color:#dff"> Hello </p> We can observe that when rendered by the browser, it looks like this: https://i.sstatic.net/rADHC.png If we were to mod ...

The div element is just a tad smaller in size (0.085 smaller as per the CSS calculation)

I have a div with fixed height and width of 24px with a background image, but the browser is cropping off 0.1 pixels... div { width: 24px; height: 24px; background: url(svg); } Upon inspecting the computed styles, I noticed that the height is actua ...

What is the method for adding a prefix to every line in JavaScript?

I'm currently working on a React project and dealing with a string that may contain newlines. I'm looking for the most efficient way to inject a symbol at the start of each line. I've considered exploding the string into an array and adding ...

The placement is set to absolute, with a designated height

Here is an example http://jsfiddle.net/HnfCU/ I am using jQuery to toggle the visibility of the .child div. The position of the .child is set to absolute relative to its parent element, .parent. The challenge I am facing is adjusting the height of the .ch ...