如何仅将CSS应用于特定班级的直系子女 [英] How to apply CSS to only immediate children of a certain class

查看:99
本文介绍了如何仅将CSS应用于特定班级的直系子女的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div,在那个div中,我想创建另一个具有不同类的div,并使内部div与外部div CSS设置完全分开.

I have a div and in that div I want to create another div with a different class and have the inner div completely separated from the outer div CSS settings.

赞:

<div class="outer">
    <div><h1> h1 </h1><div>
    <div class="inner">
        <h2> h2 </h2>
    </div>
    <h2> h2 </h2>
</div>

.outer h1{ color:blue; }
.outer h2{ color:red; }

.inner { height: 111px; }

我要取消"inner" div

似乎不只是将颜色加重为黑色似乎是愚蠢的,但是如果我在CSS中有更多参数甚至是动态的,该怎么办?

It might seem stupid not to just overweight the color to black here, but what if I have a lot more arguments in the CSS or even if it's dynamic.

我想还不清楚,但实际上我需要的是与我到目前为止所获得答案相反的东西.我有一个主容器div,它在CSS中有很多设置.现在,我要在没有任何主容器CSS设置的情况下将div插入主容器.

I guess it isn't clear, but what I need is actually kind of the opposite of the answers I got so far. I have a main-container div and it has alot of settings in the CSS. Now I want to insert a div into the main-container without it having any of the main-container CSS settings.

推荐答案

.outer > h2 { color:red; }

这样,只有外部div的直接子级才能获得此颜色值,才可以解决此工作.

this way only the direct child of the outer div get this color value, should fix the job.

这篇关于如何仅将CSS应用于特定班级的直系子女的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