Having trouble getting the hover effect to work when selecting a different section of the SVG

In my SVG checkbox design, I have a circle element surrounding a polyline element (which acts as the checkmark). The boundaries of the icon extend beyond the circle, causing hover styles to trigger even when hovering outside the circle. I want to change styles only when hovering over the circle itself, but I need both the circle and polyline to change on hover. I am using styled-components.

I have tried various methods to make the polyline change when the circle is hovered over.

Initially, I attempted:

& circle:hover circle, & circle:hover polyline { }

and similar variations. Eventually, I converted the circle and polyline elements into styled components for easier manipulation. Following a solution from this post, I tried this approach:

const Circle = styled.circle``;
const Polyline = styled.polyline``;

const Icon = styled.svg`
  width: 52px;
  height: 52px;

  & ${Circle}, ${Polyline} {
    fill: #fff;
    stroke: ${props => props.logged ? colors.secondary200 : colors.secondary400};

  & ${Circle}:hover {
    stroke: ${colors.secondary400};
    fill: ${colors.secondary400};
  }

  & ${Circle}:hover ${Polyline} {
    stroke: #fff;
    fill: ${colors.secondary400};
  }

However, the polyline still does not change when hovering over the circle. I also tried nesting:

  & ${Circle}:hover {
    stroke: ${colors.secondary400};
    fill: ${colors.secondary400};

    & ${Polyline} {
      stroke: #fff;
      fill: ${colors.secondary400};
    }
  }

None of these solutions seem to be effective. How should I properly select the polyline element?

EDIT: Upon reviewing the styled-components documentation, I realized it might be more appropriate to handle the styling within the Polyline component. Therefore, I tried the following:

const Polyline = styled.polyline`
  pointer-events: none;
  fill: #fff;
  stroke: ${props => props.logged ? colors.secondary200 : colors.secondary400};

  ${Circle}:hover & {
    stroke: #fff;
    fill: ${colors.secondary400};
  }
`;

Despite this attempt, the desired effect is still not achieved. However, strangely enough, ${Icon}:hover & { } works for some reason. I cannot use it because the Icon's boundaries exceed the circle, though. I am puzzled as to why the Polyline responds to hovering over the Icon but not the Circle. While the Circle clearly receives a hover event since its own hover styles apply, it seems that the Polyline ignores it.

View the JSFiddle here

Answer №1

Check out the solution provided: https://jsfiddle.net/x9j7ku8o/3/

An essential part is demonstrated here:

const Circle = styled.circle`
  &:hover+${Polyline} {
    stroke: #fff;
    fill: #21ccbf;
  }
