简体   繁体   English

如何用类除div中的所有元素CSS?

[英]how to except all elements css inside div with class?

I have html for. 我有HTML。 ex. 恩。 like this: 像这样:

<form class="smart-form">
  <div>
    <p></p>
    <i></i>
    <div>
      <span class="classname"><b>text</b></span>
    </div>
  </div>
</form>

And there is css code for all form elements, but I need except this rules to all elements which are inside .classname: 并且所有表单元素都有css代码,但是除了这条规则,我需要对.classname中的所有元素使用此规则:

.smart-form *,
.smart-form *:after,
.smart-form *:before {
  margin: 0;
  padding: 0;
  box-sizing: content-box;
  -moz-box-sizing: content-box;
}

I tried like this: 我这样尝试过:

.smart-form *:not(> .classname *) {} // but its wrong

From this SO answer by @BoltClock♦: 从@ BoltClock♦得到的答案中

:not() only accepts one simple selector at a time; :not()只接受一个简单的选择器; this is mentioned in the Selectors 3 spec: 在Selectors 3规范中提到了这一点:

...

If you want to use this negation by itself, selecting all other elements, then there isn't a way using just a selector. 如果您想单独使用这种否定,选择所有其他元素,那么就没有办法只使用选择器。

In other words, there is no way to nest a descendant selector inside a :not() selector. 换句话说,没有办法将后代选择器嵌套在:not()选择器中。


As an alternative, I'm suggesting a bit of a paradigm shift in your approach: instead of selecting all the elements except the ones inside the class .classname , add all the rules you want for the other elements and then reverse them by adjusting the .classname styles. 作为替代方案,我建议您在方法上进行一些范式转换:不要选择 .classname类中的所有元素以外的所有元素,而是为其他元素添加所需的所有规则,然后通过调整.classname样式。 Ie,: IE中:

.smart-form * {
    /* general CSS styles for .smart-form children */
}
.smart-form .classname, form .classname * {
    /* specific CSS styles for .classname children */
}

This should select all the descendants that are of class .classname . 这应该选择所有.classname类的后代。

 .smart-form *, .smart-form *:after, .smart-form *:before { margin: 0; padding: 0; box-sizing: content-box; -moz-box-sizing: content-box; } /* you can use initial or auto in most cases to set the properties back to default */ .smart-form .classname, .smart-form .classname * { margin: initial; padding: initial; box-sizing: initial; -moz-box-sizing: initial; } 
 <form class="smart-form"> <div> <p></p> <i></i> <div> <span class="classname"><b>text</b></span> </div> </div> </form> 

In the example above, the styles are set to all the elements, but then they are reversed by the ones just for .classname . 在上面的示例中,样式设置为所有元素,但随后仅用于.classname的样式被颠倒。

I think you should not put css on it and just put css on where you want inside your div. 我认为您不应该将CSS放在上面,而应该将CSS放在div中您想要的位置。 What is the point of putting css if you just want to have exception? 如果您只想拥有异常,放置css有什么意义?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CSS Select 除 DIV 内的所有输入以外的所有输入包含 class - CSS Select all inputs except those inside DIV containig class 使用CSS,如何隐藏所有子元素,但第一个无类div除外,它包含具有特定类的子元素? - With CSS, how do I hide all child elements except the first classless div that contains a child with a specific class? css - select 除了在具有特定 class 的 div 中的 ul 元素之外的所有 ul 元素? - css - select all ul elements except ul elements that are in a div with a specific class? 将CSS级联或显式地应用于div内的所有元素? - Apply CSS to all elements inside div with class cascadingly or explicitly? 除了div和所有div之外的所有元素的样式 - styling for all elements except for a div and all divs inside it CSS如何在div中选择具有特定类的元素,除了第一个类之外的类相同 - CSS how to select element with specific class inside div with same the class except the first one 如何选择除一个div以外的所有div中的所有div类 - How select all div class in all div except in one div 选择所有输入,Summernote div中的元素除外 - Select all inputs, except elements inside Summernote's div 将CSS样式应用于DIV中的所有元素 - Applying CSS styles to all elements inside a DIV 如何为div中除img之外的所有元素设置文本对齐方式? - How to set text-align for all elements inside div except img?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM