首頁  >  問答  >  主體

如何限制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 天前484

全部回覆(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
  • 取消回覆