`;

The issue arose from using the descendant combinator (indicated by a space between Circle and Polyline in the css selector), despite the fact that Polyline is not actually a descendant of Circle.

Polyline and Circle are siblings, so it is necessary to utilize one of the sibling combinators: either the adjacent sibling combinator or the general sibling combinator. The adjacent combinator was chosen, as Polyline and Circle are adjacent.

Crucial to remember the order: I incorporated the combinator &:hover+${Polyline} in Circle due to the placement of Circle before Polylines in your provided example's html structure.

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

"Utilizing Bootstrap to create a visually appealing image and text combination in the

I need to arrange the image, first span, and second span in a vertical stack for xs devices. The alignment should be centered for xs devices. If you want to see my code: CLICK HERE <div class="row"> <div> <div class="col-md-9 c ...

Slow rotation in CSS styling

.badge { -webkit-transform-style: preserve-3d; -webkit-perspective: 1000; position: relative; } .back, .front { position: absolute; -webkit-backface-visibility: hidden; -webkit-transition: -webkit-transform 1s ease-in; width: ...

Data is not being successfully transmitted through the ORM (Prisma) to the database

After spending hours trying to solve this issue, I've hit a roadblock. My current project is using Next.js 13. I managed to successfully connect my application to a Postgres Database, configure the Prisma schema, and test a migration with a seed.ts f ...

Looking for ethical ways to filter API data using React JS? Let's explore some best practices for maintaining integrity while sorting through

Every time I refresh the page, I encounter an error message saying "TypeError: Cannot read properties of undefined." Below is my code snippet for fetching data from an API: useEffect(() => { let isSubscribed = true; axios.get(myLocalAPI) . ...

Tips for perfectly centering a SPAN element within a DIV both vertically and horizontally

Here is an example of my HTML code: <div id="slideClick"> <div style="padding: 0; margin: 0 auto; width: 100%; height: 100%; text-align: center; border: 1px solid blue;"> <span class="sideMsg">MESSAGES</span> </d ...

Creating a Curved Border with Clip Path in CSS

Hey there! I'm currently attempting to add a speech bubble at the bottom left using clip-path, but I'm struggling to adjust the pixels just right to achieve the clear and exact look I want. Can someone provide suggestions on how to accomplish thi ...

Rendering in React by cycling through an array and displaying the content

I have been working on displaying two arrays. Whenever the button is clicked, a new user is generated and added to the array. My goal is to render the entire array instead of just the newest entries. I've attempted various methods but haven't had ...

Error: Unable to access the 'map' property of undefined in React while trying to read this.props.rsl['data']

The problem I am encountering is related to the map function, which displays data in the console log console.log("iiiiiiiiii",this.props.rsl['data']). However, when I attempt to use the map function {this.props.rsl['data'].map(row=>{ ...

Exploring the intricacies behind the functionality of the spread operator - what

Take a look at this coded function: funtion App(){ const [Cards, setCards] = useState([ {id: 0 , name: 'Harry Potter'}, {id: 1 , name: 'Hermonie Granger'}, {id: 2 , name: 'Ron Weasly'},]) const shuffle = (arr) ...

Enhancing performance of React Native app in production by eliminating console.log statements through yarn and npm

Employing console.log statements is a prevalent technique for debugging in various JavaScript applications, including React Native apps. However, it is important to remove these console statements before publishing React Native apps to prevent potential ...

What is the most effective way to leverage rxjs for executing multiple asynchronous fetch operations in one go using a forEach strategy?

As a newcomer to React, I have been assigned the task of modifying an existing application that already makes multiple API calls. The current pattern in use is illustrated below. For example, if an action mapperActions.selectPolygon of type CaseReducerActi ...

CSS: Adjusting the vertical alignment of numbers in an ordered list

Hey there, I have a question about the vertical alignment of numbering in an ordered list. It seems that the numbers are being pushed to the baseline when <li> elements contain floated divs only. Is there any way to prevent this and have the numbers ...

What is the process for passing a component as a parameter to a function within a different component?

Within my scenario, I have a series of components '1,2,3,...' imported into another primary component 'A'. Within component 'A', certain operations are performed and a filtered component is then returned from the list of compo ...

Strange HTML pop-up keeps appearing every time I use styles in my code or insert anything with CSS

Recently, I created an OctoberCMS backend setup through my cPanel (I also have one on my localhost). Now, I am trying to add a background image but every time I attempt to do so, a strange HTML popup appears that I can't seem to figure out. Here is th ...

What is the best way to create a dynamic textbox or label within a specific div class?

I need assistance with my dynamic asp.net page where I am generating labels and text boxes in a form. Is there a way to ensure that these elements are generated within a specific div class? Below are the relevant code snippets: for (int i = 0; i < cou ...

Guide to generating interactive material-ui components within a react application?

I am facing a challenge in creating a dynamic mui grid element triggered by a button click in a React component. When attempting to create let newGrid = document.createElement('Grid'), it does not behave the same way as a regular div creation. D ...

The dropdown menu is being truncated

I am having an issue with a drop-down menu being cut off by its parent div. When I increase the height of the parent div, the drop-down menu becomes visible. Can someone please assist me? Below is my code snippet: MarkUp <div id="main-navigation" clas ...

Showing a variety of pictures within a specified time frame

In my CSS, I have defined classes that allow me to display different background images on a page at set intervals: .image-fader { width: 300px; height: 300px; } .image-fader img { position: absolute; top: 0px; left: 0px; animation-name: imagefade; ...

Generating React maps using a seeds file

Here is a snapshot of my Evolution Part 1 page created with HTML. Currently, I am working on implementing react state to showcase classic ships in boxes. You can find the code here. I am transitioning from the Evolution Part 1 component to a new ClassicS ...

Unable to locate '@material-ui/core/Alert'

I encountered an issue while trying to utilize the Alert component from material-ui. Despite successfully installing @material-ui/core, I keep receiving an error stating "Can't resolve '@material-ui/core/Alert'". package.json ''& ...