![](/img/trans.png)
[英]How do I only target the :hovered element and none of its parents in CSS?
[英]In next.js, using css, how do I target a child element when its parents is hovered over?
這是 css,當父元素“.collection”懸停時,它以子元素“#overlay”為目標。
.collection {
position: relative;
overflow: hidden;
}
.collection:hover #overlay {
position: absolute;
opacity: .3;
}
這是 html:
import styles from "./Home.module.css";
<div className={`${styles.collection} card`}>
<div id="overlay"></div>
</div>
當父元素懸停時,這些屬性不會應用於子元素。
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.
您需要使用 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.