简体   繁体   English

在 next.js 中,使用 css,当其父元素悬停在其上时,如何定位子元素?

[英]In next.js, using css, how do I target a child element when its parents is hovered over?

This is the css that targets the child element, "#overlay", when the parent, ".collection", is hovered.这是 css,当父元素“.collection”悬停时,它以子元素“#overlay”为目标。

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

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

This is the html:这是 html:

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

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

The properties are not applied to the child element when the parent is hovered.当父元素悬停时,这些属性不会应用于子元素。

The problem is that by default Next.js uses css module when importing css from Components, that means that import of css will return an object with class & id map to uglified strings. The problem is that by default Next.js uses css module when importing css from Components, that means that import of css will return an object with class & id map to uglified strings.

You need to use class selector and use it on the child component.您需要使用 class 选择器并在子组件上使用它。

.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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在CSS中只定位:hovered元素,而又不定位其父元素? - How do I only target the :hovered element and none of its parents in CSS? 当我 hover 在一个 div 上时,我希望它根据悬停的第 n 个子元素的数量来更改它旁边的元素 - when I hover over a div I want it to change the element next to it depending on the number of the nth child that is hovered over 使用CSS,如何更改父级的字体颜色 <li> 标记如果孩子 <li> 标签悬停在上面 - Using CSS, how do I change the font color of parent <li> tag if child <li> tag is hovered over 如何定位css中的下一个元素? - How do I target the next element in css? 在 next.js 中,如何将自定义 css class 和引导程序 ZA2F2ED4F8EBC04AB614C21A29 - In next.js, how do I apply both a custom css class and a bootstrap class to an element 将鼠标悬停在子元素上时,CSS父列表会折叠 - CSS-Parent List Collapses when hovered over a child element 如何在 Next.js 中使用 CSS Houdini - How do I use CSS Houdini in Next.js 当子元素悬停时,我如何选择父元素? - How would I go about selecting a parent element when a child element is hovered over? 使用CSS / JS隐藏 <h2> 悬停在div中时 - Using css/js, hide <h2> in a div when hovered over 如何选择CSS中目标元素之后的下一项? - How do I select the next item after a target element in css?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM