[英]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下方,我想隐藏所有具有“ msg”类的div,但不包括具有相同类的div。
<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. 在这里,我想隐藏所有具有类“ msg”的div,但div除外,后者位于仅使用css的具有“ ref”类的另一个div中。
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. 但这是行不通的,请建议我对CSS样式进行一些必要的调整以实现结果。
Thanks. 谢谢。
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>`
Try the below code: 试试下面的代码:
CSS 的CSS
.ref :not(.msg) {
display: none;
}
Html 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. 确保在.ref类之后添加一个空格。
.msg { display:none; }
.ref .msg { display:block; }
This should be working. 这应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.