简体   繁体   English

在angular2中一起使用css选择器lastchild和:after

[英]Using css selector lastchild and :after together in angular2

HTML: HTML:

<h2 class="title-main">
     <span class="title-inside">A</span>
     <span class="title-inside">B</span>
     <span class="title-inside">C</span>
     <span class="title-inside">D</span>
     <span class="title-inside">E</span>
     <span class="title-outside">F</span>
     <span class="title-outside">G</span>
</h2>

CSS: CSS:

:host/deep/.title-main {
    display: flex;
    margin-bottom: 0;

    .title-inside {
        &:after {
            content : '-';
        }
    }
}

I am getting the output like this 我正在得到这样的输出

A-B-C-D-E-F G

I don't want the - for the last child. 我不要-最后一个孩子。 I want the output like this 我想要这样的输出

A-B-C-D-E F G

i tried many options. 我尝试了很多选择。 For eg. 例如。 one of them which i tried 我尝试过的其中之一

title-inside:last-child ::after {
           content: ''
    }

But this is not working. 但这是行不通的。 Could anyone please help me 谁能帮我

use :not with :nth-last-of-type() 使用:not:nth-last-of-type()

 .title-main { display: flex; margin-bottom: 0; } .title-inside:not(:nth-last-of-type(3))::after { content: '-'; } 
 <h2 class="title-main"> <span class="title-inside">A</span> <span class="title-inside">B</span> <span class="title-inside">C</span> <span class="title-inside">D</span> <span class="title-inside">E</span> <span class="title-outside">F</span> <span class="title-outside">G</span> </h2> 

Here is the SASS version 这是SASS版本

:host/deep/.title-main {
    display: flex;
    margin-bottom: 0;

    .title-inside {
        &:not(:nth-last-of-type(3)){
           &::after{
            content : '-';
           }
        }
    }
}

Since :nth-* and last-child selectors use the actual element type and not class name, I think it is better to use a class selector. 因为:nth-*last-child选择器使用实际的元素类型而不是类名,所以我认为最好使用类选择器。

By using the ::before pseudo instead, combined with the sibling selector + , it does not matter how many items each class has, it will always count right. 通过使用::before伪方法,再与同级选择器+结合使用,无论每个类有多少个项目,它总是可以正确计数。

 .title-main { display: flex; margin-bottom: 0; } .title-main .title-inside + .title-inside::before { content: '-'; } 
 <h2 class="title-main"> <span class="title-inside">A</span> <span class="title-inside">B</span> <span class="title-inside">C</span> <span class="title-inside">D</span> <span class="title-inside">E</span> <span class="title-outside">F</span> <span class="title-outside">G</span> </h2> <h2 class="title-main"> <span class="title-inside">A</span> <span class="title-inside">B</span> <span class="title-inside">C</span> <span class="title-outside">D</span> <span class="title-outside">E</span> <span class="title-outside">F</span> <span class="title-outside">G</span> </h2> 


SASS 上海社会科学院

:host/deep/.title-main {
    display: flex;
    margin-bottom: 0;

    .title-inside {
      &+.title-inside {
        &::before{
          content : '-';
        }
      }
    }
}

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

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