简体   繁体   English

隐藏特定类的所有div,仅使用CSS的一个除外

[英]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.

相关问题 如何使用CSS隐藏html页上除一个div以外的所有元素? - How to hide all element on html page except one div using css? 为网页中的所有图像添加类,但特定类别除外 <div> 使用JavaScript - Adding a class to all the images in the webpage except for a particular <div> using javascript 隐藏除一个之外的所有div - Hide all div's except one 隐藏所有div,但单击的除外 - Hide All div's except the one clicked 使用CSS,如何隐藏所有子元素,但第一个无类div除外,它包含具有特定类的子元素? - With CSS, how do I hide all child elements except the first classless div that contains a child with a specific class? 如何选择除一个div以外的所有div中的所有div类 - How select all div class in all div except in one div 我如何使用 jQuery 删除除特定 class 之外的所有 div 和其他 DOM HTML 元素 - How can i remove all div's and other DOM HTML elements except a particular class using jQuery CSS:not() 选择器 - 除了一个 div 之外,隐藏正文中的所有内容 - CSS :not() selector - Hide everything from body except one div 如何在可可网络视图中隐藏除一个div以外的所有内容? - How to hide all except for one div in a cocoa web view? 隐藏屏幕阅读器中除一个 div 之外的所有元素? - Hide all elements from screen readers except one div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM