首页  >  问答  >  正文

如何限制CSS仅对同一类别中的一个div生效

我正在尝试在鼠标悬停时使标题出现在图像上,但它们都必须具有相同的类。我不知道我所问的是否可能,但我会考虑各种不同的解决方案。这是我目前的代码。

<div class="item">
            <img class="Placeholderimg" src="placeholder.jpg">
            <div class="hovershow1">
                <div class="title">
                    Testing1
                </div>
            </div>
        </a>
    </div>
    <div class="item">
        <img class="Placeholderimg" src="placeholder.jpg">
        <div class="hovershow2">
            <div class="title">
                Testing2
            </div>
        </div>

这是我的CSS代码

.item{
    height: 156px;
    width: 156px;
}

.Placeholderimg{
    height: 156px;
    width: 156px;
    border-radius: 10px;
}

.hovershow1{
    visibility: hidden;
    position: absolute;
    top:0;
    height: 156px;
    width:156px;
    background-color: rgba(40, 40, 40, 0.4);
    border-radius: 10px;
}

.item:hover .hovershow1{
    visibility:visible;
}

.hovershow2{
    visibility: hidden;
    position: absolute;
    top:0;
    height: 156px;
    width:156px;
    background-color: rgba(40, 40, 40, 0.4);
    border-radius: 10px;
}

.item:hover .hovershow2{
    visibility:visible;
}
P粉475315142P粉475315142405 天前483

全部回复(1)我来回复

  • P粉709644700

    P粉7096447002023-09-10 15:21:29

    我只是不得不将位置更改为相对位置,这是我的代码

    .item{
        height: 156px;
        width: 156px;
    }
    
    .Placeholderimg{
        height: 156px;
        width: 156px;
        border-radius: 10px;
    }
    
    .hovershow{
        visibility: hidden;
        position: relative;
        height: 156px;
        width:156px;
        background-color: rgba(40, 40, 40, 0.4);
        border-radius: 10px;
    }
    
    .item:hover .hovershow{
        visibility:visible;
    }

    回复
    0
  • 取消回复