简体   繁体   English

如何用相同的类名设置嵌套div的样式?

[英]How to style nested divs with same class name?

I have this problem where I have nested divs with the same class name as their parent that I wish to style slightly differently. 我有一个问题,就是我嵌套的div具有与它们的父类相同的类名,而我希望它们的样式略有不同。 Basically what I want to do is the deeper these divs are nested the less their background color opacity will be. 基本上,我想做的就是这些div嵌套得越深,背景色的不透明度就会越小。 So my code would look something along the lines of this 所以我的代码看起来像这样

<div class="Folder-container"> //Yellow

  <div class="Folder-container">  //Yellow @ 0.5 opacity 

      <div class="folder">
          <div class="file">File</div>
      </div>

      <div class="folder">
          <div class="file">File</div>
      </div>

  </div>

  <div class="Folder-container"> //Yellow @ 0.5 opacity

      <div class="folder">
          <div class="file">File</div>
      </div>

      <div class="folder">
          <div class="file">File</div>
      </div>

  </div>
</div> 

I can't give them unique names as the hierarchy is dynamically generated and some of my functions depend on these divs having the same class name to achieve the same behavior within their nested level. 我无法给它们指定唯一的名称,因为层次结构是动态生成的,并且我的某些功能依赖于这些具有相同类名的div在其嵌套级别内实现相同的行为。 So how can I style these based on their level something like 所以我怎样才能根据它们的水平样式

.Folder-container > Folder-container
{
   //set styling
} 

if at all possible? 如果可能的话?

The trick is that you cannot style the container, but you need to style the folders within them. 诀窍是您无法设置容器的样式,但是需要设置容器中的文件夹的样式。 Otherwise you will put background on background, and this way you can only make them more opague. 否则,您会将背景放在背景上,这样只能使它们更加模糊。 With the way I wrote it down here you can make them more transparent. 用我在这里写下的方式,可以使它们更加透明。

.Folder-container > .folder
{
   background-color: rgba(0,0,0,1);
}

.Folder-container > .Folder-container > .folder
{
   background-color: rgba(0,0,0,.8);
}

.Folder-container > .Folder-container > .Folder-container > .folder
{
   background-color: rgba(255,255,255,.6);
} 

.Folder-container > .Folder-container > .Folder-container > .Folder_Container > .folder
{
   background-color: rgba(255,255,255,.4);
}

The selector would be .Folder-container>.Folder-container but because you're going to display 50% yellow over 100% yellow, it looks just like 100% yellow. 选择器将是.Folder-container>.Folder-container但是由于要显示50%黄色而不是100%黄色,因此看起来就像100%黄色。 So IMO you should display 50% white in the nested div: .Folder-container>.Folder-container{background-color:rgba(255, 255, 255, 0.5);} 因此,海事组织,您应该在嵌套的div中显示50%的白色: .Folder-container>.Folder-container{background-color:rgba(255, 255, 255, 0.5);}

<div class="Folder-container"> //Yellow

  <div class="Folder-container">  //Yellow @ 0.5 opacity 

      <div class="folder">
          <div class="file">File</div>
      </div>

      <div class="folder">
          <div class="file">File</div>
      </div>

  </div>

  <div class="Folder-container"> //Yellow @ 0.5 opacity

      <div class="Folder-container"> //Yellow @ 0.75 opacity
        <div class="folder">
            <div class="file">File</div>
        </div>

        <div class="folder">
            <div class="file">File</div>
        </div>
      </div>
  </div>
</div>

With the following css: 使用以下css:

.Folder-container{background-color:yellow;}
.Folder-container>.Folder-container{background-color:rgba(255, 255, 255, 0.5);}
.Folder-container>.Folder-container>.Folder-container{background-color:rgba(255, 255, 255, 0.75);}

 .Folder-container{background: yellow} .Folder-container > .Folder-container{opacity: 0.5} .Folder-container > .Folder-container .folder{background: #999} 
 <div class="Folder-container"> //Yellow <div class="Folder-container"> //Yellow @ 0.5 opacity <div class="folder"> <div class="file">File</div> </div> <div class="folder"> <div class="file">File</div> </div> </div> <div class="Folder-container"> //Yellow @ 0.5 opacity <div class="folder"> <div class="file">File</div> </div> <div class="folder"> <div class="file">File</div> </div> </div> </div> 

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

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