Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide all div of particular class except one using CSS only

I have below HTML and I want to hide all divs having class 'msg' excluding one div having same class.

<html>

<div class="ref" >
    <div class="msg">  Message ref </div>
</div>

<div class="msg">  Message 1 </div>
<div class="msg">  Message 2 </div>
<div class="msg">  Message 3 </div>

</html>

Here I want to hide all div having class 'msg' except div which is inside another div having class 'ref' using css only.

My style for that is

.msg:not(.ref.msg) {
     display: none;
}

But it is not working.Please suggest me some required tweaks to my CSS style to achieve result.

Thanks.

like image 487
Oomph Fortuity Avatar asked Jan 07 '23 13:01

Oomph Fortuity


2 Answers

You can try this

<style>
    .msg{
     display: none;
    }
    .ref .msg{
     display: block;
    }

</style>

Edit Note: If you want to apply the 'not'rule I think you would need this structure

<style>
    div:not(.ref){display: none;}
</style>
<div class="msg ref">  Message ref </div>
<div class="msg">  Message 1 </div>
<div class="msg">  Message 2 </div>
<div class="msg">  Message 3 </div>`
like image 71
Frnnd Sgz Avatar answered Mar 24 '23 03:03

Frnnd Sgz


Try the below code:

CSS

.ref :not(.msg) {
    display: none;
}

Html

<div class="ref" >
    <div class="msg">  Message ref 1 </div>
</div>
<div class="ref" >
    <div class="msg1">  Message ref 2 </div>
</div>
<div class="msg">  Message 1 </div>
<div class="msg">  Message 2 </div>
<div class="msg">  Message 3 </div>

make sure you add a space after .ref class.

like image 43
Shibin Joseph Avatar answered Mar 24 '23 03:03

Shibin Joseph