How can I use CSS in Next.js to style a child element when its parent is being hovered over?

Here is the CSS code that specifically targets the child element "#overlay" when its parent, ".collection", is being hovered over:

.collection {
    position: relative;
    overflow: hidden;
}

.collection:hover #overlay { 
    position: absolute;
    opacity: .3;
}

And this is the corresponding HTML:

import styles from "./Home.module.css";

<div className={`${styles.collection} card`}>
    <div id="overlay"></div>
</div>

However, it seems that the properties are not being applied to the child element when the parent is in hover state.

Answer №1

One issue is that Next.js by default utilizes css module when importing css from Components, resulting in an object with class & id map to cryptic strings.

To address this problem, you must employ the class selector and apply it on the child component.

.collection {
    position: relative;
    overflow: hidden;
}

.collection:hover .overlay { 
    // -----------^
    position: absolute;
    opacity: .3;
}
import styles from "./Home.module.css";

<div className={`${styles.collection} card`}>
    <div id="overlay" className={styles.overlay}></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

What is the best way to maintain the same height for a textarea and input fields?

What is the best way to align the message input field with the name and phone input fields? I need the height of the message input field to match the combined height of the three inputs on the left. It's crucial that the borders line up perfectly. I ...

The data-toggle function is not functioning properly with the code provided

Could someone shed some light on why my tabs and navigation bar dropdown button are not functioning properly? <div class="col-12"> <h2>Corporate Leadership</h2> <ul class = "nav nav-tabs&q ...

Arranging items in a vertical column using CSS based on the content

I am facing an issue with aligning the values in a column under each other without percentages using my own html code, css, and bootstrap classes: <div class="d-flex"> <p class="table-string">91.86</p> <span ...

Template for developing projects using JavaScript, HTML5, and CSS3 in Visual Studio 2013

After recently downloading Visual Studio 2013 Express for Web, I am struggling to figure out how to deploy projects that only consist of JavaScript, HTML5, and CSS3. Despite searching online for JavaScript templates and even trying to download Visual Stu ...

Problem with the appearance of the drop-down menu

After a few dedicated weeks on a Web application project, I am currently tackling the challenge of a drop-down menu. Despite its functionality, there are two specific points I need help with: When expanding the menu by selecting a main item, I want to pr ...

Flexbox - Consistent height image columns

I'm working on a basic flexbox layout that looks something like this: html,body { margin:0; padding:0; } body { height:100%; width:100%; } .panel-grid { -webkit-align-items: flex-start; ...

Tips for utilizing "useEffect" and "useCallback"?

Currently, I am developing a Next.js application and one of the components in my app is a header. This header includes an image that is fetched from a database. useEffect(() => { const data = JSON.parse(localStorage.getItem('userData')) ...

Steps for launching a hyperlink in a browser on Vue Native

I'm having trouble finding information on how to properly use the Linking component in react-native. The resources available are limited, and I'm not sure if I am implementing it correctly... Below is what I have in my template: <nb-button :o ...

Exploring Next.js 13: Tailoring Next.js layouts to suit your route structure

In my latest project with Next.js version 13, I am encountering a situation involving a route called problems/[slug]. At the initial level of this route (e.g., problems/react), there is a sidebar and content section that are being shown using a custom layo ...

What causes Next.js code lifecycle to run multiple times?

Recently, I dove into learning Next.js and encountered something peculiar. The issue revolves around this straightforward code snippet: import {useRouter} from 'next/Router'; import {Profile} from '../../components/Profile'; const Jobs ...

The module specifier for React could not be resolved when attempting a dynamic import from the API

I'm currently experimenting with dynamically importing a React component library from an API. I've successfully fetched the JS file and transpiled it using Babel without any issues. When I try to import the Dummy.js file from localhost like impor ...

Unable to save input from <from> in the React state object

I'm currently working on a project in reactjs where I need to store user input in the react state object. I followed an example from reactjs.com, but it seems like the input is not being stored in the state object as expected. class CreateMovieForm ex ...

Using Express and React with Socket.io

I am currently working on setting up a basic WebSocket connection using Socket.io, Express, and ReactJS. Below is the code for the server: const express = require('express'); const socket = require('socket.io'); const path = require(& ...

Creating individual components for <select> and <option> elements in ReactJS

I am uncertain about how references function when the select and option tags are used together. When I split them into separate React components, they stop working. How can I resolve this issue? Here is a basic example: <select> {() => ( ...

Sorting Contentful posts by date across various Content types

Working on my first project with GatsbyJS and Contentful, I currently have various posts on the website categorized under different Content Models. Let's simplify it by saying there are Photo content types as well as Video Embeds. Using GraphQL to fet ...

What could be causing my absolutely positioned div to be hidden from view?

I'm working on designing a slideout slideshow with three images and content that looks like papers hidden in folders, complete with a handle at the top or bottom of each folder displaying its name. Here's an example of what I'm envisioning: ...

Html content overlapping div rather than being stacked vertically

My goal is to arrange a group of divs inside another div in a vertical stack. However, I am facing an issue where the text from the last div is overlapping with the second div instead of following a proper vertical alignment where the text in the third div ...

Including a logo and navigation bar in the header while collaborating with different subregions

I'm having trouble getting a picture to display in the header alongside a horizontal menu. I've divided the header into two sections: img and navbar (html). The navbar is showing up correctly, but the image isn't appearing. Any suggestions o ...

What causes offsetHeight to be less than clientHeight?

INFORMATION: clientHeight: Retrieves the height of an element, taking into account padding offsetHeight: Obtains the height of an element, considering padding, border, and scrollbar Analyzing the Data: The value returned by offsetHeight is expected to ...

Changing the checkbox state using react JavaScript

Here is my code snippet for a checkbox in a React component, utilizing Material-UI: const [checkmark, setCheckMark] = useState(false); const [selectedRows, setSelectedRows] = useState([]); const toggleAllCheck = (event) => { console.log("before", ch ...