简体   繁体   English

将CSS应用于特定div的所有元素

[英]Apply a CSS to all the elements of a particular div

I have this code: 我有这个代码:

 <div class="main"> <div class="div1"> <div> <p> <a href="#"></a> </p> <span></span> </div> <p>Loremp Loremp</p> <p>Loremp Loremp</p> <a href="#">Loremp Loremp</a> <span>Hello World</span> </div> <div class="div2"> <p>Loremp Loremp</p> <p>Loremp Loremp</p> <a href="#">Loremp Loremp</a> <span>Hello World</span> </div> </div> 

Is there any way for me to target all the elements of div1 without that being applied on div2 ? 有没有什么办法让我能够在没有应用于div2情况下定位div1所有元素?

What I want to do is to make the CSS code very simple without having to target each element of div1 one by one. 我想要做的是使CSS代码非常简单,而不必逐个定位div1每个元素。

 /*I want to make something like that but without affecting the div2*/ *{ color: blue; } 

Just put 刚刚放

.div1 * {
 color: blue;
}

Or even better, just 或者更好,只是

.div1 {
 color: blue;
}

In my first block of code, all subelements of elements with class div1 will have color: blue applied. 在我的第一个代码块中,具有类div1的元素的所有子元素将具有color: blue应用。

In the second block of code, only the elements with class div1 will have color: blue applied, but all subelements will also inherit it (unless they override it). 在第二个代码块中,只有类div1的元素将具有color: blue应用, 所有子元素也将继承它(除非它们覆盖它)。 Therefore the effect should be the same. 因此效果应该是相同的。

first, NO : Using .div1 * will eventually bite you in the ass. 首先, :使用.div1 *最终会咬你的屁股。

Best would be to apply the style to .div1 {...} and rely on the inheritance. 最好的方法是将样式应用于.div1 {...}并依赖继承。

If you have like text in div1, that you don't want to style you may want to apply the style to the (direct) child-nodes of div1: .div1 > * {...} . 如果你有div1中的文本,你不想要样式,你可能想要将样式应用于div1的(直接)子节点: .div1 > * {...} And rely from this point on, on the inheritance. 从这一点上依靠继承。

Anything more specific like the example proposed on top will have unexpected side-effects and will drive you onto a way where you will have to increase the Specificity of your selectors more and more. 像顶部提出的示例更具体的内容会产生意想不到的副作用,并且会让您越来越需要增加选择器的特异性。
Leading to things like .div1 p ul li .foo div span.bar {...} and overriding it with .div1 p ul li .foo div span.bar * {...} and so on. 导致像.div1 p ul li .foo div span.bar {...} ,并用.div1 p ul li .foo div span.bar * {...}覆盖它等等。 (dramatized) (戏剧)

If your problem are the links in your markup, you shoold consider a generic "reset"/modification of the link-tags so that they fit better into the surrounding text: sth. 如果您的问题是标记中的链接,那么您应该考虑对链接标记进行通用的“重置”/修改,以便它们更好地适应周围的文本:sth。 like. 喜欢。

a,
a:active {
    color: inherit;
}

and maybe you want to restrict even this to a specific context, like .main 也许你想把它限制在特定的上下文中,比如.main

Edit: 编辑:

OK, P.Lionel, that is a different thing; 好的,P.Lionel,这是另一回事; I agree. 我同意。
Using .someSliderPluginClassName * { box-sizing: border-box } is an appropriate way to implement this fast and easy, and it has a manageable amount of risk. 使用.someSliderPluginClassName * { box-sizing: border-box }是实现此快速简便的合适方式,并且具有可管理的风险。

You are using .container (as in your comment) as kind of context for the elements of this slider-plugin and give all control over to this plugin. 您正在使用.container (在评论中)作为此slider-plugin元素的上下文类型,并对此插件进行全部控制。 (Almost) nothing you have to handle/style in this context. (几乎)在这种情况下你无需处理/风格。

On the other hand, you may consider the migration of your styles to box-sizing: border-box . 另一方面,您可以考虑将样式迁移到box-sizing: border-box Imo. 伊莫。 it's the better and more consistent boxing-model, and more and more plugins (and css-frameworks) rely on that. 它是更好,更一致的拳击模型,越来越多的插件(和css框架)依赖于此。

I know, it's kind of an effort now, but it might pay off in the future. 我知道,现在这是一种努力,但它可能在未来得到回报。 Just my 5 cents. 只需我5美分。

You can use a CSS selector with the name of the class you specified for the div attribute in your HTML. 您可以使用CSS选择器,其中包含您在HTML中为div属性指定的类的名称。

.div1 {
   color: blue;
}

Tip: avoid using universal selectors in CSS (such as .div1 * { . Even as it has negligent performance overload , it can have impacts you are not accounting for , besides being the least efficient selector available. 提示:避免在CSS中使用通用选择器(例如.div1 * { 。即使它具有疏忽的性能过载 ,它也会产生影响 ,除了是效率最低的选择器之外,它还没有考虑到

You can match all children with this selector: 您可以使用此选择器匹配所有子项:

.div * {

}

or if you want to match a particular element type: 或者如果要匹配特定的元素类型:

.div * p {

}

See: https://www.w3.org/TR/CSS21/selector.html#descendant-selectors 请参阅: https//www.w3.org/TR/CSS21/selector.html#descendant-selectors

Property color was inherited by default. 默认情况下继承属性color This means you just need to set this property on the parent element, then all child elements will automatically inherit it. 这意味着您只需要在父元素上设置此属性,然后所有子元素将自动继承它。

.div1{
 color: blue;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM