简体   繁体   English

如果特定子 div 处于活动状态,则更改 div 的 css

[英]Change css of div, if specific child div's are active

I have a class with 4 different columns.我有一个 class 有 4 个不同的列。

div class="mainContent">
        <div class="left-Col-1" *ngIf="data-1">
        </div>
        <div class="left-Col-2" *ngIf="!data-1">
        </div>
        <div class="right-Col-1" *ngIf="data-2">
        </div>
        <div class="right-Col-2" *ngIf="!data-2">
        </div>
</div>

Basicly i created a flexbox where i show two colums.基本上,我创建了一个 flexbox,其中显示了两个列。 If there's no data-1 in the first column i show div-right-2, and the same happens when theres no data-2, where i show div-left-2.如果第一列中没有 data-1,我会显示 div-right-2,当没有 data-2 时,我会显示 div-left-2。 If they both have data i show div-left-1 and div-right-1.如果他们都有数据,我会显示 div-left-1 和 div-right-1。 My css looks like this:我的 css 看起来像这样:

.mainContent > *:nth-child(1){
 display: flex;
 flex-direction: column;
 flex-basis: 70%;
 padding: 0;
}
.mainContent > *:nth-child(2){
 display: flex;
 flex-direction: column;
 flex-basis: 30%;
 padding: 0;
}

The problem is that i want to change flex-basis to 50% in both div's if there is no data loaded (if first div is "left-div-2" and the second div "right-div-2").问题是,如果没有加载数据(如果第一个 div 是“left-div-2”,第二个 div 是“right-div-2”),我想将两个 div 的 flex-basis 更改为 50%。 Is that possible only using css or do i need to write some fucntion on typescript?这可能只使用 css 还是我需要在 typescript 上写一些功能?

Ty for your help.谢谢你的帮助。

You might be confusing yourself by using an odd/even selector that you don't understand.您可能会因使用您不理解的奇数/偶数选择器而感到困惑。 Your selector splits your divs into odd and even children and the only difference is the flex-basis is 70% for the odd ones and 30% for the even.您的选择器将您的 div 分成奇数子和偶数子,唯一的区别是 flex-basis 是奇数的 70% 和偶数的 30%。 With the change you are describing you have 3 different values for 4 different classes, so don't use the odd even thing.通过您所描述的更改,您对 4 个不同的类有 3 个不同的值,所以不要使用奇偶数。 Angular is going to remove two of those divs based on the content of data-1 anyway.无论如何,Angular 将根据 data-1 的内容删除其中两个 div。

Since you are using four different classes for your four divs you might as well use them to specify what flex-basis you want for each.由于您为四个 div 使用了四个不同的类,因此您不妨使用它们来指定您想要的每个 flex-basis。 See if you can understand what this css does.看看你能不能看懂这个css是做什么的。

.mainContent{
    display: flex;
    flex-direction: column;
    padding: 0;
}

.mainContent .left-Col-1{
    flex-basis: 70%;
}
.mainContent .right-Col-1{
    flex-basis: 30%;
}
.mainContent .left-Col-2, .mainContent .right-Col-2{
    flex-basis: 50%;
}

The best would probably be to use classes that are more descriptive like full/empty and primary/secondary.最好的可能是使用更具描述性的类,如完整/空和主要/次要。 That would make your code more clear and easy to manage.这将使您的代码更加清晰和易于管理。

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

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