Home  >  Q&A  >  body text

How to restrict CSS to only take effect on one div in the same category

I'm trying to make a title appear over an image on mouseover, but they all have to have the same class. I don't know if what I'm asking is possible, but I'd consider various different solutions. This is my current code.

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

This is my CSS code

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

reply all(1)I'll reply

  • P粉709644700

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

    I just had to change the position to relative, here is my code

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

    reply
    0
  • Cancelreply