下拉 div 没有出现在按钮悬停上,z-index 也没有效果

Drop down div not appearing on button hover, also z-index having no effect

我正在使用 Styled-Components 在 React 中构建一些东西。

我正在尝试 div 解释悬停按钮时下拉的规则。

一切都很好,但是显示:none 悬停时没有消失。

每次我尝试做这样的事情时都会遇到这种麻烦,我什至复制了以前尝试的代码。我在网上找不到任何好的规则。如果有人可以 link 到博客 post 或网站来解释为什么会这样,或者向我解释,那将不胜感激。

JSX

<div className="rules-wrapper">
    <button id="rules-btn">
        <span role="img" aria-label="new game" className="hidden-icon">
            ❓
        </span>
        rules
    </button>
    <div className="rules-div">
        <h2>RULES:</h2>
        <p>Rule 1</p>
        <p>Rule 2</p>
        <p>Rule 1</p>
        <p>Rule 1</p>
        <p>Rule 1</p>
    </div>
</div>

样式组件

button {
    border: none;
    background: none;
    position: absolute;
    width: 200px;
    left: 50%;
    left: 50%;
    transform: translateX(-50%);
    color: #555;
    font-family: Lato;
    font-size: 20px;
    text-transform: uppercase;
    cursor: pointer;
    font-weight: 300;
    transition: background-color 0.3s, color 0.3s;
}

.rules-wrapper {
    left: 50%;
    transform: translateX(-50%);
    position: absolute;
    display: inline-block;
}

#rules-btn {
    top: 2rem;
}

#rules-btn:hover .rules-div {
    display: flex;
}

.rules-div {
    transform: translateX(-50%);
    display: none;
    position: absolute;
    left: 50%;
    top: 3.5rem;
    width: 20rem;
    transform: translateX(-50%);
    background: white;
    flex-direction: column;
    z-index: 5;
    border: 2px solid gray;
    padding: 2rem;
    align-items: center;
    box-shadow: 5px 5px 5px gray;
}

button:hover {
    font-weight: 600;
}

button:hover span {
    margin-right: 20px;
}

button:focus {
    outline: none;
}

span {
    display: inline-block;
    margin-right: 15px;
    line-height: 1;
    vertical-align: text-top;
    transition: margin 0.3s;
}

看起来你已经试过了:

#rules-btn:hover .rules-div {
    display: flex;
}

这将不起作用,因为 .rules-div 不是 #rules-btnchild。但它彼此相邻(兄弟姐妹),因此您可以使用 Adjacent sibling combinator

#rules-btn:hover + .rules-div {
    display: flex;
}

感谢@95faf8e76605e973:

这将不起作用,因为 .rules-div 不是#rules-btn 的子项。但它彼此相邻(兄弟姐妹),因此您可以使用 Adjacent sibling combinator

#rules-btn:hover + .rules-div {
    display: flex;
}

对于我必须添加的 z-index

.rules-wrapper {
     z-index: 5;
}